1 /*
  2  * Copyright (c) 2016, 2019, Oracle and/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 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 org.openjdk.jmh.infra.Blackhole;
 36 
 37 import java.util.Arrays;
 38 import java.util.Collections;
 39 import java.util.HashMap;
 40 import java.util.Map;
 41 import java.util.function.IntFunction;
 42 import java.util.concurrent.TimeUnit;
 43 
 44 @State(Scope.Thread)
 45 public class GetX extends MapBase {
 46 
 47     IntFunction<Map<Integer, Integer>> mapSupplier;
 48     Map<Integer, Integer> map;
 49     Integer[] mixed;
 50 
 51     @Param(value = {"org.openjdk.bench.valhalla.corelibs.mapprotos.YHashMap",
 52             "org.openjdk.bench.valhalla.corelibs.mapprotos.XHashMap",
 53             "java.util.HashMap"})
 54     private String mapType;
 55 
 56     @Setup
 57     public void setup() {
 58         super.init(size);
 59         try {
 60             Class<?> mapClass = Class.forName(mapType);
 61             mapSupplier =  (size) -> newInstance(mapClass, size);
 62         } catch (Exception ex) {
 63             System.out.printf("%s: %s%n", mapType, ex.getMessage());
 64             return;
 65         }
 66 
 67         map = mapSupplier.apply(0);
 68         for (Integer k : keys) {
 69             map.put(k, k);
 70         }
 71 
 72         mixed = new Integer[size];
 73         System.arraycopy(keys, 0, mixed, 0, size / 2);
 74         System.arraycopy(nonKeys, 0, mixed, size / 2, size / 2);
 75         Collections.shuffle(Arrays.asList(mixed), rnd);
 76     }
 77 
 78     Map<Integer, Integer> newInstance(Class<?> mapClass, int size) {
 79         try {
 80             return (Map<Integer, Integer>)mapClass.getConstructor(int.class).newInstance(size);
 81         } catch (Exception ex) {
 82             throw new RuntimeException("failed", ex);
 83         }
 84     }
 85 
 86     @Benchmark
 87     public void getHit(Blackhole bh) {
 88         Integer[] keys = this.keys;
 89         Map<Integer, Integer> map = this.map;
 90         for (Integer k : keys) {
 91             bh.consume(map.get(k));
 92         }
 93     }
 94 
 95     @Benchmark
 96     public void getMix(Blackhole bh) {
 97         Integer[] keys = this.mixed;
 98         Map<Integer, Integer> map = this.map;
 99         for (Integer k : keys) {
100             bh.consume(map.get(k));
101         }
102     }
103 
104 }