JMH Question: execution of code when iteration completes before TearDown

Alexei Osipov osipov.av at gmail.com
Mon Sep 26 17:07:57 UTC 2016


In my specific case I've ended up with additional "arbiter" sub-group that
sleeps during the measurement and executed "unblocking" actions when
measurement ends. Not really the way I wanted the test look like but there
is no waiting on blocking group to time-out. So it's OK for me.

@Benchmark
@GroupThreads(1)
@Group("testThroughputP1C2")
public void measureProducerP1C2(ProducerState producerState,
CoordinationState coordinationState, Control control) throws
InterruptedException {
    coordinationState.init(1, 2); // Pass number of producers and
consumers to coordination state so arbiter knows the total number of
producers and consumers
    producerIteration(producerState, coordinationState, control);
}

@Benchmark
@GroupThreads(2)
@Group("testThroughputP1C2")
public void measureConsumerP1C2(ConsumerState consumerState,
CoordinationState coordinationState, Control control) throws
InterruptedException {
    consumerIteration(consumerState, coordinationState, control);
}

@Benchmark
@GroupThreads(1)
@Group("testThroughputP1C2")
public void arbiterP1C2(ProducerState producerState, CoordinationState
coordinationState, Control control) throws InterruptedException {
    // Measurement not started yet
    if (!control.startMeasurement) {
        Thread.sleep(1);
        return;
    }
    // Arbiter waits measurement to end
    while (!control.stopMeasurement) {
        Thread.sleep(1);
    }
    executeUnblocking(producerState, coordinationState);

    // JMH will execute this method few more times on Warmdown and we
want to avoid busy spin. So let's sleep.

    Thread.sleep(1);

}


Best regards,

Alexei Osipov


2016-09-26 18:15 GMT+03:00 Lev Serebryakov <lev at serebryakov.spb.ru>:

> On 23.09.2016 09:24, Aleksey Shipilev wrote:
>
> >> I have asymmetric JMH test for a blocking queue-like data structure and
> >> can't find what's proper way to execute actions that will unblock treads
> >> blocking on my queue in JMH. I've supposed to do "unblocking" in
> @TearDown
> >> part of thread-specific state but it seems like @TearDown is not
> executed
> >> till all threads are finished.
> >
> > See this sample:
> >  http://hg.openjdk.java.net/code-tools/jmh/file/
> e810ce09937a/jmh-samples/src/main/java/org/openjdk/jmh/
> samples/JMHSample_30_Interrupts.java
>  It works, but here is one missing bit in JMH, needed for such
> benchmarks, IMHO: thread-level setup method. As result, I have
> handrolled framework to test different implementations of blocking ring
> buffer specific to my task, because some implementations of (my)
> interest need some initialization, which should be done in context of
> working threads (it assume, that reader and writer threads are never
> changed after setup, which is valid for my task), but only once before
> data pumping. You could have per-thread "isInitialized" bit of state and
> code like:
>
> @Benchmark
> @Group("G")
> public void reader() {
>  if (!isInitialized) {
>     buffer.setupReader();
>     isInitialize = true;
>  }
>  <read data from buffer under test>
> }
>
> @Benchmark
> @Group("G")
> public void writer() {
>  if (!isInitialized) {
>     buffer.setupWriter();
>     isInitialize = true;
>  }
>  <write data into buffer under test>
> }
>
>
>  but it skew results and make these specific optimizations useless :(
>
> --
> // Black Lion AKA Lev Serebryakov
>
>


More information about the jmh-dev mailing list