Path: blob/master/test/micro/org/openjdk/bench/vm/lang/LockUnlock.java
41161 views
/*1* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.vm.lang;2324import org.openjdk.jmh.annotations.Benchmark;25import org.openjdk.jmh.annotations.BenchmarkMode;26import org.openjdk.jmh.annotations.Mode;27import org.openjdk.jmh.annotations.OutputTimeUnit;28import org.openjdk.jmh.annotations.Param;29import org.openjdk.jmh.annotations.Scope;30import org.openjdk.jmh.annotations.Setup;31import org.openjdk.jmh.annotations.State;32import org.openjdk.jmh.annotations.Threads;3334import java.util.concurrent.TimeUnit;353637/**38* Benchmark class for simple lock unlock tests. Nothing big should ever go into this class.39*/40@BenchmarkMode(Mode.AverageTime)41@OutputTimeUnit(TimeUnit.NANOSECONDS)42@State(Scope.Benchmark)43public class LockUnlock {4445@Param("100")46private int innerCount;4748public Object lockObject1;49public Object lockObject2;50public int factorial;51public int dummyInt1;52public int dummyInt2;5354@Setup55public void setup() {56lockObject1 = new Object();57lockObject2 = new Object();58dummyInt1 = 47;59dummyInt2 = 11; // anything60}6162/** Perform a synchronized on a local object within a loop. */63@Benchmark64public void testSimpleLockUnlock() {65Object localObject = lockObject1;66for (int i = 0; i < innerCount; i++) {67synchronized (localObject) {68dummyInt1++;69dummyInt2++;70}71}72}7374/** Perform a recursive synchronized on a local object within a loop. */75@Benchmark76public void testRecursiveLockUnlock() {77Object localObject = lockObject1;78for (int i = 0; i < innerCount; i++) {79synchronized (localObject) {80synchronized (localObject) {81dummyInt1++;82dummyInt2++;83}84}85}86}8788/** Perform two synchronized after each other on the same local object. */89@Benchmark90public void testSerialLockUnlock() {91Object localObject = lockObject1;92for (int i = 0; i < innerCount; i++) {93synchronized (localObject) {94dummyInt1++;95}96synchronized (localObject) {97dummyInt2++;98}99}100}101102/**103* Performs recursive synchronizations on the same local object.104* <p/>105* Result is 3628800106*/107@Benchmark108public void testRecursiveSynchronization() {109factorial = fact(10);110}111112/**113* Same as {@link #testRecursiveSynchronization()} but the first call114* to this method will generate the identity hashcode for this object115* which effectively disables biased locking as they occupy the same116* bits in the object header.117*/118@Benchmark119public void testRecursiveSynchronizationNoBias() {120System.identityHashCode(this);121factorial = fact(10);122}123124private synchronized int fact(int n) {125if (n == 0) {126return 1;127} else {128return fact(n - 1) * n;129}130}131132/**133* With two threads lockObject1 will be contended so should be134* inflated.135*/136@Threads(2)137@Benchmark138public void testContendedLock() {139synchronized (lockObject1) {140dummyInt1++;141}142}143}144145146