|
From: Kalle A. <kal...@gm...> - 2013-08-22 14:46:57
|
Hello,
I'm trying to evaluate the performance of vtd for my application after
perusing the benchmark results published on the main page. However, I'm
seeing quite a substantial difference in performance of XPath evaluation
between vtd and the standard DOM/JDK implementation.
My simple test evaluates a simple XPath expression ("name()") from the
context of a specific tag one million times in a row and these are the
results I am seeing (in ms):
Start DOM
End DOM
Time Taken : 3023.547643
Start VTD
End VTD
Time Taken : 10416.065461
My DOM implementation consistently outperforms the vtd 3x-4x. Am I doing
something wrong? I would be grateful for any feedback.
Note1: I've included the full code of my test at the end of this e-mail.
Note2: I've used the same XPath expression in each iteration for testing
purposes only, in reality there will be 1000's of different expressions.
Note3: I'm using Java 7 & VTD-XML version 2.11
Thanks,
/Kalle
=== BEGIN TEST XML FILE ===
<?xml version="1.0" encoding="utf-8"?>
<TestLevel1>
<TestLevel2>
<TestLevel3 attr="test"></TestLevel3>
</TestLevel2>
</TestLevel1>
=== END TEST XML FILE ===
=== BEGIN SOURCE CODE ===
import com.ximpleware.AutoPilot;
import com.ximpleware.NavException;
import com.ximpleware.VTDGen;
import com.ximpleware.VTDNav;
import com.ximpleware.XPathParseException;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.Callable;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
public class XPathTest {
private static final String NODE_SELECTION_EXPRESSION = "//TestLevel3";
private static final String GET_NAME_EXPRESSION = "name()";
private XPathTest() {}
public static void main(String[] args) {
File file = new File("xpath_test.xml");
runXPathDomTest(file);
runXPathVtdTest(file);
}
private static void runXPathDomTest(File file) {
runTest(new DomCallable(file), "DOM");
}
private static void runXPathVtdTest(File file) {
runTest(new VtdCallable(file), "VTD");
}
private static void runTest(Callable<String> callable, String name) {
System.out.println();
System.out.println("Start " + name);
long startTime = System.nanoTime();
try {
for (int i = 0; i < 1000000; i++) {
String output = callable.call();
if (!"TestLevel3".equals(output)) {
System.out.println("Failed: " + output);
break;
}
}
} catch (Exception exception) {
System.out.println(exception);
}
long endTime = System.nanoTime();
double difference = (endTime - startTime)/1e6;
System.out.println("End " + name);
System.out.println("Time Taken : " + difference);
System.out.println();
}
private static class DomCallable implements Callable<String> {
private final File fFile;
private final Node fNode;
private final XPath fXPath;
private DomCallable(File file) {
fFile = file;
try {
Document document =
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
fXPath = XPathFactory.newInstance().newXPath();
XPathExpression expression =
fXPath.compile(NODE_SELECTION_EXPRESSION);
fNode = (Node)expression.evaluate(document,
XPathConstants.NODE);
} catch (ParserConfigurationException | SAXException |
IOException | XPathExpressionException exception) {
throw new RuntimeException(exception);
}
}
@Override
public String call() {
try {
XPathExpression expression =
fXPath.compile(GET_NAME_EXPRESSION);
return (String)expression.evaluate(fNode,
XPathConstants.STRING);
} catch (XPathExpressionException e) {
throw new RuntimeException(e);
}
}
}
private static class VtdCallable implements Callable<String> {
private final File fFile;
private final AutoPilot fAutoPilot;
private VtdCallable(File file) {
fFile = file;
VTDGen vg = new VTDGen();
vg.parseFile(fFile.getAbsolutePath(), false);
VTDNav vn = vg.getNav();
fAutoPilot = new AutoPilot(vn);
try {
fAutoPilot.selectElement(NODE_SELECTION_EXPRESSION);
fAutoPilot.iterate();
} catch (NavException exception) {
throw new RuntimeException(exception);
}
}
@Override
public String call() {
try {
fAutoPilot.selectXPath(GET_NAME_EXPRESSION);
return fAutoPilot.evalXPathToString();
} catch (XPathParseException e) {
throw new RuntimeException(e);
}
}
}
}
=== END SOURCE CODE ===
|