001: package org.apache.lucene.benchmark.byTask;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.File;
021: import java.io.FileReader;
022: import java.io.Reader;
023:
024: import org.apache.lucene.benchmark.byTask.utils.Algorithm;
025: import org.apache.lucene.benchmark.byTask.utils.Config;
026:
027: /**
028: * Run the benchmark algorithm.
029: * <p>Usage: java Benchmark algorithm-file
030: * <ol>
031: * <li>Read algorithm.</li>
032: * <li> Run the algorithm.</li>
033: * </ol>
034: * Things to be added/fixed in "Benchmarking by tasks":
035: * <ol>
036: * <li>TODO - report into Excel and/or graphed view.</li>
037: * <li>TODO - perf comparison between Lucene releases over the years.</li>
038: * <li>TODO - perf report adequate to include in Lucene nightly build site? (so we can easily track performance changes.)</li>
039: * <li>TODO - add overall time control for repeated execution (vs. current by-count only).</li>
040: * <li>TODO - query maker that is based on index statistics.</li>
041: * </ol>
042: */
043: public class Benchmark {
044:
045: private PerfRunData runData;
046: private Algorithm algorithm;
047: private boolean executed;
048:
049: public Benchmark(Reader algReader) throws Exception {
050: // prepare run data
051: try {
052: runData = new PerfRunData(new Config(algReader));
053: } catch (Exception e) {
054: e.printStackTrace();
055: throw new Exception("Error: cannot init PerfRunData!", e);
056: }
057:
058: // parse algorithm
059: try {
060: algorithm = new Algorithm(runData);
061: } catch (Exception e) {
062: throw new Exception("Error: cannot understand algorithm!",
063: e);
064: }
065: }
066:
067: public synchronized void execute() throws Exception {
068: if (executed) {
069: throw new IllegalStateException(
070: "Benchmark was already executed");
071: }
072: executed = true;
073: runData.setStartTimeMillis();
074: algorithm.execute();
075: }
076:
077: /**
078: * Run the benchmark algorithm.
079: * @param args benchmark config and algorithm files
080: */
081: public static void main(String[] args) {
082: // verify command line args
083: if (args.length < 1) {
084: System.err
085: .println("Usage: java Benchmark <algorithm file>");
086: System.exit(1);
087: }
088:
089: // verify input files
090: File algFile = new File(args[0]);
091: if (!algFile.exists() || !algFile.isFile()
092: || !algFile.canRead()) {
093: System.err.println("cannot find/read algorithm file: "
094: + algFile.getAbsolutePath());
095: System.exit(1);
096: }
097:
098: System.out.println("Running algorithm from: "
099: + algFile.getAbsolutePath());
100:
101: Benchmark benchmark = null;
102: try {
103: benchmark = new Benchmark(new FileReader(algFile));
104: } catch (Exception e) {
105: e.printStackTrace();
106: System.exit(1);
107: }
108:
109: System.out.println("------------> algorithm:");
110: System.out.println(benchmark.getAlgorithm().toString());
111:
112: // execute
113: try {
114: benchmark.execute();
115: } catch (Exception e) {
116: System.err.println("Error: cannot execute the algorithm! "
117: + e.getMessage());
118: e.printStackTrace();
119: }
120:
121: System.out.println("####################");
122: System.out.println("### D O N E !!! ###");
123: System.out.println("####################");
124:
125: }
126:
127: /**
128: * @return Returns the algorithm.
129: */
130: public Algorithm getAlgorithm() {
131: return algorithm;
132: }
133:
134: /**
135: * @return Returns the runData.
136: */
137: public PerfRunData getRunData() {
138: return runData;
139: }
140:
141: }
|