Skip to content

Apache Lucene open-source search software

LUCENE-1495: allow TaskSequence to run for certain time

Michael McCandlessbdc42e534209
diff --git a/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/TaskSequence.java b/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/TaskSequence.java
index 8ac89b6c6f1..205f529ad49 100644
--- a/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/TaskSequence.java
+++ b/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/TaskSequence.java
@@ -42,6 +42,9 @@ public class TaskSequence extends PerfTask {
private boolean anyExhaustibleTasks;
private boolean collapsable = false; // to not collapse external sequence named in alg.
+ private boolean fixedTime; // true if we run for fixed time
+ private double runTimeSec; // how long to run for
+
public TaskSequence (PerfRunData runData, String name, TaskSequence parent, boolean parallel) {
super(runData);
collapsable = (name == null);
@@ -79,11 +82,17 @@ public class TaskSequence extends PerfTask {
return repetitions;
}
+ public void setRunTime(double sec) throws Exception {
+ runTimeSec = sec;
+ fixedTime = true;
+ }
+
/**
* @param repetitions The repetitions to set.
* @throws Exception
*/
public void setRepetitions(int repetitions) throws Exception {
+ fixedTime = false;
this.repetitions = repetitions;
if (repetitions==REPEAT_EXHAUST) {
if (isParallel()) {
@@ -119,8 +128,12 @@ public class TaskSequence extends PerfTask {
initTasksArray();
int count = 0;
-
- for (int k=0; (repetitions==REPEAT_EXHAUST && !exhausted) || k<repetitions; k++) {
+
+ final long t0 = System.currentTimeMillis();
+
+ final long runTime = (long) (runTimeSec*1000);
+
+ for (int k=0; fixedTime || (repetitions==REPEAT_EXHAUST && !exhausted) || k<repetitions; k++) {
for(int l=0;l<tasksArray.length;l++)
try {
final PerfTask task = tasksArray[l];
@@ -130,6 +143,10 @@ public class TaskSequence extends PerfTask {
} catch (NoMoreDataException e) {
exhausted = true;
}
+ if (fixedTime && System.currentTimeMillis()-t0 > runTime) {
+ repetitions = k+1;
+ break;
+ }
}
return count;
}
diff --git a/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/utils/Algorithm.java b/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/utils/Algorithm.java
index 680891edf31..cb2bba6bec0 100644
--- a/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/utils/Algorithm.java
+++ b/contrib/benchmark/src/java/org/apache/lucene/benchmark/byTask/utils/Algorithm.java
@@ -124,9 +124,18 @@ public class Algorithm {
if ((char)stok.ttype == '*') {
((TaskSequence)prevTask).setRepetitions(TaskSequence.REPEAT_EXHAUST);
} else {
- if (stok.ttype!=StreamTokenizer.TT_NUMBER)
- throw new Exception("expected repetitions number: - "+stok.toString());
- ((TaskSequence)prevTask).setRepetitions((int)stok.nval);
+ if (stok.ttype!=StreamTokenizer.TT_NUMBER) {
+ throw new Exception("expected repetitions number or XXXs: - "+stok.toString());
+ } else {
+ double num = stok.nval;
+ stok.nextToken();
+ if (stok.ttype == StreamTokenizer.TT_WORD && stok.sval.equals("s")) {
+ ((TaskSequence) prevTask).setRunTime(num);
+ } else {
+ stok.pushBack();
+ ((TaskSequence) prevTask).setRepetitions((int) num);
+ }
+ }
}
// check for rate specification (ops/min)
stok.nextToken();
diff --git a/contrib/benchmark/src/test/org/apache/lucene/benchmark/byTask/TestPerfTasksLogic.java b/contrib/benchmark/src/test/org/apache/lucene/benchmark/byTask/TestPerfTasksLogic.java
index a311c4fe339..70e45d1ff35 100755
--- a/contrib/benchmark/src/test/org/apache/lucene/benchmark/byTask/TestPerfTasksLogic.java
+++ b/contrib/benchmark/src/test/org/apache/lucene/benchmark/byTask/TestPerfTasksLogic.java
@@ -98,6 +98,29 @@ public class TestPerfTasksLogic extends TestCase {
ir.close();
}
+ /**
+ * Test timed sequence task.
+ */
+ public void testTimedSearchTask() throws Exception {
+ String algLines[] = {
+ "ResetSystemErase",
+ "CreateIndex",
+ "{ AddDoc } : 1000",
+ "Optimize",
+ "CloseIndex",
+ "OpenReader",
+ "{ CountingSearchTest } : 1.5s",
+ "CloseReader",
+ };
+
+ long t0 = System.currentTimeMillis();
+ Benchmark benchmark = execBenchmark(algLines);
+ long t1 = System.currentTimeMillis();
+ assertTrue(CountingSearchTestTask.numSearches > 0);
+ long elapsed = t1-t0;
+ assertTrue(elapsed > 1500 && elapsed < 2000);
+ }
+
public void testHighlighting() throws Exception {
// 1. alg definition (required in every "logic" test)
String algLines[] = {