我正在测试DOM,SAX和StAX的效率。Java解析器测试
基本上我所做的是我使用弹簧秒表和不同大小的XML,然后比较结果。
我还认为我可以测量时间,而元素加载到对象和对象数组,但这与分析无关。
这里是我的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());
代码为 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());
我的问题是:我这样做对吗?还有其他的方法来测试解析器吗? 谢谢
我不明白第二个感叹号。我应该用建筑物来衡量吗? :) – ivanz 2013-05-02 00:41:59
@sevdah - 通过触摸所有数据我的意思是将所有文本节点和属性值作为字符串,以确保测试尽可能均匀。例如,一个懒惰的DOM impl在解析调用时可能不会做任何事情,并且只会在树遍历时才构建DOM节点,使得解析调用显得非常快。 – 2013-05-02 00:48:06
这是我的第一个测试版本,但是后来我意识到我的用于获取所有节点的代码并不一定要“写得很好”。另一件事是我有system.out.println中的所有文本节点,这也需要一些时间。那么我应该如何使遍历整个文档,但没有什么可以显示? – ivanz 2013-05-02 00:53:10