aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/semantics/benchmark/RuleBaseBenchmark.java
blob: 75b6e831983844561f6ed15d1365500c22a220fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright Yahoo. 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.language.simple.SimpleLinguistics;
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(new SimpleLinguistics()).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();
    }


}