LUCENE-2964: Allow benchmark tasks from alternative packages - merge/port from 3x.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1083731 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Doron Cohen 2011-03-21 11:23:37 +00:00
parent 6d47d7377d
commit e45d28a8d3
5 changed files with 146 additions and 6 deletions

View File

@ -2,6 +2,11 @@ Lucene Benchmark Contrib Change Log
The 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/2011
LUCENE-2963: Easier way to run benchmark, by calling Benmchmark.exec(alg-file).
(Doron Cohen)

View File

@ -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>

View File

@ -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 tasks
currSequence.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));
}
@Override
public String toString() {
String newline = System.getProperty("line.separator");

View File

@ -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
};
}
}

View File

@ -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;
}
}