expected = new Object[] { "" };
}
else { // it is a document containing zero or more XPath queries
Document queries = readDocument(args[q]); // e.g. "xpath/queries1.xml"
Nodes paths = XQueryUtil.xquery(queries, "/paths/document/path");
contexts = new Node[paths.size()];
selects = new String[paths.size()];
types = new int[paths.size()];
expected = new Object[paths.size()];
// precompute all the info necessary to run the bench
for (int i=0; i < paths.size(); i++) {
Element path = (Element) paths.get(i);
Attribute ctxAttr = path.getAttribute("context");
contexts[i] = ctxAttr == null ? doc : XQueryUtil.xquery(doc, ctxAttr.getValue()).get(0);
selects[i] = path.getAttribute("select").getValue();
types[i] = 0;
if (path.getAttribute("type") != null) {
String[] flavours = {"count", "string", "double", "boolean"};
types[i] = java.util.Arrays.asList(flavours).indexOf(path.getAttribute("type").getValue());
}
expected[i] = path.getValue();
}
}
// for each query
for (int i=0; i < selects.length; i++) {
int actualRuns = runs;
if (selects[i].equals("//*[contains(string(.),'Capulet')]")) {
// actualRuns = Math.min(runs, 10); // this one would take too long
continue; // ignore
}
System.out.print("query = " + selects[i] + " ");
if (actualRuns == 1) System.out.println(
"\nexplain = \n" + new XQuery(selects[i], null).explain());
XQuery xquery = new XQuery(selects[i], null);
// now execute the query N times and measure execution time
long start = System.currentTimeMillis();
IS_BENCHMARK = true;
Nodes results = run(contexts[i], selects[i], actualRuns, mode, types[i], xquery);
IS_BENCHMARK = false;
long end = System.currentTimeMillis();
if (check && results != null) { // found the right results?
for (int j=0; j < results.size(); j++) {
System.out.println("node " + j + ": " + results.get(j).toXML());
}
Node first = results.size() == 0 ? null : results.get(0);
Object actual = null;
switch (types[i]) {
case 0 : actual = String.valueOf(results.size()); break;
case 1 : actual = first == null ? "" : first.getValue(); break;
case 2 : actual = first == null ? "0.0" : new Double(first.getValue()).toString(); break;
case 3 : actual = first == null ? "false" : first.getValue().equals("true") ? "true" : "false"; break;
default: throw new IllegalStateException();
}