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.vm.lang;
  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.Param;
  30 import org.openjdk.jmh.annotations.Scope;
  31 import org.openjdk.jmh.annotations.Setup;
  32 import org.openjdk.jmh.annotations.State;
  33 
  34 import java.util.concurrent.TimeUnit;
  35 
  36 
  37 /**
  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)
  43 public class LockUnlock {
  44 
  45     @Param("100")
  46     private int innerCount;
  47 
  48     public Object lockObject1;
  49     public Object lockObject2;
  50     public int factorial;
  51     public int dummyInt1;
  52     public int dummyInt2;
  53 
  54     @Setup
  55     public void setup() {
  56         lockObject1 = new Object();
  57         lockObject2 = new Object();
  58         dummyInt1 = 47;
  59         dummyInt2 = 11; // anything
  60     }
  61 
  62     /** Perform a synchronized on a local object within a loop. */
  63     @Benchmark
  64     public void testSimpleLockUnlock() {
  65         Object localObject = lockObject1;
  66         for (int i = 0; i < innerCount; i++) {
  67             synchronized (localObject) {
  68                 dummyInt1++;
  69                 dummyInt2++;
  70             }
  71         }
  72     }
  73 
  74     /** Perform a recursive synchronized on a local object within a loop. */
  75     @Benchmark
  76     public void testRecursiveLockUnlock() {
  77         Object localObject = lockObject1;
  78         for (int i = 0; i < innerCount; i++) {
  79             synchronized (localObject) {
  80                 synchronized (localObject) {
  81                     dummyInt1++;
  82                     dummyInt2++;
  83                 }
  84             }
  85         }
  86     }
  87 
  88     /** Perform two synchronized after each other on the same local object. */
  89     @Benchmark
  90     public void testSerialLockUnlock() {
  91         Object localObject = lockObject1;
  92         for (int i = 0; i < innerCount; i++) {
  93             synchronized (localObject) {
  94                 dummyInt1++;
  95             }
  96             synchronized (localObject) {
  97                 dummyInt2++;
  98             }
  99         }
 100     }
 101 
 102     /**
 103      * Performs recursive synchronizations on the same local object.
 104      * <p/>
 105      * Result is 3628800
 106      */
 107     @Benchmark
 108     public void testRecursiveSynchronization() {
 109         factorial = fact(10);
 110     }
 111 
 112     private synchronized int fact(int n) {
 113         if (n == 0) {
 114             return 1;
 115         } else {
 116             return fact(n - 1) * n;
 117         }
 118     }
 119 }