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.IOException;
22 import java.util.HashMap;
23 import java.util.Locale;
25 import org.apache.lucene.analysis.Analyzer;
26 import org.apache.lucene.benchmark.byTask.feeds.DocMaker;
27 import org.apache.lucene.benchmark.byTask.feeds.QueryMaker;
28 import org.apache.lucene.benchmark.byTask.stats.Points;
29 import org.apache.lucene.benchmark.byTask.tasks.ReadTask;
30 import org.apache.lucene.benchmark.byTask.tasks.SearchTask;
31 import org.apache.lucene.benchmark.byTask.utils.Config;
32 import org.apache.lucene.benchmark.byTask.utils.FileUtils;
33 import org.apache.lucene.benchmark.byTask.tasks.NewAnalyzerTask;
34 import org.apache.lucene.index.IndexReader;
35 import org.apache.lucene.index.IndexWriter;
36 import org.apache.lucene.search.IndexSearcher;
37 import org.apache.lucene.store.Directory;
38 import org.apache.lucene.store.FSDirectory;
39 import org.apache.lucene.store.RAMDirectory;
42 * Data maintained by a performance test run.
47 * <li>Directory, Writer, Reader.
48 * <li>Docmaker and a few instances of QueryMaker.
50 * <li>Statistics data which updated during the run.
52 * Config properties: work.dir=<path to root of docs and index dirs| Default: work>
55 public class PerfRunData {
57 private Points points;
59 // objects used during performance test run
60 // directory, analyzer, docMaker - created at startup.
61 // reader, writer, searcher - maintained by basic tasks.
62 private Directory directory;
63 private Analyzer analyzer;
64 private DocMaker docMaker;
65 private Locale locale;
67 // we use separate (identical) instances for each "read" task type, so each can iterate the quries separately.
68 private HashMap<Class<? extends ReadTask>,QueryMaker> readTaskQueryMaker;
69 private Class<? extends QueryMaker> qmkrClass;
71 private IndexReader indexReader;
72 private IndexSearcher indexSearcher;
73 private IndexWriter indexWriter;
74 private Config config;
75 private long startTimeMillis;
78 public PerfRunData (Config config) throws Exception {
80 // analyzer (default is standard analyzer)
81 analyzer = NewAnalyzerTask.createAnalyzer(config.get("analyzer",
82 "org.apache.lucene.analysis.standard.StandardAnalyzer"));
84 docMaker = Class.forName(config.get("doc.maker",
85 "org.apache.lucene.benchmark.byTask.feeds.DocMaker")).asSubclass(DocMaker.class).newInstance();
86 docMaker.setConfig(config);
88 readTaskQueryMaker = new HashMap<Class<? extends ReadTask>,QueryMaker>();
89 qmkrClass = Class.forName(config.get("query.maker","org.apache.lucene.benchmark.byTask.feeds.SimpleQueryMaker")).asSubclass(QueryMaker.class);
95 points = new Points(config);
97 if (Boolean.valueOf(config.get("log.queries","false")).booleanValue()) {
98 System.out.println("------------> queries:");
99 System.out.println(getQueryMaker(new SearchTask(this)).printQueries());
103 // clean old stuff, reopen
104 public void reinit(boolean eraseIndex) throws Exception {
107 if (indexWriter!=null) {
111 if (indexReader!=null) {
115 if (directory!=null) {
119 // directory (default is ram-dir).
120 if ("FSDirectory".equals(config.get("directory","RAMDirectory"))) {
121 File workDir = new File(config.get("work.dir","work"));
122 File indexDir = new File(workDir,"index");
123 if (eraseIndex && indexDir.exists()) {
124 FileUtils.fullyDelete(indexDir);
127 directory = FSDirectory.open(indexDir);
129 directory = new RAMDirectory();
135 // release unused stuff
136 System.runFinalization();
140 setStartTimeMillis();
143 public long setStartTimeMillis() {
144 startTimeMillis = System.currentTimeMillis();
145 return startTimeMillis;
149 * @return Start time in milliseconds
151 public long getStartTimeMillis() {
152 return startTimeMillis;
156 * @return Returns the points.
158 public Points getPoints() {
163 * @return Returns the directory.
165 public Directory getDirectory() {
170 * @param directory The directory to set.
172 public void setDirectory(Directory directory) {
173 this.directory = directory;
177 * @return Returns the indexReader. NOTE: this returns a
178 * reference. You must call IndexReader.decRef() when
181 public synchronized IndexReader getIndexReader() {
182 if (indexReader != null) {
183 indexReader.incRef();
189 * @return Returns the indexSearcher. NOTE: this returns
190 * a reference to the underlying IndexReader. You must
191 * call IndexReader.decRef() when you're done.
193 public synchronized IndexSearcher getIndexSearcher() {
194 if (indexReader != null) {
195 indexReader.incRef();
197 return indexSearcher;
201 * @param indexReader The indexReader to set.
203 public synchronized void setIndexReader(IndexReader indexReader) throws IOException {
204 if (this.indexReader != null) {
205 // Release current IR
206 this.indexReader.decRef();
208 this.indexReader = indexReader;
209 if (indexReader != null) {
210 // Hold reference to new IR
211 indexReader.incRef();
212 indexSearcher = new IndexSearcher(indexReader);
214 indexSearcher = null;
219 * @return Returns the indexWriter.
221 public IndexWriter getIndexWriter() {
226 * @param indexWriter The indexWriter to set.
228 public void setIndexWriter(IndexWriter indexWriter) {
229 this.indexWriter = indexWriter;
233 * @return Returns the anlyzer.
235 public Analyzer getAnalyzer() {
240 public void setAnalyzer(Analyzer analyzer) {
241 this.analyzer = analyzer;
244 /** Returns the docMaker. */
245 public DocMaker getDocMaker() {
252 public Locale getLocale() {
257 * @param locale the locale to set
259 public void setLocale(Locale locale) {
260 this.locale = locale;
264 * @return Returns the config.
266 public Config getConfig() {
270 public void resetInputs() throws IOException {
271 docMaker.resetInputs();
272 for (final QueryMaker queryMaker : readTaskQueryMaker.values()) {
273 queryMaker.resetInputs();
278 * @return Returns the queryMaker by read task type (class)
280 synchronized public QueryMaker getQueryMaker(ReadTask readTask) {
281 // mapping the query maker by task class allows extending/adding new search/read tasks
282 // without needing to modify this class.
283 Class<? extends ReadTask> readTaskClass = readTask.getClass();
284 QueryMaker qm = readTaskQueryMaker.get(readTaskClass);
287 qm = qmkrClass.newInstance();
288 qm.setConfig(config);
289 } catch (Exception e) {
290 throw new RuntimeException(e);
292 readTaskQueryMaker.put(readTaskClass,qm);