1 package org.apache.lucene.benchmark.byTask;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 import java.io.FileReader;
22 import java.io.Reader;
24 import org.apache.lucene.benchmark.byTask.utils.Algorithm;
25 import org.apache.lucene.benchmark.byTask.utils.Config;
29 * Run the benchmark algorithm.
30 * <p>Usage: java Benchmark algorithm-file
32 * <li>Read algorithm.</li>
33 * <li> Run the algorithm.</li>
35 * Things to be added/fixed in "Benchmarking by tasks":
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>
44 public class Benchmark {
46 private PerfRunData runData;
47 private Algorithm algorithm;
48 private boolean executed;
50 public Benchmark (Reader algReader) throws Exception {
53 runData = new PerfRunData(new Config(algReader));
54 } catch (Exception e) {
56 throw new Exception("Error: cannot init PerfRunData!",e);
61 algorithm = new Algorithm(runData);
62 } catch (Exception e) {
63 throw new Exception("Error: cannot understand algorithm!",e);
68 * Execute this benchmark
70 public synchronized void execute() throws Exception {
72 throw new IllegalStateException("Benchmark was already executed");
75 runData.setStartTimeMillis();
80 * Run the benchmark algorithm.
81 * @param args benchmark config and algorithm files
83 public static void main(String[] args) {
88 * Utility: execute benchmark from command line
89 * @param args single argument is expected: algorithm-file
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>");
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());
105 System.out.println("Running algorithm from: "+algFile.getAbsolutePath());
107 Benchmark benchmark = null;
109 benchmark = new Benchmark(new FileReader(algFile));
110 } catch (Exception e) {
115 System.out.println("------------> algorithm:");
116 System.out.println(benchmark.getAlgorithm().toString());
121 } catch (Exception e) {
122 System.err.println("Error: cannot execute the algorithm! "+e.getMessage());
126 System.out.println("####################");
127 System.out.println("### D O N E !!! ###");
128 System.out.println("####################");
132 * @return Returns the algorithm.
134 public Algorithm getAlgorithm() {
139 * @return Returns the runData.
141 public PerfRunData getRunData() {