1 /* 2 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package org.openjdk.bench.vm.compiler.pea; 25 26 import org.openjdk.jmh.annotations.Benchmark; 27 import org.openjdk.jmh.annotations.BenchmarkMode; 28 import org.openjdk.jmh.annotations.Fork; 29 import org.openjdk.jmh.annotations.Measurement; 30 import org.openjdk.jmh.annotations.Mode; 31 import org.openjdk.jmh.annotations.Param; 32 import org.openjdk.jmh.annotations.Setup; 33 import org.openjdk.jmh.annotations.Scope; 34 import org.openjdk.jmh.annotations.State; 35 import org.openjdk.jmh.annotations.OutputTimeUnit; 36 import org.openjdk.jmh.annotations.Warmup; 37 38 import java.util.concurrent.TimeUnit; 39 import java.util.ArrayList; 40 import java.util.HashMap; 41 42 @BenchmarkMode(Mode.AverageTime) 43 @OutputTimeUnit(TimeUnit.NANOSECONDS) 44 @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) 45 @Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS) 46 @State(Scope.Benchmark) 47 @Fork(value = 3) 48 public class HashMapBench { 49 class Key { 50 int x; 51 Key(int x) { this.x = x; } 52 public int hashCode() { return x; } 53 public boolean equals(Object other) { return this.hashCode() == other.hashCode(); } 54 } 55 56 static void blackhole(Object o) {} 57 58 @Param("1024") 59 private int size; 60 61 @Param({"1", "2", "4"}) 62 private int fillInterval; 63 64 ArrayList<Key> keys; 65 HashMap<Key, Object> map; 66 67 @Setup 68 public void setUp() { 69 keys = new ArrayList<>(); 70 map = new HashMap<>(); 71 for (int i = 0; i < size; ++i) { 72 keys.add(new Key(i)); 73 if (i % fillInterval == 0) { 74 map.put(new Key(i), new Object()); 75 } 76 } 77 } 78 79 @Benchmark 80 public void replace() { 81 for (int i = 0; i < size; ++i) { 82 map.replace(keys.get(i), new Object()); 83 } 84 } 85 86 @Benchmark 87 public void computeIfAbsent() { 88 for (int i = 0; i < size; ++i) { 89 map.computeIfAbsent(new Key(i), key -> new Object()); 90 } 91 } 92 93 @Benchmark 94 public void cacheUpdate() { 95 for (int i = 0; i < size; ++i) { 96 Key key = new Key(i); 97 if (!map.containsKey(key)) { 98 map.put(key, new Object()); 99 } 100 } 101 } 102 } 103