Question on "Control" class contract

Aleksey Shipilev shade at
Tue Sep 20 09:37:24 UTC 2016


On 09/20/2016 02:41 AM, Осипов Алексей wrote:
> @State(Scope.Group)
> @BenchmarkMode(Mode.Throughput)
> @Measurement(time =5, iterations =1)
> @Warmup(time =5, iterations =1)
> public class MyBenchmark {
>     @Benchmark @Group("pingpong")
>     public void ping(Control cnt) {
>         while (!cnt.stopMeasurement) {
>             // this body is intentionally left blank }
>     }
>     @Benchmark @Group("pingpong")
>     public void pong(Control cnt) {
>         while (!cnt.stopMeasurement) {
>             // this body is intentionally left blank }
>     }
> }
> So I've removed CAS part and left only busy loops on "stopMeasurement"
> field of "Control" class.
> I've expected that this benchmark will end after 5 seconds. However it
> looks like "stopMeasurement" never switches to True and benchmark
> invocations never finish.

Ah, there is an unspoken caveat: at least one of the threads should exit
the @Benchmark method for this mechanics to work. Control was supposed
to work in the cases where one thread exiting leaves another thread
hanging waiting for the first one, never exiting itself.

You are not supposed to block all the @Benchmark threads. That would be
useless for measurement anyhow: from the perspective of JMH, the
@Benchmark would execute exactly once during the iteration time, and the
op/sec would be constant.

Anyhow, we better notify start/stop more consistently, which also solves
your trouble:


More information about the jmh-dev mailing list