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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package org.openjdk.bench.vm.lang; 26 27 import org.openjdk.jmh.annotations.Benchmark; 28 import org.openjdk.jmh.annotations.BenchmarkMode; 29 import org.openjdk.jmh.annotations.Mode; 30 import org.openjdk.jmh.annotations.OutputTimeUnit; 31 import org.openjdk.jmh.annotations.Param; 32 import org.openjdk.jmh.annotations.Scope; 33 import org.openjdk.jmh.annotations.Setup; 34 import org.openjdk.jmh.annotations.State; 35 36 import java.util.concurrent.TimeUnit; 37 38 39 /** 40 * Benchmark class for simple lock unlock tests. Nothing big should ever go into this class. 41 */ 42 @BenchmarkMode(Mode.AverageTime) 43 @OutputTimeUnit(TimeUnit.NANOSECONDS) 44 @State(Scope.Benchmark) 45 public class LockUnlock { 46 47 @Param("100") 48 private int innerCount; 49 50 public Object lockObject1; 51 public Object lockObject2; 52 public int factorial; 53 public int dummyInt1; 54 public int dummyInt2; 55 56 @Setup 57 public void setup() { 58 lockObject1 = new Object(); 59 lockObject2 = new Object(); 60 dummyInt1 = 47; 61 dummyInt2 = 11; // anything 62 } 63 64 /** Perform a synchronized on a local object within a loop. */ 65 @Benchmark 66 public void testSimpleLockUnlock() { 67 Object localObject = lockObject1; 68 for (int i = 0; i < innerCount; i++) { 69 synchronized (localObject) { 70 dummyInt1++; 71 dummyInt2++; 72 } 73 } 74 } 75 76 /** Perform a recursive synchronized on a local object within a loop. */ 77 @Benchmark 78 public void testRecursiveLockUnlock() { 79 Object localObject = lockObject1; 80 for (int i = 0; i < innerCount; i++) { 81 synchronized (localObject) { 82 synchronized (localObject) { 83 dummyInt1++; 84 dummyInt2++; 85 } 86 } 87 } 88 } 89 90 /** Perform two synchronized after each other on the same local object. */ 91 @Benchmark 92 public void testSerialLockUnlock() { 93 Object localObject = lockObject1; 94 for (int i = 0; i < innerCount; i++) { 95 synchronized (localObject) { 96 dummyInt1++; 97 } 98 synchronized (localObject) { 99 dummyInt2++; 100 } 101 } 102 } 103 104 /** 105 * Performs recursive synchronizations on the same local object. 106 * <p/> 107 * Result is 3628800 108 */ 109 @Benchmark 110 public void testRecursiveSynchronization() { 111 factorial = fact(10); 112 } 113 114 private synchronized int fact(int n) { 115 if (n == 0) { 116 return 1; 117 } else { 118 return fact(n - 1) * n; 119 } 120 } 121 }