jmh: 运行 同时进行基准测试
jmh: Run benchmark concurrently
我正在 运行宁 jmh benchmark,但每次试验中的调用都是连续发生的。如何同时进行调用 运行?
这是我的代码摘要:
@State(Scope.Benchmark)
public class FooBenchmark {
@Param({"123456"})
public int barId;
@Setup
public void setup() {
}
@Benchmark
public void run(Blackhole hole) {
System.out.println("A"); // for proof that it's serial (see below)
...
System.out.println("B"); // for proof that it's serial (see below)
}
}
这将打印 A,然后打印 B。绝不会给出两个连续的 A 或 B。
如果你想明确定义测试期间使用的线程总数,你需要注释你的测试方法(用@Benchmark
注释的方法)或你的封闭class用@Threads(numberOfThreads)
作为下一个:
@Threads(10)
@Benchmark
public void run(Blackhole hole) {
在此示例中,10
个线程将并发执行测试方法。
提醒一下,这是描述此注释的部分文档:
Threads annotation provides the default number of threads to run.
This annotation may be put at Benchmark
method to have effect
on that method only, or at the enclosing class instance to have the effect
over all Benchmark
methods in the class. This annotation may be
overridden with the runtime options.
默认情况下它只会使用一个线程。
如果您使用 @Threads(Threads.MAX)
,它将使用 Runtime.getRuntime().availableProcessors()
个线程。
我正在 运行宁 jmh benchmark,但每次试验中的调用都是连续发生的。如何同时进行调用 运行?
这是我的代码摘要:
@State(Scope.Benchmark)
public class FooBenchmark {
@Param({"123456"})
public int barId;
@Setup
public void setup() {
}
@Benchmark
public void run(Blackhole hole) {
System.out.println("A"); // for proof that it's serial (see below)
...
System.out.println("B"); // for proof that it's serial (see below)
}
}
这将打印 A,然后打印 B。绝不会给出两个连续的 A 或 B。
如果你想明确定义测试期间使用的线程总数,你需要注释你的测试方法(用@Benchmark
注释的方法)或你的封闭class用@Threads(numberOfThreads)
作为下一个:
@Threads(10)
@Benchmark
public void run(Blackhole hole) {
在此示例中,10
个线程将并发执行测试方法。
提醒一下,这是描述此注释的部分文档:
Threads annotation provides the default number of threads to run.
This annotation may be put at
Benchmark
method to have effect on that method only, or at the enclosing class instance to have the effect over allBenchmark
methods in the class. This annotation may be overridden with the runtime options.
默认情况下它只会使用一个线程。
如果您使用 @Threads(Threads.MAX)
,它将使用 Runtime.getRuntime().availableProcessors()
个线程。