1 /* 2 * Copyright (C) 2010 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.AfterExperiment; 20 import com.google.caliper.BeforeExperiment; 21 import com.google.caliper.Benchmark; 22 import com.google.caliper.Param; 23 import com.google.common.primitives.Ints; 24 import java.util.Random; 25 import java.util.concurrent.atomic.AtomicLong; 26 27 /** 28 * Single-threaded benchmark for {@link LoadingCache}. 29 * 30 * @author Charles Fry 31 */ 32 public class LoadingCacheSingleThreadBenchmark { 33 @Param({"1000", "2000"}) 34 int maximumSize; 35 36 @Param("5000") 37 int distinctKeys; 38 39 @Param("4") 40 int segments; 41 42 // 1 means uniform likelihood of keys; higher means some keys are more popular 43 // tweak this to control hit rate 44 @Param("2.5") 45 double concentration; 46 47 Random random = new Random(); 48 49 LoadingCache<Integer, Integer> cache; 50 51 int max; 52 53 static AtomicLong requests = new AtomicLong(0); 54 static AtomicLong misses = new AtomicLong(0); 55 56 @BeforeExperiment setUp()57 void setUp() { 58 // random integers will be generated in this range, then raised to the 59 // power of (1/concentration) and floor()ed 60 max = Ints.checkedCast((long) Math.pow(distinctKeys, concentration)); 61 62 cache = 63 CacheBuilder.newBuilder() 64 .concurrencyLevel(segments) 65 .maximumSize(maximumSize) 66 .build( 67 new CacheLoader<Integer, Integer>() { 68 @Override 69 public Integer load(Integer from) { 70 return (int) misses.incrementAndGet(); 71 } 72 }); 73 74 // To start, fill up the cache. 75 // Each miss both increments the counter and causes the map to grow by one, 76 // so until evictions begin, the size of the map is the greatest return 77 // value seen so far 78 while (cache.getUnchecked(nextRandomKey()) < maximumSize) {} 79 80 requests.set(0); 81 misses.set(0); 82 } 83 84 @Benchmark time(int reps)85 int time(int reps) { 86 int dummy = 0; 87 for (int i = 0; i < reps; i++) { 88 dummy += cache.getUnchecked(nextRandomKey()); 89 } 90 requests.addAndGet(reps); 91 return dummy; 92 } 93 nextRandomKey()94 private int nextRandomKey() { 95 int a = random.nextInt(max); 96 97 /* 98 * For example, if concentration=2.0, the following takes the square root of 99 * the uniformly-distributed random integer, then truncates any fractional 100 * part, so higher integers would appear (in this case linearly) more often 101 * than lower ones. 102 */ 103 return (int) Math.pow(a, 1.0 / concentration); 104 } 105 106 @AfterExperiment tearDown()107 void tearDown() { 108 double req = requests.get(); 109 double hit = req - misses.get(); 110 111 // Currently, this is going into /dev/null, but I'll fix that 112 System.out.println("hit rate: " + hit / req); 113 } 114 115 // for proper distributions later: 116 // import JSci.maths.statistics.ProbabilityDistribution; 117 // int key = (int) dist.inverse(random.nextDouble()); 118 } 119