jmh: Run benchmark concurrently
Asked Answered
C

1

9

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.

Carmody answered 22/9, 2016 at 16:32 Comment(0)
L
19

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.

Leddy answered 22/9, 2016 at 18:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.