/*
 * XMLBench.java
 *
 * Created on February 20, 2003, 7:09 PM
 */

package XMLBench;

import java.io.*;
import java.util.*;
import org.tienshiao.XMLLite.*;               // My XML Parser
import nanoxml.*;               // nanoxml (modified for j2me)
import com.exploringxml.xml.*;  // Xparse-J 1.1
import com.alsutton.xmlparser.*;
import com.alsutton.xmlparser.objectmodel.*;

/**
 *
 * @author  Tienshiao Ma
 */
public class XMLBench {
    
    /** Creates a new instance of XMLBench */
    public XMLBench() {
    }
    
    private static void testNothing(String s) {
    }
    
    private static void testReaderSetup(String s) {
        ByteArrayInputStream bin = 
            new ByteArrayInputStream(s.getBytes());
        new InputStreamReader(bin);
    }
    
    private static void testMyXML(String s) throws IOException, XMLParserException {
        ByteArrayInputStream bin = 
            new ByteArrayInputStream(s.getBytes());
        XMLElement test = new XMLElement();
        test.parseFromReader(new InputStreamReader(bin));
    }
    
    private static void testNanoXML(String s) throws IOException {
        ByteArrayInputStream bin = 
            new ByteArrayInputStream(s.getBytes());
        kXMLElement root = new kXMLElement();
        root.parseFromReader(new InputStreamReader(bin));
    }
    
    private static void testXparseJ(String s) {
        com.exploringxml.xml.Node root = new Xparse().parse(s);
    }
    
    private static void testAlSuttons(String s) throws IOException {
        ByteArrayInputStream bin = 
            new ByteArrayInputStream(s.getBytes());        
        TreeBuilder tb = new TreeBuilder();
        com.alsutton.xmlparser.objectmodel.Node xmlModel = tb.createTree(new InputStreamReader(bin));
    }
        
