1 /*
2  * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package org.openjdk.bench.java.util.concurrent;
24 
25 import org.openjdk.jmh.annotations.Benchmark;
26 import org.openjdk.jmh.annotations.BenchmarkMode;
27 import org.openjdk.jmh.annotations.Level;
28 import org.openjdk.jmh.annotations.Mode;
29 import org.openjdk.jmh.annotations.OperationsPerInvocation;
30 import org.openjdk.jmh.annotations.OutputTimeUnit;
31 import org.openjdk.jmh.annotations.Scope;
32 import org.openjdk.jmh.annotations.Setup;
33 import org.openjdk.jmh.annotations.State;
34 import org.openjdk.jmh.infra.Blackhole;
35 
36 import java.util.concurrent.TimeUnit;
37 import java.util.concurrent.atomic.AtomicBoolean;
38 import java.util.concurrent.atomic.AtomicInteger;
39 import java.util.concurrent.atomic.AtomicLong;
40 import java.util.concurrent.atomic.AtomicReference;
41 
42 @BenchmarkMode(Mode.AverageTime)
43 @OutputTimeUnit(TimeUnit.NANOSECONDS)
44 @State(Scope.Benchmark)
45 public class Atomic {
46 
47     public AtomicInteger aInteger;
48     public AtomicLong aLong;
49     public AtomicBoolean aBool;
50 
51     public Object testObject1;
52     public Object testObject2;
53     public AtomicReference<Object> aReference;
54 
55     /**
56      * The test variables are allocated every iteration so you can assume they are initialized to get similar behaviour
57      * across iterations
58      */
59     @Setup(Level.Iteration)
setupIteration()60     public void setupIteration() {
61         testObject1 = new Object();
62         testObject2 = new Object();
63         aInteger = new AtomicInteger(0);
64         aBool = new AtomicBoolean(false);
65         aReference = new AtomicReference<>(testObject1);
66         aLong = new AtomicLong(0);
67     }
68 
69 
70     /** Always swap in value. This test should be compiled into a CAS */
71     @Benchmark
72     @OperationsPerInvocation(2)
testAtomicIntegerAlways(Blackhole bh)73     public void testAtomicIntegerAlways(Blackhole bh) {
74         bh.consume(aInteger.compareAndSet(0, 2));
75         bh.consume(aInteger.compareAndSet(2, 0));
76     }
77 
78     /** Never write a value just return the old one. This test should be compiled into a CAS */
79     @Benchmark
testAtomicIntegerNever(Blackhole bh)80     public void testAtomicIntegerNever(Blackhole bh) {
81         bh.consume(aInteger.compareAndSet(1, 3));
82     }
83 
84     /** Flips an atomic boolean on and off */
85     @Benchmark
86     @OperationsPerInvocation(2)
testAtomicBooleanFlip(Blackhole bh)87     public void testAtomicBooleanFlip(Blackhole bh) {
88         bh.consume(aBool.getAndSet(true));
89         bh.consume(aBool.getAndSet(false));
90     }
91 
92     /** Writes same value over and over */
93     @Benchmark
testAtomicBooleanSame(Blackhole bh)94     public void testAtomicBooleanSame(Blackhole bh) {
95         bh.consume(aBool.getAndSet(true));
96     }
97 
98     /** Increment and get over multiple threads */
99     @Benchmark
testAtomicIntegerGetAndIncrement(Blackhole bh)100     public void testAtomicIntegerGetAndIncrement(Blackhole bh) {
101         bh.consume(aInteger.getAndIncrement());
102     }
103 
104     /** Increment and get over multiple threads */
105     @Benchmark
testAtomicLongGetAndIncrement(Blackhole bh)106     public void testAtomicLongGetAndIncrement(Blackhole bh) {
107         bh.consume(aLong.getAndIncrement());
108     }
109 
110     /** Swap a few references */
111     @Benchmark
112     @OperationsPerInvocation(2)
testAtomicReference(Blackhole bh)113     public void testAtomicReference(Blackhole bh) {
114         bh.consume(aReference.compareAndSet(testObject1, testObject2));
115         bh.consume(aReference.compareAndSet(testObject2, testObject1));
116     }
117 }
118