1 /*
  2  * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
  3  *
  4  * This code is free software; you can redistribute it and/or modify it
  5  * under the terms of the GNU General Public License version 2 only, as
  6  * published by the Free Software Foundation.
  7  *
  8  * This code is distributed in the hope that it will be useful, but WITHOUT
  9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 11  * version 2 for more details (a copy is included in the LICENSE file that
 12  * accompanied this code).
 13  *
 14  * You should have received a copy of the GNU General Public License version
 15  * 2 along with this work; if not, write to the Free Software Foundation,
 16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 17  *
 18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 19  * or visit www.oracle.com if you need additional information or have any
 20  * questions.
 21  */
 22 
 23 package org.openjdk.bench.valhalla.corelibs.mapprotos;
 24 
 25 import org.openjdk.jmh.annotations.Benchmark;
 26 import org.openjdk.jmh.annotations.BenchmarkMode;
 27 import org.openjdk.jmh.annotations.Fork;
 28 import org.openjdk.jmh.annotations.Mode;
 29 import org.openjdk.jmh.annotations.OutputTimeUnit;
 30 import org.openjdk.jmh.annotations.Param;
 31 import org.openjdk.jmh.annotations.Scope;
 32 import org.openjdk.jmh.annotations.Setup;
 33 import org.openjdk.jmh.annotations.State;
 34 
 35 import java.util.HashMap;
 36 import java.util.LinkedHashMap;
 37 import java.util.Map;
 38 import java.util.concurrent.ThreadLocalRandom;
 39 import java.util.concurrent.TimeUnit;
 40 import java.util.function.IntFunction;
 41 import java.util.stream.IntStream;
 42 
 43 import static java.util.stream.Collectors.toMap;
 44 
 45 /**
 46  * Benchmark                                    (mapType)   (size)  Mode  Cnt    Score    Error  Units
 47  * XHashMapBench.put                             HASH_MAP  1000000  avgt    5  214.470 +/- 44.063  ms/op
 48  * XHashMapBench.put                            XHASH_MAP  1000000  avgt    5  215.772 +/- 31.595  ms/op
 49  * XHashMapBench.putAllWithBigMapToEmptyMap      HASH_MAP  1000000  avgt    5  126.472 +/- 38.452  ms/op
 50  * XHashMapBench.putAllWithBigMapToEmptyMap     XHASH_MAP  1000000  avgt    5  117.741 +/- 10.460  ms/op
 51  * XHashMapBench.putAllWithBigMapToNonEmptyMap   HASH_MAP  1000000  avgt    5  136.112 +/- 36.712  ms/op
 52  * XHashMapBench.putAllWithBigMapToNonEmptyMap  XHASH_MAP  1000000  avgt    5  144.681 +/-  8.755  ms/op
 53  * Finished running test 'micro:valhalla.corelibs.XHashMapBench'
 54  */
 55 @BenchmarkMode(Mode.AverageTime)
 56 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 57 @Fork(1)
 58 @State(Scope.Thread)
 59 public class HashMapBench {
 60     private IntFunction<Map<Integer, Integer>> mapSupplier;
 61     private Map<Integer, Integer> bigMapToAdd;
 62 
 63     @Param("1000000")
 64     private int size;
 65 
 66     @Param(value = {"org.openjdk.bench.valhalla.corelibs.mapprotos.YHashMap",
 67             "org.openjdk.bench.valhalla.corelibs.mapprotos.XHashMap",
 68             "java.util.HashMap"})
 69     private String mapType;
 70 
 71     @Setup
 72     public void setup() {
 73         try {
 74             Class<?> mapClass = Class.forName(mapType);
 75             mapSupplier =  (size) -> newInstance(mapClass, size);
 76         } catch (Exception ex) {
 77             System.out.printf("%s: %s%n", mapType, ex.getMessage());
 78             return;
 79         }
 80 
 81         ThreadLocalRandom rnd = ThreadLocalRandom.current();
 82         this.bigMapToAdd = IntStream.range(0, size).boxed()
 83             .collect(toMap(i -> 7 + i * 128, i -> rnd.nextInt()));
 84     }
 85 
 86     Map<Integer, Integer> newInstance(Class<?> mapClass, int size) {
 87         try {
 88             return (Map<Integer, Integer>)mapClass.getConstructor(int.class).newInstance(size);
 89         } catch (Exception ex) {
 90             throw new RuntimeException("failed", ex);
 91         }
 92     }
 93 
 94     @Benchmark
 95     public int putAllWithBigMapToNonEmptyMap() {
 96         Map<Integer, Integer> map = mapSupplier.apply(16);
 97         map.put(-1, -1);
 98         map.putAll(bigMapToAdd);
 99         return map.size();
100     }
101 
102     @Benchmark
103     public int putAllWithBigMapToEmptyMap() {
104         Map<Integer, Integer> map = mapSupplier.apply(16);
105         map.putAll(bigMapToAdd);
106         return map.size();
107     }
108 
109     @Benchmark
110     public int put() {
111         Map<Integer, Integer> map = mapSupplier.apply(16);
112         for (int k : bigMapToAdd.keySet()) {
113             map.put(k, bigMapToAdd.get(k));
114         }
115         return map.size();
116     }
117 }