pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / tasks / NearRealtimeReaderTask.java
1 package org.apache.lucene.benchmark.byTask.tasks;
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 org.apache.lucene.benchmark.byTask.PerfRunData;
21 import org.apache.lucene.index.IndexReader;
22 import org.apache.lucene.index.IndexWriter;
23 import org.apache.lucene.util.ArrayUtil;
24
25 /**
26  * Spawns a BG thread that periodically (defaults to 3.0
27  * seconds, but accepts param in seconds) wakes up and asks
28  * IndexWriter for a near real-time reader.  Then runs a
29  * single query (body: 1) sorted by docdate, and prints
30  * time to reopen and time to run the search.
31  *
32  * @lucene.experimental It's also not generally usable, eg
33  * you cannot change which query is executed.
34  */
35 public class NearRealtimeReaderTask extends PerfTask {
36
37   long pauseMSec = 3000L;
38
39   int reopenCount;
40   int[] reopenTimes = new int[1];
41
42   public NearRealtimeReaderTask(PerfRunData runData) {
43     super(runData);
44   }
45
46   @Override
47   public int doLogic() throws Exception {
48
49     final PerfRunData runData = getRunData();
50
51     // Get initial reader
52     IndexWriter w = runData.getIndexWriter();
53     if (w == null) {
54       throw new RuntimeException("please open the writer before invoking NearRealtimeReader");
55     }
56
57     if (runData.getIndexReader() != null) {
58       throw new RuntimeException("please close the existing reader before invoking NearRealtimeReader");
59     }
60     
61     long t = System.currentTimeMillis();
62     IndexReader r = IndexReader.open(w, true);
63     runData.setIndexReader(r);
64     // Transfer our reference to runData
65     r.decRef();
66
67     // TODO: gather basic metrics for reporting -- eg mean,
68     // stddev, min/max reopen latencies
69
70     // Parent sequence sets stopNow
71     reopenCount = 0;
72     while(!stopNow) {
73       long waitForMsec = (pauseMSec - (System.currentTimeMillis() - t));
74       if (waitForMsec > 0) {
75         Thread.sleep(waitForMsec);
76         //System.out.println("NRT wait: " + waitForMsec + " msec");
77       }
78
79       t = System.currentTimeMillis();
80       final IndexReader newReader = r.reopen();
81       if (r != newReader) {
82         final int delay = (int) (System.currentTimeMillis()-t);
83         if (reopenTimes.length == reopenCount) {
84           reopenTimes = ArrayUtil.grow(reopenTimes, 1+reopenCount);
85         }
86         reopenTimes[reopenCount++] = delay;
87         // TODO: somehow we need to enable warming, here
88         runData.setIndexReader(newReader);
89         // Transfer our reference to runData
90         newReader.decRef();
91         r = newReader;
92       }
93     }
94     stopNow = false;
95
96     return reopenCount;
97   }
98
99   @Override
100   public void setParams(String params) {
101     super.setParams(params);
102     pauseMSec = (long) (1000.0*Float.parseFloat(params));
103   }
104
105   @Override
106   public void close() {
107     System.out.println("NRT reopen times:");
108     for(int i=0;i<reopenCount;i++) {
109       System.out.print(" " + reopenTimes[i]);
110     }
111     System.out.println();
112   }
113
114   @Override
115   public boolean supportsParams() {
116     return true;
117   }
118 }