Path: blob/master/test/micro/org/openjdk/bench/vm/lang/MonitorBench.java
41241 views
/*1* Copyright (c) 2020, 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 java.util.concurrent.ThreadLocalRandom;25import java.util.concurrent.TimeUnit;2627import org.openjdk.jmh.annotations.Benchmark;28import org.openjdk.jmh.annotations.BenchmarkMode;29import org.openjdk.jmh.annotations.Mode;30import org.openjdk.jmh.annotations.OutputTimeUnit;31import org.openjdk.jmh.annotations.Param;32import org.openjdk.jmh.annotations.Scope;33import org.openjdk.jmh.annotations.Setup;34import org.openjdk.jmh.annotations.State;35import org.openjdk.jmh.annotations.Threads;36import org.openjdk.jmh.infra.Blackhole;3738@State(Scope.Benchmark)39@Threads(Threads.MAX)40public class MonitorBench {4142@Param({"100", "250"})43int consumeUnlocked;4445@Param({"100", "250"})46int consumeLocked;4748@Param({"0", "1"})49int throwThreshold;5051@Param({"10000"})52int range;5354@Param({"1"})55static int locksSize;5657Object[] sharedLocks;5859@Setup60public void setup() {61sharedLocks = new Object[locksSize];62for (int i = 0; i < locksSize; i++) {63sharedLocks[i] = new Object();64}65}6667int update2(int sharedIndex) throws Exception {68synchronized (sharedLocks[sharedIndex]) {69Blackhole.consumeCPU(consumeLocked);70if (ThreadLocalRandom.current().nextInt(range) < throwThreshold) {71throw new Exception("Update failed");72} else {73Blackhole.consumeCPU(consumeLocked);74return 0;75}76}77}7879int update1(int sharedIndex) throws Exception {80synchronized (sharedLocks[sharedIndex]) {81Blackhole.consumeCPU(consumeLocked);82return update2(sharedIndex);83}84}8586@Benchmark87@BenchmarkMode(Mode.Throughput)88@OutputTimeUnit(TimeUnit.MILLISECONDS)89public int action() throws InterruptedException {90Blackhole.consumeCPU(consumeUnlocked);91int sharedLockIndex = ThreadLocalRandom.current().nextInt(locksSize);92Object sharedLock = sharedLocks[sharedLockIndex];93synchronized (sharedLock) {94while (true) {95try {96return update1(sharedLockIndex);97} catch (Exception e) {98sharedLock.wait(100);99}100}101}102}103}104105106