JMH how to tips

JMH is a good tool for benchmarking your code.

The following hints will help you to save time in an otherwise not easy set up.

  • always use state and scope at class level. UseĀ Scope.Thread when uncertainn. Like:
    @State(Scope.Thread)
    public class MyClass {
  • No benchmarks to run message? check the include/exclude regexps.
  • Always use Fork s greater than zero.
  • Methods annotated with @Benchmark should be public.
  • Always return something from a benchmarked method.
  • Do you need to organize many becnhmarks though many classes? Use the command line to select a specific benchmark to run. Like:

Please refer to thisĀ  document for more hints.