4 Replies Latest reply on Oct 8, 2009 10:44 AM by jesper.pedersen

    performance test of fast jaxb, xb, jaxb

    aloubyansky

      I've added JAXB to the mix. And also modified the test a bit.
      There is ParsingCommand per framework (fast jaxb, xb and jaxb). run() method of each of them unmarshals Person object from the same person.xml one time.
      These commands are executed one after the other. The time (in ms) is checked separately for the first run() and then for the next X repetitions.

      The command executed first appears to do the "warm up" for the others (perhaps caching, classloading...). I.e. here are the results for the first run in different sequences:

      fast jaxb - xb - jaxb:
      fast jaxb total=123, avg=123.0
      xb total=53, avg=53.0
      jaxb total=9, avg=9.0

      xb - fast jaxb - jaxb
      fast jaxb total=7, avg=7.0
      xb total=163, avg=163.0
      jaxb total=9, avg=9.0

      jaxb - xb - fast jaxb
      fast jaxb total=7, avg=7.0
      xb total=49, avg=49.0
      jaxb total=127, avg=127.0


      Here are the results for the next repetitions (i.e. not counting the first run) for sequence fast jaxb - xb - jaxb:

      --- next 1 repetitions
      fast jaxb total=2, avg=2.0
      xb total=9, avg=9.0
      jaxb total=3, avg=3.0

      --- next 10 repetitions
      fast jaxb total=15, avg=15.0
      xb total=49, avg=49.0
      jaxb total=38, avg=38.0

      --- next 100 repetitions
      fast jaxb total=106, avg=106.0
      xb total=243, avg=243.0
      jaxb total=177, avg=177.0

      --- next 1000 repetitions
      fast jaxb total=838, avg=838.0
      xb total=1596, avg=1596.0
      jaxb total=1368, avg=1368.0

      So, apparently, fast jaxb is in fact the fastest (even in the first and worst warming up run).

      For the protocol, here is the test code

      private long perCommandReps = 1;
       public void testMixed() throws Exception
       {
       FastJaxbParsing fastJaxb = new FastJaxbParsing(perCommandReps);
       XBParsing xb = new XBParsing(perCommandReps);
       JAXBParsing jaxb = new JAXBParsing(perCommandReps);
      
       ParsingCommand[] c = new ParsingCommand[3];
       c[0] = fastJaxb;
       c[1] = xb;
       c[2] = jaxb;
      
       for(int j = 0; j < c.length; ++j)
       c[j].run();
      
       System.out.println("--- first run");
       System.out.println("fast jaxb total=" + fastJaxb.getTotal() + ", avg=" + fastJaxb.getAverage());
       System.out.println("xb total=" + xb.getTotal() + ", avg=" + xb.getAverage());
       System.out.println("jaxb total=" + jaxb.getTotal() + ", avg=" + jaxb.getAverage());
      
       for(int j = 0; j < c.length; ++j)
       c[j].reset();
      
       int repetitions = 1000;
       for(int i = 0; i < repetitions; ++i)
       {
       for(int j = 0; j < c.length; ++j)
       c[j].run();
       }
      
       System.out.println("--- next " + repetitions + " repetitions");
       System.out.println("fast jaxb total=" + fastJaxb.getTotal() + ", avg=" + fastJaxb.getAverage());
       System.out.println("xb total=" + xb.getTotal() + ", avg=" + xb.getAverage());
       System.out.println("jaxb total=" + jaxb.getTotal() + ", avg=" + jaxb.getAverage());
       }
      
       private static class JAXBParsing extends ParsingCommand
       {
       private JAXBContext ctx;
      
       protected JAXBParsing(long repetitions)
       {
       super(repetitions);
       }
      
       @Override
       protected void setup() throws Exception
       {
       super.setup();
       ctx = JAXBContext.newInstance(Person.class);
       }
      
       @Override
       protected Object parse() throws Exception
       {
       javax.xml.bind.Unmarshaller unmarshaller = ctx.createUnmarshaller();
       return unmarshaller.unmarshal(new InputSource(xmlUri));
       }
       }
      
       private static class XBParsing extends ParsingCommand
       {
       private static final UnmarshallerFactory factory = UnmarshallerFactory.newInstance();
       private SchemaBinding schema;
      
       protected XBParsing(long repetitions)
       {
       super(repetitions);
       }
      
       @Override
       protected void setup() throws Exception
       {
       super.setup();
       schema = JBossXBBuilder.build(Person.class);
       }
      
       @Override
       protected Object parse() throws Exception
       {
       Unmarshaller unmarshaller = factory.newUnmarshaller();
       return unmarshaller.unmarshal(xmlUri, schema);
       }
       }
      
       private static class FastJaxbParsing extends ParsingCommand
       {
       private static SAXParserFactory factory = SAXParserFactory.newInstance();
       static
       {
       factory.setNamespaceAware(true);
       }
      
       private Sax sax;
      
       protected FastJaxbParsing(long repetitions)
       {
       super(repetitions);
       }
      
       @Override
       protected Object parse() throws Exception
       {
       SAXParser parser = factory.newSAXParser();
       parser.parse(xmlUri, sax);
       return sax.getRoot();
       }
      
       @Override
       protected void setup() throws Exception
       {
       super.setup();
       HashMap<String, Handler> map = new HashMap<String, Handler>();
       map.put("urn:person", new Person_Parser());
       sax = new Sax(map);
       }
       }
      
       private static abstract class ParsingCommand
       {
       protected long repetitions;
       protected long total;
       protected String xmlUri;
       protected Object o;
      
       protected ParsingCommand(long repetitions)
       {
       this.repetitions = repetitions;
       }
      
       public void reset()
       {
       total = 0;
       o = null;
       }
      
       public long getTotal()
       {
       return total;
       }
      
       public double getAverage()
       {
       return ((double)total)/repetitions;
       }
      
       public long getRepetitions()
       {
       return repetitions;
       }
      
       public Object getResult()
       {
       return o;
       }
      
       protected void setup() throws Exception
       {
       xmlUri = Thread.currentThread().getContextClassLoader().getResource("person.xml").toExternalForm();
       }
      
       protected abstract Object parse() throws Exception;
      
       public void run() throws Exception
       {
       setup();
       for(int i = 0; i < repetitions; ++i)
       {
       long start = System.currentTimeMillis();
       o = parse();
       total += System.currentTimeMillis() - start;
       }
       }
       }