Benchmark keeps running
Yann Le Tallec
ylt at letallec.org
Wed May 8 01:47:45 PDT 2013
Which falls under the category: "I'm probably missing something obvious".
Sorry about that and thanks.
On 8 May 2013 08:17, Aleksey Shipilev <aleksey.shipilev at oracle.com> wrote:
> On 05/08/2013 07:21 AM, Yann Le Tallec wrote:
> > @OutputTimeUnit(TimeUnit.NANOSECONDS)
> > public class CAS {
> >
> > @State(Scope.Benchmark)
> > public static class AIState {
> > private final AtomicInteger ai = new AtomicInteger();
> > }
> >
> > @GenerateMicroBenchmark(BenchmarkType.AverageTimePerOp)
> > public int cas(CAS.AIState state) {
> > int i = state.ai.get();
> > while (!state.ai.compareAndSet(i, i + 1)) { }
> > return state.ai.get();
> > }
> > }
>
> Your benchmark is probably incorrect, you have to re-read the AI value
> in the CAS'ed loop: if you had failed to CAS, that means the current
> value is not "i" anymore:
>
> @GenerateMicroBenchmark(BenchmarkType.AverageTimePerOp)
> public int cas(AIState state) {
> int i;
> while (!state.ai.compareAndSet((i = state.ai.get()), i + 1)) { }
> return state.ai.get();
> }
>
> Otherwise some thread can obviously be stuck. If that is the behavior
> you are after, you have to let JMH terminate the loop, see
>
> http://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_18_Control.java
>
> -Aleksey.
>
More information about the jmh-dev
mailing list