What it the equivalent to @BeforeClass / @BeforeAll in JMH?

37 Views Asked by At

before I start, I wanted to apologize for my bad English.

This is my class:

package de.shd.kps.backend.customer.benchmark;

import java.util.concurrent.TimeUnit;

import org.junit.runner.RunWith;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.springframework.test.context.junit4.SpringRunner;

@State(Scope.Benchmark)                                                       // Addition for JMH
@BenchmarkMode(Mode.AverageTime)                                              // Addition for JMH
@OutputTimeUnit(TimeUnit.MICROSECONDS)                                        // Addition for JMH
@RunWith(SpringRunner.class)                                                  // Addition for JMH

public class ExampleTest extends AbstractBenchmarkHarnessForTests
{
   private static int setupCalls = 0;

   //@Setup(Level.Invocation) //Setup-Calls: 4417801
   //@Setup(Level.Iteration) Setup-Calls: 3
   //@Setup(Level.Trial) Setup-Calls: 3
   @Setup // Setup-Calls: 3
   public void setup()
   {
      setupCalls++;
      System.err.println("Setup-Calls:");
      System.err.println(setupCalls);
   }

   @Benchmark
   public void benchmark()
   {
   }

   @Benchmark
   public void benchmark2()
   {
   }

   @Benchmark
   public void benchmark3()
   {
   }
}

I am using java with JMH. I have a Setup Method and 3 Benchmark Methods. I want JMH to call the setup Method only one time. Like the @BeforeAll Annotation in junit.

This sheet JMH_Cheatsheet tells me, that there are 3 Levels, that controls the number of calls.

I tried every possible variant. This are the solutions:

@Setup(Level.Invocation) Setup-Calls: 4417801
@Setup(Level.Iteration) Setup-Calls: 3
@Setup(Level.Trial) Setup-Calls: 3

I don´t want that jmh calls the setup method each time for every method. How can i configure it? Thanks in advance

0

There are 0 best solutions below