JMH Sample 和 SingleShot 的区别

JMH difference between Sample and SingleShot

设置Mode.SampleMode.SingleShot以及measurementIterations设置为大数有什么区别?这两种模式实际上是相同的还是存在一些数量上的差异?

但是...Javadoc 非常具体地说明了区别?

Sample time: samples the time for each operation.

Runs by continuously calling {@link Benchmark} methods, and randomly samples the time needed for the call. This mode automatically adjusts the sampling frequency, but may omit some pauses which missed the sampling measurement. This mode is time-based, and it will run until the iteration time expires.

并且:

Single shot time: measures the time for a single operation.

Runs by calling {@link Benchmark} once and measuring its time. This mode is useful to estimate the "cold" performance when you don't want to hide the warmup invocations, or if you want to see the progress from call to call, or you want to record every single sample. This mode is work-based, and will run only for a single invocation of {@link Benchmark} method.

Caveats for this mode include:

  • More warmup/measurement iterations are generally required.
  • Timers overhead might be significant if benchmarks are small; switch to {@link #SampleTime} mode if that is a problem.