I'm running a jmh benchmark, but the invocations in each trial are occurring in serial. How can I make the invocations run concurrently?
Here's a summary of my code:
@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)
}
}
This will print A and then B. Will never give two consecutive A's or B's.
If you want to explicitly define the total amount of threads to use during your test, you need to annotate your test method (method annotated with @Benchmark
) or your enclosing class with @Threads(numberOfThreads)
as next:
@Threads(10)
@Benchmark
public void run(Blackhole hole) {
In this example, 10
threads will execute the test method concurrently.
As reminder, here is part of the documentation that describes this annotation:
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.
By default it will use only one thread.
If you use @Threads(Threads.MAX)
, it will use Runtime.getRuntime().availableProcessors()
threads.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With