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.Mode; 28 import org.openjdk.jmh.annotations.OutputTimeUnit; 29 import org.openjdk.jmh.annotations.Scope; 30 import org.openjdk.jmh.annotations.Setup; 31 import org.openjdk.jmh.annotations.State; 32 33 import java.util.concurrent.TimeUnit; 34 import java.util.concurrent.atomic.AtomicInteger; 35 import java.util.function.IntUnaryOperator; 36 37 /** 38 * Benchmarks assesses the performance of new Atomic* API. 39 * 40 * Implementation notes: 41 * - atomic instances are padded to eliminate false sharing 42 */ 43 @BenchmarkMode(Mode.AverageTime) 44 @OutputTimeUnit(TimeUnit.NANOSECONDS) 45 @State(Scope.Thread) 46 public class AtomicIntegerUpdateAndGet { 47 48 private PaddedAtomicInteger count; 49 private int value = 42; 50 private IntUnaryOperator captureOp; 51 private IntUnaryOperator noCaptureOp; 52 53 @Setup 54 public void setup() { 55 count = new PaddedAtomicInteger(); 56 noCaptureOp = new IntUnaryOperator() { 57 public int applyAsInt(int v) { 58 return v + 42; 59 } 60 }; 61 captureOp = new IntUnaryOperator() { 62 public int applyAsInt(int v) { 63 return v + value; 64 } 65 }; 66 } 67 68 @Benchmark 69 public int testAddAndGet() { 70 return count.addAndGet(42); 71 } 72 73 @Benchmark 74 public int testInnerNoCapture() { 75 return count.updateAndGet(new IntUnaryOperator() { 76 public int applyAsInt(int v) { 77 return v + 42; 78 } 79 }); 80 } 81 82 @Benchmark 83 public int testInnerCapture() { 84 return count.updateAndGet(new IntUnaryOperator() { 85 public int applyAsInt(int v) { 86 return v + value; 87 } 88 }); 89 } 90 91 @Benchmark 92 public int testInnerCaptureCached() { 93 return count.updateAndGet(captureOp); 94 } 95 96 @Benchmark 97 public int testInnerNoCaptureCached() { 98 return count.updateAndGet(noCaptureOp); 99 } 100 101 @Benchmark 102 public int testLambdaNoCapture() { 103 return count.updateAndGet(x -> x + 42); 104 } 105 106 @Benchmark 107 public int testLambdaCapture() { 108 return count.updateAndGet(x -> x + value); 109 } 110 111 private static class PaddedAtomicInteger extends AtomicInteger { 112 private volatile long pad00, pad01, pad02, pad03, pad04, pad05, pad06, pad07; 113 private volatile long pad10, pad11, pad12, pad13, pad14, pad15, pad16, pad17; 114 } 115 116 }