Max and min time benchmark in Java with JMH



I want to write a benchmark to keep track of maximum, minimum and average time required for the execution of some Java code.

I read about JMH (Java Microbenchmark Harness) and it seems to match my needs, but I’m not quite sure about which mode should I use:

  • Mode.Throughput measures the raw throughput by continuously calling the benchmark method in a time-bound iteration, and counting how many times we executed the method.

    It seems not to be what I am looking for.

  • Mode.AverageTime measures the average time it takes for your benchmark method to be executed.

    This should be 1/3 of my needs.

  • Mode.SingleShotTime measures how long a single benchmark method execution takes to run.

    It seems not to be what I am looking for.

  • Mode.SampleTime is the most obscure to me.

    Documentations on examples states that it “samples” the execution time, by running the method in a time-bound iteration, but instead of measuring the total time, it measures the time spent in “some” of the benchmark method calls. What does it mean?

What mode can provide me min and max times? Are there any more modes, or are there other frameworks to do the job?

Answer

If you run your @Benchmark with @BenchmarkMode(Mode.AverageTime), as a bonus, you will get an output like:

Result "StopAllBenchmarks.measureRight":
 2.387 ±(99.9%) 0.307 ns/op [Average]
 (min, avg, max) = (2.339, 2.387, 2.526), stdev = 0.080
 CI (99.9%): [2.079, 2.694] (assumes normal distribution)

Notice the min, avg, max



Source: stackoverflow