• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.util.concurrent;
18 
19 import com.google.caliper.BeforeExperiment;
20 import com.google.caliper.Benchmark;
21 import com.google.caliper.Param;
22 import java.util.concurrent.locks.Lock;
23 import java.util.concurrent.locks.ReentrantLock;
24 
25 /**
26  * Benchmarks for {@link CycleDetectingLockFactory}.
27  *
28  * @author Darick Tong
29  */
30 public class CycleDetectingLockFactoryBenchmark {
31 
32   @Param({"2", "3", "4", "5", "10"})
33   int lockNestingDepth;
34 
35   CycleDetectingLockFactory factory;
36   private Lock[] plainLocks;
37   private Lock[] detectingLocks;
38 
39   @BeforeExperiment
setUp()40   void setUp() throws Exception {
41     this.factory = CycleDetectingLockFactory.newInstance(CycleDetectingLockFactory.Policies.WARN);
42     this.plainLocks = new Lock[lockNestingDepth];
43     for (int i = 0; i < lockNestingDepth; i++) {
44       plainLocks[i] = new ReentrantLock();
45     }
46     this.detectingLocks = new Lock[lockNestingDepth];
47     for (int i = 0; i < lockNestingDepth; i++) {
48       detectingLocks[i] = factory.newReentrantLock("Lock" + i);
49     }
50   }
51 
52   @Benchmark
unorderedPlainLocks(int reps)53   void unorderedPlainLocks(int reps) {
54     lockAndUnlock(new ReentrantLock(), reps);
55   }
56 
57   @Benchmark
unorderedCycleDetectingLocks(int reps)58   void unorderedCycleDetectingLocks(int reps) {
59     lockAndUnlock(factory.newReentrantLock("foo"), reps);
60   }
61 
lockAndUnlock(Lock lock, int reps)62   private static void lockAndUnlock(Lock lock, int reps) {
63     for (int i = 0; i < reps; i++) {
64       lock.lock();
65       lock.unlock();
66     }
67   }
68 
69   @Benchmark
orderedPlainLocks(int reps)70   void orderedPlainLocks(int reps) {
71     lockAndUnlockNested(plainLocks, reps);
72   }
73 
74   @Benchmark
orderedCycleDetectingLocks(int reps)75   void orderedCycleDetectingLocks(int reps) {
76     lockAndUnlockNested(detectingLocks, reps);
77   }
78 
lockAndUnlockNested(Lock[] locks, int reps)79   private static void lockAndUnlockNested(Lock[] locks, int reps) {
80     for (int i = 0; i < reps; i++) {
81       for (int j = 0; j < locks.length; j++) {
82         locks[j].lock();
83       }
84       for (int j = locks.length - 1; j >= 0; j--) {
85         locks[j].unlock();
86       }
87     }
88   }
89 }
90