我被要求使用 Google 的 Caliper 项目来创建一些微基准测试。我非常想使用最新的 beta 快照的注释功能,但是除了一些小例子之外,我很难找到关于如何实际运行这个东西的好文档……有一个视频教程可以指导用户新的 Maven 集成功能,我也被要求不要使用。
现在我只是从他们的一个中剥离了一个小例子,并用我从另一个 SO 问题中收集到的一些其他信息进行了修改:
public class Benchmarks {
public class Test {
@Param int size; // set automatically by framework
private int[] array; // set by us, in setUp()
@BeforeExperiment void setUp() {
// @Param values are guaranteed to have been injected by now
array = new int[size];
}
@Benchmark int timeArrayIteration(int reps) {
int dummy = 0;
for (int i = 0; i < reps; i++) {
for (int doNotIgnoreMe : array) {
dummy += doNotIgnoreMe;
}
}
return dummy;
}
}
//(Questionable practice here?)
public static void main (String args[]) {
CaliperMain.main(Test.class, args);
}
}
运行它会告诉我我没有设置大小的默认值。我无法追踪我应该把它放在哪里。
通过注释掉 @Param 行并为 setUp 中的数组声明赋予硬值来完全删除“大小”只会导致它决定“没有要进行的实验”,我想这是有道理的。
如果有任何最新的资源或教程可以指出我做错了什么(可能很多,老实说)我会非常感激。
编辑:
根据一些建议,我已对此进行了更新:
public class Benchmarks {
@Param({"1", "10", "1000"}) int size; // set automatically by framework
private int[] array; // set by us, in setUp()
@BeforeExperiment void setUp() {
// @Param values are guaranteed to have been injected by now
array = new int[size];
}
@Benchmark int timeArrayIteration(int reps) {
int dummy = 0;
for (int i = 0; i < reps; i++) {
for (int doNotIgnoreMe : array) {
dummy += doNotIgnoreMe;
}
}
return dummy;
}
}
我正在运行 beta 快照并将 Benchmarks 类作为参数传递。我收到以下信息:
Experiment selection:
Instruments: []
User parameters: {size=[1, 10, 1000]}
Virtual machines: [default]
Selection type: Full cartesian product
There were no experiments to be performed for the class Benchmarks using the instruments [allocation, runtime]
它似乎没有检测到任何仪器。我没有传入任何内容,因为它在文档中提到它只是使用默认分配、运行时(这对我的目的来说很好)。
双重编辑:发现了那个问题,愚蠢的错误。一旦我确认它会做一个快速的记录。