aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/semantics/benchmark/RuleBaseBenchmark.java
diff options
context:
space:
mode:
Diffstat (limited to 'container-search/src/main/java/com/yahoo/prelude/semantics/benchmark/RuleBaseBenchmark.java')
-rw-r--r--container-search/src/main/java/com/yahoo/prelude/semantics/benchmark/RuleBaseBenchmark.java86
1 files changed, 86 insertions, 0 deletions
diff --git a/container-search/src/main/java/com/yahoo/prelude/semantics/benchmark/RuleBaseBenchmark.java b/container-search/src/main/java/com/yahoo/prelude/semantics/benchmark/RuleBaseBenchmark.java
new file mode 100644
index 00000000000..b04e693089a
--- /dev/null
+++ b/container-search/src/main/java/com/yahoo/prelude/semantics/benchmark/RuleBaseBenchmark.java
@@ -0,0 +1,86 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.prelude.semantics.benchmark;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.Iterator;
+
+import com.yahoo.search.Query;
+import com.yahoo.prelude.semantics.RuleBase;
+import com.yahoo.prelude.semantics.RuleImporter;
+import com.yahoo.prelude.semantics.parser.ParseException;
+
+public class RuleBaseBenchmark {
+
+ public void benchmark(String ruleBaseFile, String queryFile, int iterations)
+ throws IOException, ParseException {
+
+ String fsaFile = null;
+ if(ruleBaseFile.endsWith(".sr")){
+ fsaFile = ruleBaseFile.substring(0,ruleBaseFile.length()-3) + ".fsa";
+ File fsa = new File(fsaFile);
+ if(!fsa.exists()){
+ fsaFile = null;
+ }
+ }
+ RuleBase ruleBase = new RuleImporter().importFile(ruleBaseFile,fsaFile);
+ ArrayList<String> queries = new ArrayList<>();
+ BufferedReader reader = new BufferedReader(new FileReader(queryFile));
+ String line;
+ while((line=reader.readLine())!=null){
+ queries.add(line);
+ }
+ Date start = new Date();
+ for (int i=0;i<iterations;i++){
+ for (Iterator<String> iter = queries.iterator(); iter.hasNext(); ){
+ String queryString = iter.next();
+ Query query = new Query("?query="+queryString);
+ ruleBase.analyze(query,0);
+ }
+ }
+ Date end = new Date();
+ long elapsed = end.getTime()-start.getTime();
+ System.out.print("BENCHMARK: rulebase=" + ruleBaseFile +
+ "\n fsa=" + fsaFile +
+ "\n queries=" + queryFile +
+ "\n iterations=" + iterations +
+ "\n elapsed=" + elapsed + "ms\n");
+ }
+
+
+ public static void main(String[] args) {
+ if(args.length<3){
+ System.out.println("USAGE: RuleBaseBenchmark ruleBaseFile queryFile iterations");
+ System.exit(1);
+ }
+
+ try {
+ new RuleBaseBenchmark().benchmark(args[0],args[1],Integer.parseInt(args[2]));
+ }
+ catch (Exception e) {
+ System.out.println("ERROR: " + collectMessage(e));
+ //e.printStackTrace();
+ System.exit(1);
+ }
+ }
+
+ private static String collectMessage(Throwable e) {
+ if (e.getCause()==null)
+ return messageOrName(e);
+ else
+ return messageOrName(e) + ": " + collectMessage(e.getCause());
+ }
+
+ private static String messageOrName(Throwable e) {
+ if (e.getMessage()!=null)
+ return e.getMessage();
+ else
+ return e.getClass().getName();
+ }
+
+
+}