add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / Benchmark.java
1 package org.apache.lucene.benchmark.byTask;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.File;
21 import java.io.FileReader;
22 import java.io.Reader;
23
24 import org.apache.lucene.benchmark.byTask.utils.Algorithm;
25 import org.apache.lucene.benchmark.byTask.utils.Config;
26
27
28 /**
29  * Run the benchmark algorithm.
30  * <p>Usage: java Benchmark  algorithm-file
31  * <ol>
32  * <li>Read algorithm.</li>
33  * <li> Run the algorithm.</li>
34  * </ol>
35  * Things to be added/fixed in "Benchmarking by tasks":
36  * <ol>
37  * <li>TODO - report into Excel and/or graphed view.</li>
38  * <li>TODO - perf comparison between Lucene releases over the years.</li>
39  * <li>TODO - perf report adequate to include in Lucene nightly build site? (so we can easily track performance changes.)</li>
40  * <li>TODO - add overall time control for repeated execution (vs. current by-count only).</li>
41  * <li>TODO - query maker that is based on index statistics.</li>
42  * </ol>
43  */
44 public class Benchmark {
45
46   private PerfRunData runData;
47   private Algorithm algorithm;
48   private boolean executed;
49   
50   public Benchmark (Reader algReader) throws Exception {
51     // prepare run data
52     try {
53       runData = new PerfRunData(new Config(algReader));
54     } catch (Exception e) {
55       e.printStackTrace();
56       throw new Exception("Error: cannot init PerfRunData!",e);
57     }
58     
59     // parse algorithm
60     try {
61       algorithm = new Algorithm(runData);
62     } catch (Exception e) {
63       throw new Exception("Error: cannot understand algorithm!",e);
64     }
65   }
66   
67   /**
68    * Execute this benchmark 
69    */
70   public synchronized void  execute() throws Exception {
71     if (executed) {
72       throw new IllegalStateException("Benchmark was already executed");
73     }
74     executed = true;
75     runData.setStartTimeMillis();
76     algorithm.execute();
77   }
78   
79   /**
80    * Run the benchmark algorithm.
81    * @param args benchmark config and algorithm files
82    */
83   public static void main(String[] args) {
84     exec(args);
85   }
86
87   /**
88    * Utility: execute benchmark from command line
89    * @param args single argument is expected: algorithm-file
90    */
91   public static void exec(String[] args) {
92     // verify command line args
93     if (args.length < 1) {
94       System.err.println("Usage: java Benchmark <algorithm file>");
95       System.exit(1);
96     }
97     
98     // verify input files 
99     File algFile = new File(args[0]);
100     if (!algFile.exists() || !algFile.isFile() || !algFile.canRead()) {
101       System.err.println("cannot find/read algorithm file: "+algFile.getAbsolutePath()); 
102       System.exit(1);
103     }
104     
105     System.out.println("Running algorithm from: "+algFile.getAbsolutePath());
106     
107     Benchmark benchmark = null;
108     try {
109       benchmark = new Benchmark(new FileReader(algFile));
110     } catch (Exception e) {
111       e.printStackTrace();
112       System.exit(1);
113     }
114
115     System.out.println("------------> algorithm:");
116     System.out.println(benchmark.getAlgorithm().toString());
117
118     // execute
119     try {
120       benchmark.execute();
121     } catch (Exception e) {
122       System.err.println("Error: cannot execute the algorithm! "+e.getMessage());
123       e.printStackTrace();
124     }
125
126     System.out.println("####################");
127     System.out.println("###  D O N E !!! ###");
128     System.out.println("####################");
129   }
130
131   /**
132    * @return Returns the algorithm.
133    */
134   public Algorithm getAlgorithm() {
135     return algorithm;
136   }
137
138   /**
139    * @return Returns the runData.
140    */
141   public PerfRunData getRunData() {
142     return runData;
143   }
144
145 }