TaliG
TaliG

Reputation: 211

JMH setup and tear down

I've created a class. Within that class I have several methods that are marked as @Benchmark. I also have a main method that runs the JMH benchmarks:

    System.out.println("NUMBER OF THREADS: "+numOfThreads);
    Options opt = new OptionsBuilder()
            .include(JMHtopToBottom.class.getSimpleName())
            .warmupIterations(5)
            .measurementIterations(3)
            .forks(numOfThreads)
            .build();

    Collection<RunResult> collection = new Runner(opt).run();

My interest is to have:

  1. A setup method that runs only ones - right after the new Runner(opt).run(); and before all of the @Benchmark methods are called (along with their iterations).

  2. As well, to have a tear down method that runs only once right after all the methods runs and before we go back to main.

When I tried @setup and @tear_down (with Level support: Trial/Iteration/Invocation) the methods run several times and not only ones as I wished. Is there a way in JMH to annotate methods so it will run just ones - right after run() and right before the run() is over?

Upvotes: 1

Views: 3043

Answers (1)

Nitsan Wakart
Nitsan Wakart

Reputation: 2909

You are missing a few things:

  1. Forks are not threads, they are separate processes launched to run each benchmark. I.e if you set forks to 5 any benchmark (in the selected benchmark set) will be run 5 times, each time in a separate VM.
  2. Unless forks=0 (not recommended as benchmark isolation is gone, mixed compilation profiles etc, meant mostly for debugging) all benchmarks are run in separate processes. So each 'Trial' setup/teardown for a given benchmark will run once for that JVM. There is no shared 'Suite' context.

If you absolutely require some 'Suite' level context you'll have to construct it out of VM (e.g. some file that is read on benchmark setup/updated on teardown etc.).

Upvotes: 5

Related Questions