< prev index next >

test/micro/org/openjdk/bench/java/lang/ScopedValues.java

Print this page

  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 
 24 
 25 package org.openjdk.bench.jdk.incubator.concurrent;
 26 
 27 import jdk.incubator.concurrent.ScopedValue;
 28 import java.util.concurrent.TimeUnit;

 29 import org.openjdk.jmh.annotations.*;
 30 import org.openjdk.jmh.infra.Blackhole;
 31 
 32 import static org.openjdk.bench.jdk.incubator.concurrent.ScopedValuesData.*;
 33 
 34 /**
 35  * Tests ScopedValue
 36  */
 37 @BenchmarkMode(Mode.AverageTime)
 38 @OutputTimeUnit(TimeUnit.MICROSECONDS)
 39 @Warmup(iterations=4, time=1)
 40 @Measurement(iterations=10, time=1)
 41 @Threads(1)
 42 @Fork(value = 1,
 43       jvmArgsPrepend = {"-Djmh.executor.class=org.openjdk.bench.jdk.incubator.concurrent.ScopedValuesExecutorService",
 44                         "-Djmh.executor=CUSTOM",
 45                         "-Djmh.blackhole.mode=COMPILER",
 46                         "--add-modules=jdk.incubator.concurrent",
 47                         "--enable-preview"})
 48 @State(Scope.Thread)
 49 @SuppressWarnings("preview")
 50 public class ScopedValues {
 51 
 52     private static final Integer THE_ANSWER = 42;
 53 
 54     // Test 1: make sure ScopedValue.get() is hoisted out of loops.
 55 
 56     @Benchmark
 57     public void thousandAdds_ScopedValue(Blackhole bh) throws Exception {
 58         int result = 0;
 59         for (int i = 0; i < 1_000; i++) {
 60             result += ScopedValuesData.sl1.get();
 61         }
 62         bh.consume(result);
 63     }
 64 
 65     @Benchmark
 66     public void thousandAdds_ThreadLocal(Blackhole bh) throws Exception {

144     @OutputTimeUnit(TimeUnit.NANOSECONDS)
145     public int bindThenGetThenRemove_ThreadLocal() throws Exception {
146         try {
147             tl1.set(THE_ANSWER);
148             return tl1.get();
149         } finally {
150             tl1.remove();
151         }
152     }
153 
154     // This has no exact equivalent in ScopedValue, but it's provided here for
155     // information.
156     @Benchmark
157     @OutputTimeUnit(TimeUnit.NANOSECONDS)
158     public int bindThenGetNoRemove_ThreadLocal() throws Exception {
159         tl1.set(THE_ANSWER);
160         return tl1.get();
161     }
162 
163     // Test 4: The cost of binding, but not using any result
164 
165     @Benchmark
166     @OutputTimeUnit(TimeUnit.NANOSECONDS)
167     public Object bind_ScopedValue() throws Exception {
168         return HOLD_42.call(this::getClass);









169     }

170 
171     @Benchmark
172     @OutputTimeUnit(TimeUnit.NANOSECONDS)
173     public Object bind_ThreadLocal() throws Exception {
174         try {
175             tl1.set(THE_ANSWER);
176             return this.getClass();
177         } finally {
178             tl1.remove();
179         }
180     }
181 
182     // Simply set a ThreadLocal so that the caller can see it
183     // This has no exact equivalent in ScopedValue, but it's provided here for
184     // information.
185     @Benchmark
186     @OutputTimeUnit(TimeUnit.NANOSECONDS)
187     public void setNoRemove_ThreadLocal() throws Exception {
188         tl1.set(THE_ANSWER);
189     }

  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 
 24 
 25 package org.openjdk.bench.java.lang;
 26 
 27 import java.util.concurrent.Callable;
 28 import java.util.concurrent.TimeUnit;
 29 import java.util.function.Supplier;
 30 import org.openjdk.jmh.annotations.*;
 31 import org.openjdk.jmh.infra.Blackhole;
 32 
 33 import static org.openjdk.bench.java.lang.ScopedValuesData.*;
 34 
 35 /**
 36  * Tests ScopedValue
 37  */
 38 @BenchmarkMode(Mode.AverageTime)
 39 @OutputTimeUnit(TimeUnit.MICROSECONDS)
 40 @Warmup(iterations=4, time=1)
 41 @Measurement(iterations=10, time=1)
 42 @Threads(1)
 43 @Fork(value = 1,
 44       jvmArgsPrepend = {"-Djmh.executor.class=org.openjdk.bench.java.lang.ScopedValuesExecutorService",
 45                         "-Djmh.executor=CUSTOM",
 46                         "-Djmh.blackhole.mode=COMPILER",

 47                         "--enable-preview"})
 48 @State(Scope.Thread)
 49 @SuppressWarnings("preview")
 50 public class ScopedValues {
 51 
 52     private static final Integer THE_ANSWER = 42;
 53 
 54     // Test 1: make sure ScopedValue.get() is hoisted out of loops.
 55 
 56     @Benchmark
 57     public void thousandAdds_ScopedValue(Blackhole bh) throws Exception {
 58         int result = 0;
 59         for (int i = 0; i < 1_000; i++) {
 60             result += ScopedValuesData.sl1.get();
 61         }
 62         bh.consume(result);
 63     }
 64 
 65     @Benchmark
 66     public void thousandAdds_ThreadLocal(Blackhole bh) throws Exception {

144     @OutputTimeUnit(TimeUnit.NANOSECONDS)
145     public int bindThenGetThenRemove_ThreadLocal() throws Exception {
146         try {
147             tl1.set(THE_ANSWER);
148             return tl1.get();
149         } finally {
150             tl1.remove();
151         }
152     }
153 
154     // This has no exact equivalent in ScopedValue, but it's provided here for
155     // information.
156     @Benchmark
157     @OutputTimeUnit(TimeUnit.NANOSECONDS)
158     public int bindThenGetNoRemove_ThreadLocal() throws Exception {
159         tl1.set(THE_ANSWER);
160         return tl1.get();
161     }
162 
163     // Test 4: The cost of binding, but not using any result

164     @Benchmark
165     @OutputTimeUnit(TimeUnit.NANOSECONDS)
166     public Object bind_ScopedValue() throws Exception {
167         return HOLD_42.call(aCallable);
168     }
169     private static final Callable<Class<?>> aCallable = () -> ScopedValues.class;
170 
171     // Same, but make sure that Carrier.get(Supplier) is no slower
172     // than Carrier.call(Callable).
173     @Benchmark
174     @OutputTimeUnit(TimeUnit.NANOSECONDS)
175     public Object bindViaGet_ScopedValue() {
176         return HOLD_42.get(aSupplier);
177     }
178     private static final Supplier<Class<?>> aSupplier = () -> ScopedValues.class;
179 
180     @Benchmark
181     @OutputTimeUnit(TimeUnit.NANOSECONDS)
182     public Object bind_ThreadLocal() throws Exception {
183         try {
184             tl1.set(THE_ANSWER);
185             return this.getClass();
186         } finally {
187             tl1.remove();
188         }
189     }
190 
191     // Simply set a ThreadLocal so that the caller can see it
192     // This has no exact equivalent in ScopedValue, but it's provided here for
193     // information.
194     @Benchmark
195     @OutputTimeUnit(TimeUnit.NANOSECONDS)
196     public void setNoRemove_ThreadLocal() throws Exception {
197         tl1.set(THE_ANSWER);
198     }
< prev index next >