gpt4 book ai didi

java - 如何在没有 Maven 的情况下使用 Caliper 基准测试版快照?

转载 作者:行者123 更新时间:2023-11-29 08:54:10 25 4
gpt4 key购买 nike

有人要求我使用 Google 的 Caliper 项目创建一些微基准测试。我非常想使用最新的 beta 快照的注释功能,但除了一些小示异常(exception),我很难找到关于如何实际运行该东西的好文档......有一个视频教程可以指导用户新的 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]

它似乎没有检测到任何仪器。我没有传入任何内容,正如文档中提到的那样,它只使用默认分配、运行时(这对我的目的来说很好)。

双重编辑:发现那个问题,愚蠢的错误。一旦我确认,我会做一个快速的记录。

最佳答案

Running it gives me the message that I did not set a default value for size.

参数设置为默认值:

@Param({"1", "10", "1000"}) 整数大小;

或者通过 -D 标志传递值。例如:-Dsize=1,10,1000。枚举和 boolean 值得到特殊处理,因为它使用所有可能的值而不必在注释中列出它们。


Removing "size" entirely by commenting out the @Param line and giving a hard value to the array declaration in setUp just leads to it deciding that there are "No Experiments to be done," which makes sense, I suppose.

问题很可能是您的基准测试是一个内部类并且需要对封闭类的引用(尽管这应该是一个错误)。要么将基准类设为顶级类(推荐),要么将其设为静态。


此外,没有特别需要包含 main 方法。将基准类作为第一个参数调用 CaliperMain 是等效的。

关于java - 如何在没有 Maven 的情况下使用 Caliper 基准测试版快照?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21193140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com