1

i am testing efficiency of DOM, SAX and StAX.

Basically what i do is that i use spring stopwatch and different sizes of XML and then compare results.

I also thought that i could measure time while elements are loaded to objects and objects to array, but that has nothing to do with parsring.

here are my codes for SAX

  StopWatch stopWatch = new StopWatch("SAX");
  stopWatch.start("SAX");  
  SAXParserFactory spf = SAXParserFactory.newInstance();
  spf.setValidating(false);
  SAXParser sp = spf.newSAXParser();
  XMLReader parser = sp.getXMLReader();
  parser.setErrorHandler(new Chyby());
  parser.setContentHandler(new DefaultHandler());
  parser.parse(file);
 stopWatch.stop();
 System.out.println(stopWatch.prettyPrint());

for StAX

  int temp = 0;
  StopWatch stopWatch = new StopWatch("StAX");
  stopWatch.start("StAX");    
  XMLInputFactory f = XMLInputFactory.newInstance();
  XMLStreamReader r = f.createXMLStreamReader( new FileInputStream( file ));   
    while (r.hasNext()==true){
    temp++;
    r.next();
    }
     System.out.println("parsed");
  stopWatch.stop();
 System.out.println(stopWatch.prettyPrint());

DOM

StopWatch stopWatch = new StopWatch("DOM");
stopWatch.start("DOM");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(subor);
System.out.println("parsed");
System.out.println("----------------\n");
    stopWatch.stop();
 System.out.println(stopWatch.prettyPrint());

My question is: Am i doing it right? is there other approach for testing parsers? Thanks

4

2 回答 2

2

创建 JAXP 工厂类是一项非常昂贵的操作,其成本很大程度上取决于类路径中存在哪些 JAR。你真的不想衡量它。

您需要注意消除 Java 启动成本。在开始测量之前解析一些文档。重复运行测量,平均结果,并检查结果是否一致。

我会使用不同大小的文档进行测试。通常,成本为 (ax+b),其中 x 是文档大小。此处的数字“b”表示“每个文档的开销”,如果文档很小,则可能非常重要。

在 DOM 的情况下,很可能会发生垃圾收集,这可能会扭曲结果,因为它们发生在不可预测的时间。有时建议在已知时间强制进行垃圾收集以获得一致的测量结果。

于 2013-05-02T06:51:05.083 回答
1

您可能希望将工厂的创建排除在性能运行之外或单独测量它们。您可能想要触摸所有数据以防止解析器错误地看起来很好,从而懒惰地构建对象。

于 2013-05-02T00:39:21.923 回答