简体   繁体   中英

Benchmarking a simple spring application with JMH

I am writing a simple jmh-demo program to see how to benchmark a spring program. Following the steps here , I start the benchmark with java -jar target/benchmarks.jar . It prints out the following and just stocks!

# JMH version: 1.21
# VM version: JDK 1.8.0_121, Java HotSpot(TM) 64-Bit Server VM, 25.121-b13
# VM invoker: C:\jdk1.8.0_121\jre\bin\java.exe
# VM options: <none>
# Warmup: 5 iterations, 10 s each
# Measurement: 5 iterations, 10 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: MyBenchmark.testMethod

# Run progress: 0.00% complete, ETA 00:08:20
# Fork: 1 of 5
# Warmup Iteration   1: Do Setup
init context
DEBUG [MyBenchmark.testMethod-jmh-worker-1] (AbstractApplicationContext.java:594) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1dba888f

Here is the code for the main class

public class MyBenchmark {
  @State(Scope.Thread)
  public static class MyState {
    @Setup(Level.Trial)
    public void doSetup() {
      System.out.println("Do Setup");
      if (context == null) {
        System.out.println("init context");
        context = new ClassPathXmlApplicationContext(
            "applicationContext.xml");
        System.out.println("after init");
      }
      System.out.println("Did Setup");
    }

    public ApplicationContext context;
    public int a = 1;
    public int b = 2;
  }

  @Benchmark @BenchmarkMode(Mode.Throughput) @OutputTimeUnit(TimeUnit.MINUTES)
  public void testMethod(MyState state, Blackhole blackhole) {
    int sum1 = state.a + state.b;
    int sum2 = state.a + state.a + state.b + state.b;

    blackhole.consume(sum1);
    blackhole.consume(sum2);
  }
}

What is the problem and what should I do?

I skipped working with the jar file and just simply added a main method to run the benchmark:

public static void main(String... args) throws RunnerException {//just run (not debug) it
  Options opt = new OptionsBuilder()
      .include(MyBenchmark.class.getSimpleName())
      .build();
  Collection<RunResult> runResults = new Runner(opt).run();
  for (RunResult runResult : runResults) {
    System.out.println(runResult);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM