Add Saxon benchmark.
authorKim Nguyễn <kn@lri.fr>
Wed, 12 Apr 2017 10:31:12 +0000 (12:31 +0200)
committerKim Nguyễn <kn@lri.fr>
Wed, 12 Apr 2017 10:31:12 +0000 (12:31 +0200)
tests/XPathEval.java [new file with mode: 0644]
tests/XPathEvalSaxon.java [new file with mode: 0644]
tests/XPathExample.java [new file with mode: 0644]
tests/gen_ref.sh [new file with mode: 0755]
tests/gen_ref_saxon.sh [new file with mode: 0755]

diff --git a/tests/XPathEval.java b/tests/XPathEval.java
new file mode 100644 (file)
index 0000000..8ce8db1
--- /dev/null
@@ -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("<xml_result num=\"1\">");
+      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("</xml_result>");
+      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 (file)
index 0000000..0f8fd2e
--- /dev/null
@@ -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<NodeInfo> nodes = (ArrayList<NodeInfo>) 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("<xml_result num=\"1\">");
+      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("</xml_result>");
+      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 (file)
index 0000000..326cb03
--- /dev/null
@@ -0,0 +1,105 @@
+import org.xml.sax.*;\r
+import org.w3c.dom.*;\r
+import javax.xml.transform.*;\r
+import javax.xml.transform.dom.*;\r
+import javax.xml.transform.stream.*;\r
+import java.util.ArrayList;\r
+import net.sf.saxon.Configuration;\r
+import net.sf.saxon.lib.NamespaceConstant;\r
+import net.sf.saxon.om.DocumentInfo;\r
+import net.sf.saxon.om.NodeInfo;\r
+import net.sf.saxon.xpath.XPathFactoryImpl;\r
+import org.xml.sax.InputSource;\r
+\r
+import javax.xml.namespace.QName;\r
+import javax.xml.transform.sax.SAXSource;\r
+import javax.xml.xpath.*;\r
+import java.io.BufferedReader;\r
+import java.io.File;\r
+import java.io.InputStreamReader;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+import net.sf.saxon.type.Type;\r
+\r
+\r
+public class XPathExample {\r
+\r
+\r
+    public static void main (String args[])   {\r
+\r
+        if (args.length != 2) {\r
+            System.err.println("Usage: java XPathEvalSaxon input-file '/xpath/expression'");\r
+            System.exit(1);\r
+        }\r
+\r
+       try {\r
+\r
+         System.setProperty("javax.xml.xpath.XPathFactory:"+NamespaceConstant.OBJECT_MODEL_SAXON,\r
+                          "net.sf.saxon.xpath.XPathFactoryImpl");\r
+\r
+         XPathFactory xpf = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON);\r
+\r
+         XPath xpe = xpf.newXPath();\r
+\r
+         System.err.println("Loaded XPath Provider " + xpe.getClass().getName());\r
+\r
+         long startTime = System.nanoTime ();\r
+         InputSource is = new InputSource(args[0]);\r
+         SAXSource ss = new SAXSource(is);\r
+         Configuration config = ((XPathFactoryImpl) xpf).getConfiguration();\r
+         NodeInfo doc = config.buildDocumentTree(ss).getRootNode();\r
+         long stopTime = System.nanoTime ();\r
+         System.err.println ("Loading document : " + (stopTime - startTime) / 1000000 + "ms");\r
+\r
+         startTime = System.nanoTime ();\r
+         XPathExpression cexpr = xpe.compile(args[1]);\r
+         stopTime = System.nanoTime ();\r
+         System.err.println ("Compiling XPath expression : " + (stopTime - startTime) / 1000000 + "ms");\r
+\r
+         startTime = System.nanoTime ();\r
+         @SuppressWarnings("unchecked")\r
+           ArrayList<NodeInfo> nodes = (ArrayList<NodeInfo>) cexpr.evaluate(doc, XPathConstants.NODESET);\r
+         stopTime = System.nanoTime ();\r
+         long firstRun = (stopTime - startTime);\r
+         System.err.println ("Evaluating XPath expression (1st run): " + firstRun / 1000000 + "ms");\r
+\r
+         startTime = System.nanoTime ();\r
+         for (int i = 1; i < 10; i++) {\r
+\r
+           cexpr.evaluate(doc, XPathConstants.NODESET);\r
+\r
+         };\r
+\r
+         stopTime = System.nanoTime ();\r
+         System.err.println ("Evaluating XPath expression (average 10 runs): " + (stopTime - startTime + firstRun) / (10 * 1000000) + "ms");\r
+\r
+         Transformer serializer = TransformerFactory.newInstance().newTransformer();\r
+         serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");\r
+         StreamResult o = new StreamResult(System.out);\r
+         startTime = System.nanoTime();\r
+         System.out.println("<xml_result num=\"1\">");\r
+         for(int i = 0; i < nodes.size(); i++){\r
+           NodeInfo n = nodes.get(i);\r
+           switch (n.getNodeKind()) {\r
+           case Type.ATTRIBUTE:\r
+             System.out.print (n.getLocalPart() + "=" + n.getStringValue());\r
+             break;\r
+           default:\r
+             serializer.transform(n, o);\r
+             break;\r
+           };\r
+           System.out.println();\r
+         };\r
+         System.out.println("</xml_result>");\r
+         stopTime = System.nanoTime();\r
+         System.err.println ("Serializing document : " + (stopTime - startTime) / 1000000 + "ms");\r
+\r
+       } catch (XPathException e) {\r
+         System.out.println (e.getCause());\r
+       } catch (Exception e) {\r
+         System.out.println(e);\r
+       };\r
+\r
+    }\r
+\r
+}\r
diff --git a/tests/gen_ref.sh b/tests/gen_ref.sh
new file mode 100755 (executable)
index 0000000..b6d8c3b
--- /dev/null
@@ -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 (executable)
index 0000000..d695d06
--- /dev/null
@@ -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