    public static void runTests(String testString) throws IOException, XMLParserException {
        int iterations = 5000;
        long start;
        long stop;
        
        System.out.println("Test String is " + testString.length() + " chars.");
        
        testReaderSetup(testString);
        System.out.println("TestReaderSetup start ...");
        System.gc();
        start = System.currentTimeMillis();
        for (int i = 0; i < iterations; i++) {
            testReaderSetup(testString);
        }
        stop = System.currentTimeMillis();
        System.out.println("... TestReaderSetup end: " + (stop - start));
        
        testMyXML(testString);
        System.out.println("TestMyXML start ...");
        System.gc();
        start = System.currentTimeMillis();
        for (int i = 0; i < iterations; i++) {
            testMyXML(testString);
        }
        stop = System.currentTimeMillis();
        System.out.println("... TestMyXML end: " + (stop - start));
        
        testNanoXML(testString);
        System.out.println("TestNanoXML start ...");
        System.gc();
        start = System.currentTimeMillis();
        for (int i = 0; i < iterations; i++) {
            testNanoXML(testString);
        }
        stop = System.currentTimeMillis();
        System.out.println("... TestNanoXML end: " + (stop - start));

        testXparseJ(testString);
        System.out.println("TestXparseJ start ...");
        System.gc();
        start = System.currentTimeMillis();
        for (int i = 0; i < iterations; i++) {
            testXparseJ(testString);
        }
        stop = System.currentTimeMillis();
        System.out.println("... TestXparseJ end: " + (stop - start));
        
        testAlSuttons(testString);
        System.out.println("TestAlSuttons start ...");
        System.gc();  
        start = System.currentTimeMillis();
        for (int i = 0; i < iterations; i++) {
            testAlSuttons(testString);
        }
        stop = System.currentTimeMillis();
        System.out.println("... TestAlSuttons end: " + (stop - start));
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException, XMLParserException {
        // simple example
        runTests("<b>Hello World</b>");
        
        // SOAP example
        runTests("<SOAP-ENV:Envelope\n" +
                 "  xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
                 "  SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" +
                 "   <SOAP-ENV:Header>\n" +
                 "       <t:Transaction\n" +
                 "         xmlns:t=\"some-URI\"\n" +
                 "         xsi:type=\"xsd:int\" mustUnderstand=\"1\">\n" +
                 "           5\n" +
                 "       </t:Transaction>\n" +
                 "   </SOAP-ENV:Header>\n" +
                 "   <SOAP-ENV:Body>\n" +
                 "       <m:GetLastTradePriceResponse\n" +
                 "         xmlns:m=\"Some-URI\">\n" +
                 "           <Price>34.5</Price>\n" +
                 "       </m:GetLastTradePriceResponse>\n" +
                 "   </SOAP-ENV:Body>\n" +
                 "</SOAP-ENV:Envelope>\n");

        // deep example
        runTests("<sometag depth=\"0\">\n" +
                 "  <sometag depth=\"1\">\n" +
                 "    <sometag depth=\"2\">\n" +
                 "      <sometag depth=\"3\">\n" +
                 "        <sometag depth=\"4\">\n" +
                 "          <sometag depth=\"5\">\n" +
                 "            <sometag depth=\"6\">\n" +
                 "              <sometag depth=\"7\">\n" +
                 "                <sometag depth=\"8\">\n" +
                 "                  <sometag depth=\"9\">\n" +
                 "                    <sometag depth=\"10\">\n" +
                 "                      No more\n" +
                 "                    </sometag>\n" +
                 "                  </sometag>\n" +
                 "                </sometag>\n" +
                 "              </sometag>\n" +
                 "            </sometag>\n" +
                 "          </sometag>\n" +
                 "        </sometag>\n" +
                 "      </sometag>\n" +
                 "    </sometag>\n" +
                 "  </sometag>\n" +
                 "</sometag>\n");
        
        // long example
        runTests("<?xml version=\"1.0\"?>\n" +
                 "<!DOCTYPE rss PUBLIC\n" +
                 "  \"-//Netscape Communications//DTD RSS 0.91//EN\"\n" + 
                 "  \"http://my.netscape.com/publish/formats/rss-0.91.dtd\"\n" +
                 ">\n" +
                 "<rss version=\"0.91\">\n" +
                 "  <channel>\n" + 
                 "    <title>Meerkat: An Open Wire Service</title>\n" +
                 "    <link>http://meerkat.oreillynet.com</link>\n" +
                 "    <description>\n" +
                 "      Meerkat is a Web-based syndicated content reader \n" +
                 "      providing a simple interface to RSS stories.  While\n" +
                 "      maintaining the original association of a story with\n" +
                 "      a channel, Meerkat's focus is on chronological\n" +
                 "      order -- the latest stories float to the top,\n" +
                 "      regardless of their source.\n" +
                 "    </description>\n" +
                 "    <language>en-us</language>\n" +
                 "    <image>\n" +
                 "      <title>Meerkat Powered!</title> \n" +
                 "      <url>\n" +
                 "      http://meerkat.oreillynet.com/icons/meerkat-powered.jpg\n" +
                 "      </url>\n" +
                 "      <link>http://meerkat.oreillynet.com</link> \n" +
                 "      <width>88</width> \n" +
                 "      <height>31</height> \n" +
                 "      <description>Visit Meerkat in full splendor at\n" +
                 "          meerkat.oreillynet.com...</description> \n" +
                 "    </image> \n" +
                 "    <item>\n" +
                 "      <title>MmO2 cuts jobs, to take GBP110m charge</title>\n" +
                 "      <link>http://c.moreover.com/click/here.pl?r31561327\n" +
                 "          </link>\n" +
                 "      <description>FTMarketWatch Feb 5 2002 5:05AM ET...\n" +
                 "          </description>\n" +
                 "    </item>\n" +
                 "    <item>\n" +
                 "      <title>S.E.C. Says Motorola Cant Exclude Audit\n" +
                 "          Proposal</title>\n" +
                 "      <link>http://c.moreover.com/click/here.pl?r31562096\n" +
                 "          </link>\n" +
                 "      <description>New York Times Feb 5 2002 5:17AM ET...\n" +
                 "          </description>\n" +
                 "    </item>\n" +
                 "    <item>\n" +
                 "      <title>1,900 jobs to go at mmO2</title>\n" +
                 "      <link>http://c.moreover.com/click/here.pl?r31562134\n" +
                 "          </link>\n" +
                 "      <description>ZDNet Feb 5 2002 5:18AM ET..\n" +
                 "          </description>\n" +
                 "    </item>\n" +
                 "    <item>\n" +
                 "      <title>Mobile firm cutting 1,900 jobs</title>\n" +
                 "      <link>http://c.moreover.com/click/here.pl?r31558750\n" +
                 "          </link>\n" +
                 "      <description>CNN Europe Feb 5 2002 4:30AM ET...\n" +
                 "          </description>\n" +
                 "    </item>\n" +
                 "    <item>\n" +
                 "      <title>The axe falls at mmO2</title>\n" +
                 "      <link>http://c.moreover.com/click/here.pl?r31558856\n" +
                 "          </link>\n" +
                 "      <description>The Register Feb 5 2002 4:32AM ET...\n" +
                 "          j</description>\n" +
                 "    </item>\n" +
                 "    <item>\n" +
                 "      <title>mmO2 plans to axe 1,900 jobs</title>\n" +
                 "      <link>http://c.moreover.com/click/here.pl?r31559617\n" +
                 "          </link>\n" +
                 "      <description>Evening Standard Feb 5 2002 4:42AM ET...\n" +
                 "          </description>\n" +
                 "    </item>\n" +
                 "    <item>\n" +
                 "      <title>UPDATE 2-Sohu Q4 revenues up 15 pct on wireless\n" +
                 "          services</title>\n" +
                 "      <link>http://c.moreover.com/click/here.pl?r31557811\n" +
                 "          </link>\n" +
                 "      <description>CNET Feb 5 2002 4:08AM ET...</description>\n" +
                 "    </item>\n" +
                 "  </channel>\n" +
                 "</rss>");

    }

}
