Apache Lucene open-source search software
LUCENE-2964: Allow benchmark tasks from alternative packages - merge/port from 3x.
Doron Cohene45d28a8d340
diff --git a/modules/benchmark/CHANGES.txt b/modules/benchmark/CHANGES.txtindex 0f2736fe4e3..d217226867d 100644--- a/modules/benchmark/CHANGES.txt+++ b/modules/benchmark/CHANGES.txt@@ -2,6 +2,11 @@ Lucene Benchmark Contrib Change LogThe Benchmark contrib package contains code for benchmarking Lucene in a variety of ways.+03/21/2011+ LUCENE-2964: Allow benchmark tasks from alternative packages,+ specified through a new property "alt.tasks.packages".+ (Doron Cohen, Shai Erera)+03/20/2011LUCENE-2963: Easier way to run benchmark, by calling Benmchmark.exec(alg-file).(Doron Cohen)diff --git a/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/package.html b/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/package.htmlindex f5440bd5439..b92da02b171 100644--- a/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/package.html+++ b/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/package.html@@ -170,6 +170,9 @@ with the benchmark.ext.classpath property:<font color="#FF0000">-Dbenchmark.ext.classpath=/mydir/classes</font> -Dtask.mem=512M</li></ul>+<u>External tasks</u>: When writing your own tasks under a package other than+<b>org.apache.lucene.benchmark.byTask.tasks</b> specify that package thru the+<font color="#FF0000">alt.tasks.packages</font> property.</p><a name="algorithm"></a>@@ -590,6 +593,14 @@ Here is a list of currently defined properties:<ul><li>doc.delete.step</li></ul></li>++ <li><b>Task alternative packages</b>:+ <ul><li>alt.tasks.packages+ - comma separated list of additional packages where tasks classes will be looked for+ when not found in the default package (that of PerfTask). If the same task class+ appears in more than one package, the package indicated first in this list will be used.+ </li></ul>+ </li></ol>diff --git a/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/utils/Algorithm.java b/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/utils/Algorithm.javaindex b4d6198b73b..0d6ff80ef4e 100644--- a/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/utils/Algorithm.java+++ b/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/utils/Algorithm.java@@ -21,6 +21,7 @@ import java.io.StreamTokenizer;import java.io.StringReader;import java.lang.reflect.Constructor;import java.util.ArrayList;+import java.util.Arrays;import org.apache.lucene.benchmark.byTask.PerfRunData;import org.apache.lucene.benchmark.byTask.tasks.PerfTask;@@ -33,15 +34,23 @@ import org.apache.lucene.benchmark.byTask.tasks.TaskSequence;public class Algorithm {private TaskSequence sequence;+ private final String[] taskPackages;/*** Read algorithm from file+ * Property examined: alt.tasks.packages == comma separated list of+ * alternate package names where tasks would be searched for, when not found+ * in the default package (that of {@link PerfTask}{@link #getClass()}).+ * If the same task class appears in more than one package, the package+ * indicated first in this list will be used.* @param runData perf-run-data used at running the tasks.* @throws Exception if errors while parsing the algorithm*/@SuppressWarnings("fallthrough")public Algorithm (PerfRunData runData) throws Exception {- String algTxt = runData.getConfig().getAlgorithmText();+ Config config = runData.getConfig();+ taskPackages = initTasksPackages(config);+ String algTxt = config.getAlgorithmText();sequence = new TaskSequence(runData,null,null,false);TaskSequence currSequence = sequence;PerfTask prevTask = null;@@ -55,14 +64,13 @@ public class Algorithm {boolean colonOk = false;boolean isDisableCountNextTask = false; // only for primitive taskscurrSequence.setDepth(0);- String taskPackage = PerfTask.class.getPackage().getName() + ".";while (stok.nextToken() != StreamTokenizer.TT_EOF) {switch(stok.ttype) {case StreamTokenizer.TT_WORD:String s = stok.sval;- Constructor<? extends PerfTask> cnstr = Class.forName(taskPackage+s+"Task")+ Constructor<? extends PerfTask> cnstr = taskClass(config,s).asSubclass(PerfTask.class).getConstructor(PerfRunData.class);PerfTask task = cnstr.newInstance(runData);task.setDisableCounting(isDisableCountNextTask);@@ -248,9 +256,33 @@ public class Algorithm {}}- /* (non-Javadoc)- * @see java.lang.Object#toString()- */+ private String[] initTasksPackages(Config config) {+ String alts = config.get("alt.tasks.packages", null);+ String dfltPkg = PerfTask.class.getPackage().getName();+ if (alts==null) {+ return new String[]{ dfltPkg };+ }+ ArrayList<String> pkgs = new ArrayList<String>();+ pkgs.add(dfltPkg);+ for (String alt : alts.split(",")) {+ pkgs.add(alt);+ }+ return pkgs.toArray(new String[0]);+ }++ private Class<?> taskClass(Config config, String taskName)+ throws ClassNotFoundException {+ for (String pkg : taskPackages) {+ try {+ return Class.forName(pkg+'.'+taskName+"Task");+ } catch (ClassNotFoundException e) {+ // failed in this package, might succeed in the next one...+ }+ }+ // can only get here if failed to instantiate+ throw new ClassNotFoundException(taskName+" not found in packages "+Arrays.toString(taskPackages));+ }+@Overridepublic String toString() {String newline = System.getProperty("line.separator");diff --git a/modules/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/alt/AltPackageTaskTest.java b/modules/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/alt/AltPackageTaskTest.javanew file mode 100644index 00000000000..3727791d6ce--- /dev/null+++ b/modules/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/alt/AltPackageTaskTest.java@@ -0,0 +1,55 @@+package org.apache.lucene.benchmark.byTask.tasks.alt;++/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import org.apache.lucene.benchmark.BenchmarkTestCase;+import org.apache.lucene.benchmark.byTask.Benchmark;++/** Tests that tasks in alternate packages are found. */+public class AltPackageTaskTest extends BenchmarkTestCase {++ /** Benchmark should fail loading the algorithm when alt is not specified */+ public void testWithoutAlt() throws Exception {+ try {+ execBenchmark(altAlg(false));+ assertFalse("Should have failed to run the algorithm",true);+ } catch(Exception e) {+ // expected exception, do nothing+ }+ }++ /** Benchmark should be able to load the algorithm when alt is specified */+ public void testWithAlt() throws Exception {+ Benchmark bm = execBenchmark(altAlg(true));+ assertNotNull(bm);+ assertNotNull(bm.getRunData().getPoints());+ }++ private String[] altAlg(boolean allowAlt) {+ String altTask = "{ AltTest }";+ if (allowAlt) {+ return new String[] {+ "alt.tasks.packages = " +this.getClass().getPackage().getName(),+ altTask+ };+ }+ return new String[] {+ altTask+ };+ }+}diff --git a/modules/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/alt/AltTestTask.java b/modules/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/alt/AltTestTask.javanew file mode 100644index 00000000000..a38583ff69c--- /dev/null+++ b/modules/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/alt/AltTestTask.java@@ -0,0 +1,37 @@+package org.apache.lucene.benchmark.byTask.tasks.alt;++/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import org.apache.lucene.benchmark.byTask.PerfRunData;+import org.apache.lucene.benchmark.byTask.tasks.PerfTask;++/**+ * {@link PerfTask} which does nothing, but is in a different package+ */+public class AltTestTask extends PerfTask {++ public AltTestTask(PerfRunData runData) {+ super(runData);+ }++ @Override+ public int doLogic() throws Exception {+ return 0;+ }++}