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 static com.google.common.base.Preconditions.checkState; 20 21 import com.google.caliper.BeforeExperiment; 22 import com.google.caliper.Benchmark; 23 import com.google.caliper.Param; 24 import com.google.common.cache.LocalCache.Segment; 25 import java.util.concurrent.atomic.AtomicReferenceArray; 26 27 /** 28 * Benchmark for {@code LocalCache.Segment.expand()}. 29 * 30 * @author Charles Fry 31 */ 32 public class SegmentBenchmark { 33 34 @Param({"16", "32", "64", "128", "256", "512", "1024", "2048", "4096", "8192"}) 35 int capacity; 36 37 private Segment<Object, Object> segment; 38 39 @BeforeExperiment setUp()40 void setUp() { 41 LocalCache<Object, Object> cache = 42 new LocalCache<>( 43 CacheBuilder.newBuilder().concurrencyLevel(1).initialCapacity(capacity), null); 44 checkState(cache.segments.length == 1); 45 segment = cache.segments[0]; 46 checkState(segment.table.length() == capacity); 47 for (int i = 0; i < segment.threshold; i++) { 48 cache.put(new Object(), new Object()); 49 } 50 checkState(segment.table.length() == capacity); 51 } 52 53 @SuppressWarnings("GuardedBy") 54 @Benchmark time(int reps)55 int time(int reps) { 56 int dummy = 0; 57 AtomicReferenceArray<ReferenceEntry<Object, Object>> oldTable = segment.table; 58 for (int i = 0; i < reps; i++) { 59 // TODO(b/145386688): This access should be guarded by 'this.segment', which is not currently 60 // held 61 segment.expand(); 62 segment.table = oldTable; 63 dummy += segment.count; 64 } 65 return dummy; 66 } 67 } 68