我使用 jmh( http://openjdk.java.net/projects/code-tools/jmh/ ) 对某些方法进行基准测试。此外,我还有一组参数,我想将它们用作运行此方法的参数。是否可以为每个特定的参数值生成一个方法(使用 @GenerateMicroBenchmark 注释)?
现在我使用类似的实现,但不是那么方便,因为我必须手动编写很多统一的代码:
interface State {
int action();
void prepare();
}
class Method {
...;
toString() { return "State1_" + param; }
}
{
Method[] states;
curState = -1;
count = 0;
int[] params = {1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 11000, 12000};
for (int param: params) {
states[count++] = new Method(param);
}
}
@Setup(Level.Invocation)
public void prepareState() {
if (curState != -1) {
states[curState].prepare();
}
}
@GenerateMicroBenchmark
public int param_1000() {
curState = 0;
return states[curState].action();
}
@GenerateMicroBenchmark
public int param_2000() {
curState = 1;
return states[curState].action();
}
@GenerateMicroBenchmark
public int param_3000() {
curState = 2;
return states[curState].action();
}
...
@GenerateMicroBenchmark
public int param_12000() {
curState = 11;
return states[curState].action();
}