1 /*
  2  * Copyright (c) 2018, Red Hat, Inc. 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.valhalla.sandbox.corelibs.corelibs.mapprotos;
 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.Mode;
 30 import org.openjdk.jmh.annotations.OutputTimeUnit;
 31 import org.openjdk.jmh.annotations.Param;
 32 import org.openjdk.jmh.annotations.Scope;
 33 import org.openjdk.jmh.annotations.Setup;
 34 import org.openjdk.jmh.annotations.State;
 35 
 36 import java.util.HashMap;
 37 import java.util.LinkedHashMap;
 38 import java.util.Map;
 39 import java.util.concurrent.ThreadLocalRandom;
 40 import java.util.concurrent.TimeUnit;
 41 import java.util.function.IntFunction;
 42 import java.util.stream.IntStream;
 43 
 44 import static java.util.stream.Collectors.toMap;
 45 
 46 /**
 47  * Benchmark                                    (mapType)   (size)  Mode  Cnt    Score    Error  Units
 48  * XHashMapBench.put                             HASH_MAP  1000000  avgt    5  214.470 +/- 44.063  ms/op
 49  * XHashMapBench.put                            XHASH_MAP  1000000  avgt    5  215.772 +/- 31.595  ms/op
 50  * XHashMapBench.putAllWithBigMapToEmptyMap      HASH_MAP  1000000  avgt    5  126.472 +/- 38.452  ms/op
 51  * XHashMapBench.putAllWithBigMapToEmptyMap     XHASH_MAP  1000000  avgt    5  117.741 +/- 10.460  ms/op
 52  * XHashMapBench.putAllWithBigMapToNonEmptyMap   HASH_MAP  1000000  avgt    5  136.112 +/- 36.712  ms/op
 53  * XHashMapBench.putAllWithBigMapToNonEmptyMap  XHASH_MAP  1000000  avgt    5  144.681 +/-  8.755  ms/op
 54  * Finished running test 'micro:valhalla.corelibs.XHashMapBench'
 55  */
 56 @BenchmarkMode(Mode.AverageTime)
 57 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 58 @Fork(1)
 59 @State(Scope.Thread)
 60 public class HashMapBench {
 61     private IntFunction<Map<Integer, Integer>> mapSupplier;
 62     private Map<Integer, Integer> bigMapToAdd;
 63 
 64     @Param("1000000")
 65     private int size;
 66 
 67     @Param(value = {
 68             "org.openjdk.bench.valhalla.corelibs.mapprotos.HashMap",
 69 //            "org.openjdk.bench.valhalla.corelibs.mapprotos.XHashMap",
 70             "java.util.HashMap",
 71         })
 72     private String mapType;
 73 
 74     @Setup
 75     public void setup() {
 76         try {
 77             Class<?> mapClass = Class.forName(mapType);
 78             mapSupplier =  (size) -> newInstance(mapClass, size);
 79         } catch (Exception ex) {
 80             System.out.printf("%s: %s%n", mapType, ex.getMessage());
 81             return;
 82         }
 83 
 84         ThreadLocalRandom rnd = ThreadLocalRandom.current();
 85         this.bigMapToAdd = IntStream.range(0, size).boxed()
 86             .collect(toMap(i -> 7 + i * 128, i -> rnd.nextInt()));
 87     }
 88 
 89     Map<Integer, Integer> newInstance(Class<?> mapClass, int size) {
 90         try {
 91             return (Map<Integer, Integer>)mapClass.getConstructor(int.class).newInstance(size);
 92         } catch (Exception ex) {
 93             throw new RuntimeException("failed", ex);
 94         }
 95     }
 96 
 97     @Benchmark
 98     public int putAllWithBigMapToNonEmptyMap() {
 99         Map<Integer, Integer> map = mapSupplier.apply(16);
100         map.put(-1, -1);
101         map.putAll(bigMapToAdd);
102         return map.size();
103     }
104 
105     @Benchmark
106     public int putAllWithBigMapToEmptyMap() {
107         Map<Integer, Integer> map = mapSupplier.apply(16);
108         map.putAll(bigMapToAdd);
109         return map.size();
110     }
111 
112     @Benchmark
113     public int put() {
114         Map<Integer, Integer> map = mapSupplier.apply(16);
115         for (int k : bigMapToAdd.keySet()) {
116             map.put(k, bigMapToAdd.get(k));
117         }
118         return map.size();
119     }
120 }