JMH Question: execution of code when iteration completes before TearDown
Lev Serebryakov
lev at serebryakov.spb.ru
Tue Sep 27 18:19:07 UTC 2016
On 27.09.2016 15:54, Aleksey Shipilev wrote:
> It can even initialize after the shared @State(Benchmark) object:
> http://hg.openjdk.java.net/code-tools/jmh/file/e810ce09937a/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_29_StatesDAG.java
Hmmm, I wrote this [code at the end of the message] and sometimes I got
exception from my setup method, that shows that it was called twice. My
".registerReader()" and ".registerWriter()" checks, that they was called
only once, and sometimes it is not the case and my invariant check
throws exception.
I'm using 1.14.1
@Fork(1)
@Warmup(iterations = 5, time = 5)
@Measurement(iterations = 10, time = 5)
@State(Scope.Group)
@Timeout(time = 5)
public class Buffers {
@State(Scope.Group)
public static class GroupState {
@Param({ ... })
int writeFrame;
@Param({ ... })
int readFrame;
@Param({ ... })
String implName;
SPSCBuffer impl;
@Setup
public synchronized void setup()
throws ClassNotFoundException,
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException,
InstantiationException
{
String className = "benchmarks.buffers.impl." + implName;
Class<? extends SPSCBuffer> clazz =
(Class<? extends SPSCBuffer>)
getClass().getClassLoader().loadClass(className);
Constructor<? extends SPSCBuffer> c =
clazz.getConstructor(int.class, int.class, int.class, int.class);
impl = c.newInstance(writeFrame, readFrame, 0, 128 * 1024);
}
}
@State(Scope.Thread)
public static class ReaderState {
SPSCBuffer buf;
int frame;
@Setup
public void setup(GroupState gs) {
buf = gs.impl;
frame = gs.readFrame;
// SOMETIMES CALLED TWICE! WHY?
buf.registerReader(Thread.currentThread());
}
}
@State(Scope.Thread)
public static class WriterState {
SPSCBuffer buf;
int frame;
@Setup
public void setup(GroupState gs) {
buf = gs.impl;
frame = gs.readFrame;
// SOMETIMES CALLED TWICE! WHY?
buf.registerWriter(Thread.currentThread());
}
}
@Benchmark
@Group("buf")
public void writer(WriterState s) throws InterruptedException {
// Write to buffer
}
@Benchmark
@Group("buf")
public void reader(ReaderState s) throws InterruptedException {
// Read from buffer
}
}
--
// Black Lion AKA Lev Serebryakov
More information about the jmh-dev
mailing list