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.cache;
18 
19 import com.google.caliper.BeforeExperiment;
20 import com.google.caliper.Benchmark;
21 import com.google.caliper.Param;
22 import com.google.common.cache.LocalCache.Segment;
23 
24 /**
25  * Benchmark for {@code LocalCache.Segment.removeEntryFromChain}.
26  *
27  * @author Charles Fry
28  */
29 public class ChainBenchmark {
30 
31   @Param({"1", "2", "3", "4", "5", "6"})
32   int length;
33 
34   private Segment<Object, Object> segment;
35   private ReferenceEntry<Object, Object> head;
36   private ReferenceEntry<Object, Object> chain;
37 
38   @SuppressWarnings("GuardedBy")
39   @BeforeExperiment
setUp()40   void setUp() {
41     LocalCache<Object, Object> cache =
42         new LocalCache<>(CacheBuilder.newBuilder().concurrencyLevel(1), null);
43     segment = cache.segments[0];
44     chain = null;
45     for (int i = 0; i < length; i++) {
46       Object key = new Object();
47       // TODO(b/145386688): This access should be guarded by 'this.segment', which is not currently
48       // held
49       chain = segment.newEntry(key, cache.hash(key), chain);
50       if (i == 0) {
51         head = chain;
52       }
53     }
54   }
55 
56   @SuppressWarnings("GuardedBy")
57   @Benchmark
time(int reps)58   int time(int reps) {
59     int dummy = 0;
60     for (int i = 0; i < reps; i++) {
61       // TODO(b/145386688): This access should be guarded by 'this.segment', which is not currently
62       // held
63       segment.removeEntryFromChain(chain, head);
64       dummy += segment.count;
65     }
66     return dummy;
67   }
68 }
69