使用JMH对简单的Spring应用程序进行基准测试

时间:2019-04-27 07:54:58

标签: java spring jmh

我正在编写一个简单的jmh-demo程序,以了解如何对spring程序进行基准测试。按照步骤here,我从java -jar target/benchmarks.jar开始基准测试。它打印出以下内容,只是库存!

# 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

这是主类的代码

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);
  }
}

问题是什么,我该怎么办?

1 个答案:

答案 0 :(得分:0)

我跳过了使用jar文件的过程,只是简单地添加了一种运行基准测试的主要方法:

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);
  }
}