/* * (c) Copyright 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP * [See end of file] */ package arqo; import java.util.*; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import com.hp.hpl.jena.query.Query; import com.hp.hpl.jena.query.QueryExecution; import com.hp.hpl.jena.query.QueryExecutionFactory; import com.hp.hpl.jena.query.ResultSet; import com.hp.hpl.jena.query.ResultSetFormatter; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.reasoner.ReasonerRegistry; import com.hp.hpl.jena.sparql.engine.optimizer.Optimizer; import com.hp.hpl.jena.util.FileManager; import com.hp.hpl.jena.query.QueryFactory; /** * Measure the performance of queries * * @author Markus Stocker * @version $Id$ */ public class performance { private static String inGraphFileName = null ; private static String inSchemaFileName = null ; private static String inQueryFileName = null ; private static String inQueryString = null ; private static String inIndexFileName = null ; private static String optimizer = "on" ; private static String reasoner = null ; private static String explain = "yes" ; private static int tests = 10 ; private static String heuristic = null ; private static Model model ; /** * Main program * * @param args */ public static void main(String[] args) { int resultSetSize = 0 ; List durations = new ArrayList() ; // List try { readCmdParams(args) ; System.out.println("================================= TEST RUN =================================") ; System.out.println("Data: " + inGraphFileName) ; System.out.println("Schema: " + inSchemaFileName) ; System.out.println("Query: " + inQueryFileName) ; System.out.println("Index: " + inIndexFileName) ; System.out.println("Tests: " + tests) ; System.out.println("Reasoner: " + reasoner) ; System.out.println("Optimizer: " + optimizer) ; System.out.println("Explain: " + explain) ; System.out.println("Heuristic: " + heuristic) ; System.out.println("============================================================================") ; readInQueryFileName() ; loadModel() ; if (optimizer.equals("on")) Optimizer.enable(model, FileManager.get().loadModel(inIndexFileName)) ; for (int i = 0; i <= tests; i++) { long start = System.nanoTime() ; Query query = QueryFactory.create(inQueryString) ; QueryExecution exec = QueryExecutionFactory.create(query, model) ; if (optimizer.equals("on") && explain.equals("yes")) Optimizer.explain(model, query) ; else { try { ResultSet rs = exec.execSelect() ; ResultSetFormatter.consume(rs) ; //ResultSetFormatter.out(rs) ; resultSetSize = rs.getRowNumber() ; } finally { exec.close() ; } long end = System.nanoTime() ; long duration = end - start ; System.out.println("Elapsed time for run " + i + " (ns): " + duration) ; // Skip the first if (i > 0) durations.add(new Long(duration)) ; } } if (explain.equals("no")) { System.out.println("Result set size: " + resultSetSize) ; System.out.println("Elapsed time (ms): " + getAvgElapsedTimeInMillis(durations)) ; } } catch (Exception e) { e.printStackTrace() ; } } private static double getAvgElapsedTimeInMillis(List durations) { double ns = 0d ; for (Iterator iter = durations.iterator(); iter.hasNext(); ) { double duration = ((Long)iter.next()).doubleValue() ; ns += duration ; } double ms = ns / 1000000 ; return ms / durations.size() ; } // Load the model with RDFS inferencing if schema is provided private static void loadModel() { System.out.println("Loading model ...") ; if (inSchemaFileName != null) { if (reasoner.equals("Transitive")) model = ModelFactory.createInfModel(ReasonerRegistry.getTransitiveReasoner(), FileManager.get().loadModel(inSchemaFileName), FileManager.get().loadModel(inGraphFileName)) ; else model = ModelFactory.createInfModel(ReasonerRegistry.getRDFSReasoner(), FileManager.get().loadModel(inSchemaFileName), FileManager.get().loadModel(inGraphFileName)) ; } else model = FileManager.get().loadModel(inGraphFileName) ; } // Read the query file into a string private static void readInQueryFileName() { String line = null ; BufferedReader br = null ; try { br = new BufferedReader(new FileReader(inQueryFileName)) ; inQueryString = new String() ; while ((line = br.readLine()) != null) { inQueryString += line + "\n" ; } br.close() ; } catch (FileNotFoundException e) { e.printStackTrace() ; } catch (IOException e) { e.printStackTrace() ; } } // Read the command line params private static void readCmdParams(String[] args) throws Exception { for (int i = 0; i < args.length; i++) { if (args[i].equals("--graph")) inGraphFileName = args[i+1] ; else if (args[i].equals("--schema")) inSchemaFileName = args[i+1] ; else if (args[i].equals("--query")) inQueryFileName = args[i+1] ; else if (args[i].equals("--index")) inIndexFileName = args[i+1] ; else if (args[i].equals("--tests")) tests = new Integer(args[i+1]).intValue() ; else if (args[i].equals("--reasoner")) reasoner = args[i+1] ; else if (args[i].equals("--optimizer")) optimizer = args[i+1] ; else if (args[i].equals("--explain")) explain = args[i+1] ; else if (args[i].equals("--heuristic")) heuristic = args[i+1] ; else if (args[i].equals("--help")) usage() ; } if (inGraphFileName == null) usage() ; if (inQueryFileName == null) usage() ; } // Print the usage of the main program private static void usage() { String usage = "arqo.performance [options]\n" ; usage += "--graph\n" ; usage += "--schema (optional)\n" ; usage += "--query\n" ; usage += "--index\n" ; usage += "--tests\n" ; usage += "--optimizer [on (default) | off]\n" ; usage += "--explain [yes (default - no query execution) | no (execute the query)]\n" ; usage += "--reasoner [RDFS | Transitive]\n" ; usage += "--heuristic [one of HeuristicsRegistry.]\n" ; System.out.println(usage) ; System.exit(0) ; } } /* * (c) Copyright 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */