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;
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 }
|
1 /*
2 * Copyright (c) 2014, 2019, 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 import org.openjdk.jmh.annotations.Threads;
34
35 import java.util.concurrent.TimeUnit;
36
37
38 /**
39 * Benchmark class for simple lock unlock tests. Nothing big should ever go into this class.
40 */
41 @BenchmarkMode(Mode.AverageTime)
42 @OutputTimeUnit(TimeUnit.NANOSECONDS)
43 @State(Scope.Benchmark)
44 public class LockUnlock {
45
46 @Param("100")
47 private int innerCount;
48
49 public Object lockObject1;
50 public Object lockObject2;
51 public int factorial;
52 public int dummyInt1;
53 public int dummyInt2;
98 dummyInt2++;
99 }
100 }
101 }
102
103 /**
104 * Performs recursive synchronizations on the same local object.
105 * <p/>
106 * Result is 3628800
107 */
108 @Benchmark
109 public void testRecursiveSynchronization() {
110 factorial = fact(10);
111 }
112
113 private synchronized int fact(int n) {
114 if (n == 0) {
115 return 1;
116 } else {
117 return fact(n - 1) * n;
118 }
119 }
120
121 /**
122 * With two threads lockObject1 will be contended so should be
123 * inflated.
124 */
125 @Threads(2)
126 @Benchmark
127 public void testContendedLock() {
128 synchronized (lockObject1) {
129 dummyInt1++;
130 }
131 }
132 }
|