1 /* 2 * Copyright (C) 2012 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.common.collect.MapMaker; 22 import java.util.Map; 23 24 /** 25 * Compare CacheBuilder and MapMaker performance, ensuring that they remain on par with each other. 26 * 27 * @author Nikita Sidorov 28 */ 29 public class MapMakerComparisonBenchmark { 30 private static final String TEST_KEY = "test key"; 31 private static final String TEST_VALUE = "test value"; 32 33 // Non-loading versions: 34 private final Map<Object, Object> map = new MapMaker().makeMap(); // Returns ConcurrentHashMap 35 private final Cache<Object, Object> cache = CacheBuilder.newBuilder().recordStats().build(); 36 private final Cache<Object, Object> cacheNoStats = CacheBuilder.newBuilder().build(); 37 38 @BeforeExperiment setUp()39 void setUp() { 40 map.put(TEST_KEY, TEST_VALUE); 41 cache.put(TEST_KEY, TEST_VALUE); 42 cacheNoStats.put(TEST_KEY, TEST_VALUE); 43 } 44 45 @Benchmark concurrentHashMap(int rep)46 void concurrentHashMap(int rep) { 47 for (int i = 0; i < rep; i++) { 48 map.get(TEST_KEY); 49 } 50 } 51 52 @Benchmark cacheBuilder_stats(int rep)53 void cacheBuilder_stats(int rep) { 54 for (int i = 0; i < rep; i++) { 55 cache.getIfPresent(TEST_KEY); 56 } 57 } 58 59 @Benchmark cacheBuilder(int rep)60 void cacheBuilder(int rep) { 61 for (int i = 0; i < rep; i++) { 62 cacheNoStats.getIfPresent(TEST_KEY); 63 } 64 } 65 } 66