From 363f60ac01d819bf2b4bf57d72be05e7b9372c1a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kim=20Nguy=E1=BB=85n?= Date: Wed, 12 Apr 2017 12:31:12 +0200 Subject: [PATCH] Add Saxon benchmark. --- tests/XPathEval.java | 50 ++++++++++++++++++ tests/XPathEvalSaxon.java | 59 +++++++++++++++++++++ tests/XPathExample.java | 105 ++++++++++++++++++++++++++++++++++++++ tests/gen_ref.sh | 19 +++++++ tests/gen_ref_saxon.sh | 19 +++++++ 5 files changed, 252 insertions(+) create mode 100644 tests/XPathEval.java create mode 100644 tests/XPathEvalSaxon.java create mode 100644 tests/XPathExample.java create mode 100755 tests/gen_ref.sh create mode 100755 tests/gen_ref_saxon.sh diff --git a/tests/XPathEval.java b/tests/XPathEval.java new file mode 100644 index 0000000..8ce8db1 --- /dev/null +++ b/tests/XPathEval.java @@ -0,0 +1,50 @@ +import javax.xml.xpath.*; +import org.xml.sax.*; +import org.w3c.dom.*; +import javax.xml.transform.*; +import javax.xml.transform.dom.*; +import javax.xml.transform.stream.*; + +public class XPathEval { + + + public static void main(String args[]) { + try { + + + XPath xpath = XPathFactory.newInstance().newXPath(); + String expression = args[1]; + InputSource inputSource = new InputSource(args[0]); + long startTime = System.nanoTime(); + NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET); + long evalTime = (System.nanoTime() - startTime) / 1000000; + Transformer serializer = TransformerFactory.newInstance().newTransformer(); + serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + StreamResult o = new StreamResult(System.out); + startTime = System.nanoTime(); + System.out.println(""); + for(int i = 0; i < nodes.getLength(); i++){ + Node n = nodes.item(i); + switch (n.getNodeType()) { + case Node.ATTRIBUTE_NODE: + System.out.print (n.getNodeName() + "=" + n.getNodeValue()); + break; + default: + serializer.transform(new DOMSource(nodes.item(i)), o); + break; + }; + System.out.println(); + }; + System.out.println(""); + long printTime = (System.nanoTime() - startTime) / 1000000; + System.err.println("evaluation time: " + evalTime + "ms"); + System.err.println("serialization time: " + printTime + "ms"); + } catch (XPathException e) { + System.out.println (e.getCause()); + } catch (Exception e) { + System.out.println(e); + }; + + + } +} diff --git a/tests/XPathEvalSaxon.java b/tests/XPathEvalSaxon.java new file mode 100644 index 0000000..0f8fd2e --- /dev/null +++ b/tests/XPathEvalSaxon.java @@ -0,0 +1,59 @@ +//import javax.xml.xpath.*; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathException; +import net.sf.saxon.xpath.XPathFactoryImpl; +import net.sf.saxon.om.NodeInfo; +import org.xml.sax.*; +import org.w3c.dom.*; +import javax.xml.transform.*; +import javax.xml.transform.dom.*; +import javax.xml.transform.stream.*; +import java.util.ArrayList; +import net.sf.saxon.type.Type; + + +public class XPathEvalSaxon { + + + public static void main(String args[]) { + try { + + + XPath xpath = (new XPathFactoryImpl()).newXPath(); + String expression = args[1]; + InputSource inputSource = new InputSource(args[0]); + long startTime = System.nanoTime(); + @SuppressWarnings("unchecked") + ArrayList nodes = (ArrayList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET); + long evalTime = (System.nanoTime() - startTime) / 1000000; + Transformer serializer = TransformerFactory.newInstance().newTransformer(); + serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + StreamResult o = new StreamResult(System.out); + startTime = System.nanoTime(); + System.out.println(""); + for(int i = 0; i < nodes.size(); i++){ + NodeInfo n = nodes.get(i); + switch (n.getNodeKind()) { + case net.sf.saxon.type.Type.ATTRIBUTE: + System.out.print (n.getLocalPart() + "=" + n.getStringValue()); + break; + default: + serializer.transform(n, o); + break; + }; + System.out.println(); + }; + System.out.println(""); + long printTime = (System.nanoTime() - startTime) / 1000000; + System.err.println("evaluation time: " + evalTime + "ms"); + System.err.println("serialization time: " + printTime + "ms"); + } catch (XPathException e) { + System.out.println (e.getCause()); + } catch (Exception e) { + System.out.println(e); + }; + + + } +} diff --git a/tests/XPathExample.java b/tests/XPathExample.java new file mode 100644 index 0000000..326cb03 --- /dev/null +++ b/tests/XPathExample.java @@ -0,0 +1,105 @@ +import org.xml.sax.*; +import org.w3c.dom.*; +import javax.xml.transform.*; +import javax.xml.transform.dom.*; +import javax.xml.transform.stream.*; +import java.util.ArrayList; +import net.sf.saxon.Configuration; +import net.sf.saxon.lib.NamespaceConstant; +import net.sf.saxon.om.DocumentInfo; +import net.sf.saxon.om.NodeInfo; +import net.sf.saxon.xpath.XPathFactoryImpl; +import org.xml.sax.InputSource; + +import javax.xml.namespace.QName; +import javax.xml.transform.sax.SAXSource; +import javax.xml.xpath.*; +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStreamReader; +import java.util.Iterator; +import java.util.List; +import net.sf.saxon.type.Type; + + +public class XPathExample { + + + public static void main (String args[]) { + + if (args.length != 2) { + System.err.println("Usage: java XPathEvalSaxon input-file '/xpath/expression'"); + System.exit(1); + } + + try { + + System.setProperty("javax.xml.xpath.XPathFactory:"+NamespaceConstant.OBJECT_MODEL_SAXON, + "net.sf.saxon.xpath.XPathFactoryImpl"); + + XPathFactory xpf = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON); + + XPath xpe = xpf.newXPath(); + + System.err.println("Loaded XPath Provider " + xpe.getClass().getName()); + + long startTime = System.nanoTime (); + InputSource is = new InputSource(args[0]); + SAXSource ss = new SAXSource(is); + Configuration config = ((XPathFactoryImpl) xpf).getConfiguration(); + NodeInfo doc = config.buildDocumentTree(ss).getRootNode(); + long stopTime = System.nanoTime (); + System.err.println ("Loading document : " + (stopTime - startTime) / 1000000 + "ms"); + + startTime = System.nanoTime (); + XPathExpression cexpr = xpe.compile(args[1]); + stopTime = System.nanoTime (); + System.err.println ("Compiling XPath expression : " + (stopTime - startTime) / 1000000 + "ms"); + + startTime = System.nanoTime (); + @SuppressWarnings("unchecked") + ArrayList nodes = (ArrayList) cexpr.evaluate(doc, XPathConstants.NODESET); + stopTime = System.nanoTime (); + long firstRun = (stopTime - startTime); + System.err.println ("Evaluating XPath expression (1st run): " + firstRun / 1000000 + "ms"); + + startTime = System.nanoTime (); + for (int i = 1; i < 10; i++) { + + cexpr.evaluate(doc, XPathConstants.NODESET); + + }; + + stopTime = System.nanoTime (); + System.err.println ("Evaluating XPath expression (average 10 runs): " + (stopTime - startTime + firstRun) / (10 * 1000000) + "ms"); + + Transformer serializer = TransformerFactory.newInstance().newTransformer(); + serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + StreamResult o = new StreamResult(System.out); + startTime = System.nanoTime(); + System.out.println(""); + for(int i = 0; i < nodes.size(); i++){ + NodeInfo n = nodes.get(i); + switch (n.getNodeKind()) { + case Type.ATTRIBUTE: + System.out.print (n.getLocalPart() + "=" + n.getStringValue()); + break; + default: + serializer.transform(n, o); + break; + }; + System.out.println(); + }; + System.out.println(""); + stopTime = System.nanoTime(); + System.err.println ("Serializing document : " + (stopTime - startTime) / 1000000 + "ms"); + + } catch (XPathException e) { + System.out.println (e.getCause()); + } catch (Exception e) { + System.out.println(e); + }; + + } + +} diff --git a/tests/gen_ref.sh b/tests/gen_ref.sh new file mode 100755 index 0000000..b6d8c3b --- /dev/null +++ b/tests/gen_ref.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +javac XPathEval.java +for xml in *.xml +do + QUERIES="${xml}.queries" + RESULTS="${xml}.results" + cat "$QUERIES" | grep -v '^#' | while read qname query + do + printf "Generating reference for %s, %s ... " "${xml}" "${qname}" + java XPathEval "${xml}" "${query}" > "$RESULTS"/"${qname}_reference.xml" 2> "$RESULTS"/"${qname}_reference.log" + if [ $? -eq 0 ] + then + printf "ok\\n" + else + printf "ERROR\\n" + fi + done +done diff --git a/tests/gen_ref_saxon.sh b/tests/gen_ref_saxon.sh new file mode 100755 index 0000000..d695d06 --- /dev/null +++ b/tests/gen_ref_saxon.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +javac -cp .:/tmp/saxon9he.jar XPathEvalSaxon.java +for xml in *.xml +do + QUERIES="${xml}.queries" + RESULTS="${xml}.results" + cat "$QUERIES" | grep -v '^#' | while read qname query + do + printf "Generating reference for %s, %s ... " "${xml}" "${qname}" + java -cp .:/tmp/saxon9he.jar XPathEvalSaxon "${xml}" "${query}" > "$RESULTS"/"${qname}_saxon.xml" 2> "$RESULTS"/"${qname}_saxon.log" + if [ $? -eq 0 ] + then + printf "ok\\n" + else + printf "ERROR\\n" + fi + done +done -- 2.17.1