我试图弄清楚如何使用 Caliper 在 Eclipse 中进行基准测试,但我一无所获。我尝试按照此处找到的 26 分钟教程进行操作:https ://code.google.com/p/caliper/但我很快就迷路了。我已经下载了 Caliper jar 文件,但我不确定它应该在哪个文件夹中。我还下载了 Maven for Eclipse 插件,但我什至不确定这是否有必要。是否可以从 Eclipse 帮助菜单中的“安装新软件..”选项安装 Caliper?我只想能够对我为正在学习的数据结构和算法类创建的一些算法进行非常简单的速度测试。
3 回答
这个答案现在已经过时了。Caliper 在 Windows 中工作了一年多,至少:https ://code.google.com/p/caliper/issues/detail?id=167
卡尺在 Windows 中不起作用。看到这个案例。您需要使用 0.5-rc1 版本,该版本还有其他问题,但仍然很好,并且缺少很多功能,但它确实可以在 Windows 中使用。
如果您知道如何使用 Maven,请将这个 pom 片段添加到您的 pom.xml 中。
<dependency> <groupId>com.google.caliper</groupId> <artifactId>caliper</artifactId> <version>0.5-rc1</version> </dependency>
- 如果您想学习 maven,请先阅读以下内容:http ://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
- 将您的项目转换为 Maven 项目(右键单击项目 -> 配置 -> 转换为 Maven 项目)
- 如果您不知道如何使用 Maven(这里是如何使用图片执行此操作的指南):
- 下载 0.5-rc1 jar
- 右键单击您要使用的项目并选择
Build Path -> Configure Build Path
- 使用将其添加到您的库选项卡
Add External Jar
完成后,您就可以开始编写基准测试了。这是我为另一个 Stack Overflow 问题编写的基准测试示例。
以下是在撰写本文时使用最新版本的 Caliper caliper-1.0-beta2设置工作 Caliper 类的方法。据我所知,除了 Caliper 代码文件中的内联注释之外,没有记录此过程。
首先在pom.xml中安装 caliper-1.0-beta2或下载 jar 文件。然后制作一个像这样的文件:
import com.google.caliper.Benchmark;
import com.google.caliper.runner.CaliperMain;
public class DemoBenchmark {
public static void main(String[] args) {
CaliperMain.main(DemoBenchmark.class, args);
}
@Benchmark
void timeStringBuilder(int reps) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < reps; i++) {
sb.setLength(0);
sb.append("hello world");
}
}
}
运行该文件,Caliper 将为您执行基准测试。
我已经在 Windows 上尝试了所有建议的解决方案,但是没有成功。
我总是遇到以下错误:
This selection yields 4 experiments.
ERROR: Trial failed to complete (its results will not be included in the run):
The worker exited without producing data. It has likely crashed. Inspect C:\Users\Piotr\AppData\Local\Temp\1583760443321-0\trial-1.log to see any worker output.
我能够通过将@VmOptions
注释添加到自定义基准类来解决它。
这是完整的配置:
@VmOptions("-XX:-TieredCompilation")
public class CaliperBenchmark {
@BeforeExperiment
void setUp() {
// set up
}
@Benchmark
void boxed() {
// test
}
}
马文:com.google.caliper caliper 1.0-beta-2 compile
主类:
public class Main {
public static void main(String[] args) {
CaliperMain.main(CaliperBenchmark.class, args);
}
}
命令行:mvn exec:java -Dexec.mainClass="com.google.caliper.runner.CaliperMain" -Dexec.args="org.CaliperBenchmark"