Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jmh: Run benchmark concurrently

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.

like image 507
lf215 Avatar asked Dec 19 '22 13:12

lf215


1 Answers

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 all Benchmark 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.

like image 162
Nicolas Filotto Avatar answered Jan 04 '23 23:01

Nicolas Filotto