add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / PerfRunData.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.IOException;
22 import java.util.HashMap;
23 import java.util.Locale;
24
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;
40
41 /**
42  * Data maintained by a performance test run.
43  * <p>
44  * Data includes:
45  * <ul>
46  *  <li>Configuration.
47  *  <li>Directory, Writer, Reader.
48  *  <li>Docmaker and a few instances of QueryMaker.
49  *  <li>Analyzer.
50  *  <li>Statistics data which updated during the run.
51  * </ul>
52  * Config properties: work.dir=&lt;path to root of docs and index dirs| Default: work&gt;
53  * </ul>
54  */
55 public class PerfRunData {
56
57   private Points points;
58   
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;
66   
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;
70
71   private IndexReader indexReader;
72   private IndexSearcher indexSearcher;
73   private IndexWriter indexWriter;
74   private Config config;
75   private long startTimeMillis;
76   
77   // constructor
78   public PerfRunData (Config config) throws Exception {
79     this.config = config;
80     // analyzer (default is standard analyzer)
81     analyzer = NewAnalyzerTask.createAnalyzer(config.get("analyzer",
82         "org.apache.lucene.analysis.standard.StandardAnalyzer"));
83     // doc maker
84     docMaker = Class.forName(config.get("doc.maker",
85         "org.apache.lucene.benchmark.byTask.feeds.DocMaker")).asSubclass(DocMaker.class).newInstance();
86     docMaker.setConfig(config);
87     // query makers
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);
90
91     // index stuff
92     reinit(false);
93     
94     // statistic points
95     points = new Points(config);
96     
97     if (Boolean.valueOf(config.get("log.queries","false")).booleanValue()) {
98       System.out.println("------------> queries:");
99       System.out.println(getQueryMaker(new SearchTask(this)).printQueries());
100     }
101   }
102
103   // clean old stuff, reopen 
104   public void reinit(boolean eraseIndex) throws Exception {
105
106     // cleanup index
107     if (indexWriter!=null) {
108       indexWriter.close();
109       indexWriter = null;
110     }
111     if (indexReader!=null) {
112       indexReader.close();
113       indexReader = null;
114     }
115     if (directory!=null) {
116       directory.close();
117     }
118     
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);
125       }
126       indexDir.mkdirs();
127       directory = FSDirectory.open(indexDir);
128     } else {
129       directory = new RAMDirectory();
130     }
131
132     // inputs
133     resetInputs();
134     
135     // release unused stuff
136     System.runFinalization();
137     System.gc();
138
139     // Re-init clock
140     setStartTimeMillis();
141   }
142   
143   public long setStartTimeMillis() {
144     startTimeMillis = System.currentTimeMillis();
145     return startTimeMillis;
146   }
147
148   /**
149    * @return Start time in milliseconds
150    */
151   public long getStartTimeMillis() {
152     return startTimeMillis;
153   }
154
155   /**
156    * @return Returns the points.
157    */
158   public Points getPoints() {
159     return points;
160   }
161
162   /**
163    * @return Returns the directory.
164    */
165   public Directory getDirectory() {
166     return directory;
167   }
168
169   /**
170    * @param directory The directory to set.
171    */
172   public void setDirectory(Directory directory) {
173     this.directory = directory;
174   }
175
176   /**
177    * @return Returns the indexReader.  NOTE: this returns a
178    * reference.  You must call IndexReader.decRef() when
179    * you're done.
180    */
181   public synchronized IndexReader getIndexReader() {
182     if (indexReader != null) {
183       indexReader.incRef();
184     }
185     return indexReader;
186   }
187
188   /**
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.
192    */
193   public synchronized IndexSearcher getIndexSearcher() {
194     if (indexReader != null) {
195       indexReader.incRef();
196     }
197     return indexSearcher;
198   }
199
200   /**
201    * @param indexReader The indexReader to set.
202    */
203   public synchronized void setIndexReader(IndexReader indexReader) throws IOException {
204     if (this.indexReader != null) {
205       // Release current IR
206       this.indexReader.decRef();
207     }
208     this.indexReader = indexReader;
209     if (indexReader != null) {
210       // Hold reference to new IR
211       indexReader.incRef();
212       indexSearcher = new IndexSearcher(indexReader);
213     } else {
214       indexSearcher = null;
215     }
216   }
217
218   /**
219    * @return Returns the indexWriter.
220    */
221   public IndexWriter getIndexWriter() {
222     return indexWriter;
223   }
224
225   /**
226    * @param indexWriter The indexWriter to set.
227    */
228   public void setIndexWriter(IndexWriter indexWriter) {
229     this.indexWriter = indexWriter;
230   }
231
232   /**
233    * @return Returns the anlyzer.
234    */
235   public Analyzer getAnalyzer() {
236     return analyzer;
237   }
238
239
240   public void setAnalyzer(Analyzer analyzer) {
241     this.analyzer = analyzer;
242   }
243
244   /** Returns the docMaker. */
245   public DocMaker getDocMaker() {
246     return docMaker;
247   }
248
249   /**
250    * @return the locale
251    */
252   public Locale getLocale() {
253     return locale;
254   }
255
256   /**
257    * @param locale the locale to set
258    */
259   public void setLocale(Locale locale) {
260     this.locale = locale;
261   }
262
263   /**
264    * @return Returns the config.
265    */
266   public Config getConfig() {
267     return config;
268   }
269
270   public void resetInputs() throws IOException {
271     docMaker.resetInputs();
272     for (final QueryMaker queryMaker : readTaskQueryMaker.values()) {
273       queryMaker.resetInputs();
274     }
275   }
276
277   /**
278    * @return Returns the queryMaker by read task type (class)
279    */
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);
285     if (qm == null) {
286       try {
287         qm = qmkrClass.newInstance();
288         qm.setConfig(config);
289       } catch (Exception e) {
290         throw new RuntimeException(e);
291       }
292       readTaskQueryMaker.put(readTaskClass,qm);
293     }
294     return qm;
295   }
296
297 }