JMH Question: execution of code when iteration completes before TearDown

Lev Serebryakov lev at serebryakov.spb.ru
Mon Sep 26 15:15:20 UTC 2016


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