1 package org.apache.lucene.benchmark.byTask.tasks;
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.
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;
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.
32 * @lucene.experimental It's also not generally usable, eg
33 * you cannot change which query is executed.
35 public class NearRealtimeReaderTask extends PerfTask {
37 long pauseMSec = 3000L;
40 int[] reopenTimes = new int[1];
42 public NearRealtimeReaderTask(PerfRunData runData) {
47 public int doLogic() throws Exception {
49 final PerfRunData runData = getRunData();
52 IndexWriter w = runData.getIndexWriter();
54 throw new RuntimeException("please open the writer before invoking NearRealtimeReader");
57 if (runData.getIndexReader() != null) {
58 throw new RuntimeException("please close the existing reader before invoking NearRealtimeReader");
61 long t = System.currentTimeMillis();
62 IndexReader r = IndexReader.open(w, true);
63 runData.setIndexReader(r);
64 // Transfer our reference to runData
67 // TODO: gather basic metrics for reporting -- eg mean,
68 // stddev, min/max reopen latencies
70 // Parent sequence sets stopNow
73 long waitForMsec = (pauseMSec - (System.currentTimeMillis() - t));
74 if (waitForMsec > 0) {
75 Thread.sleep(waitForMsec);
76 //System.out.println("NRT wait: " + waitForMsec + " msec");
79 t = System.currentTimeMillis();
80 final IndexReader newReader = r.reopen();
82 final int delay = (int) (System.currentTimeMillis()-t);
83 if (reopenTimes.length == reopenCount) {
84 reopenTimes = ArrayUtil.grow(reopenTimes, 1+reopenCount);
86 reopenTimes[reopenCount++] = delay;
87 // TODO: somehow we need to enable warming, here
88 runData.setIndexReader(newReader);
89 // Transfer our reference to runData
100 public void setParams(String params) {
101 super.setParams(params);
102 pauseMSec = (long) (1000.0*Float.parseFloat(params));
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]);
111 System.out.println();
115 public boolean supportsParams() {