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.sandbox.corelibs.corelibs.mapprotos;
24 
25 import org.openjdk.jmh.annotations.Benchmark;
26 import org.openjdk.jmh.annotations.Setup;
27 import org.openjdk.jmh.annotations.TearDown;
28 
29 import org.openjdk.jmh.infra.Blackhole;
30 
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.Map;
35 import java.util.function.IntFunction;
36 
37 public class GetX extends MapBase {
38 
39     IntFunction<Map<Integer, Integer>> mapSupplier;
40     Map<Integer, Integer> map;
41     Integer[] mixed;
42 
43     @Setup
44     public void setup() {
45         super.init(size);
46         try {
47             Class<?> mapClass = Class.forName(mapType);
48             mapSupplier =  (s) -> newInstance(mapClass, s);
49         } catch (Exception ex) {
50             System.out.printf("%s: %s%n", mapType, ex.getMessage());
51             return;
52         }
53 
54         map = mapSupplier.apply(size);
55         for (Integer k : keys) {
56             map.put(k, k);
57         }
58 
59         mixed = new Integer[size];
60         System.arraycopy(keys, 0, mixed, 0, size / 2);
61         System.arraycopy(nonKeys, 0, mixed, size / 2, size / 2);
62         Collections.shuffle(Arrays.asList(mixed), rnd);
63     }
64 
65     @TearDown
66     public void teardown() {
67         super.TearDown(map);
68     }
69 
70     Map<Integer, Integer> newInstance(Class<?> mapClass, int size) {
71         try {
72             return (Map<Integer, Integer>)mapClass.getConstructor(int.class).newInstance(size);
73         } catch (Exception ex) {
74             throw new RuntimeException("failed", ex);
75         }
76     }
77 
78     @Benchmark
79     public void getHit(Blackhole bh) {
80         Integer[] keys = this.keys;
81         Map<Integer, Integer> map = this.map;
82         for (Integer k : keys) {
83             bh.consume(map.get(k));
84         }
85     }
86 
87     @Benchmark
88     public void getMix(Blackhole bh) {
89         Integer[] keys = this.mixed;
90         Map<Integer, Integer> map = this.map;
91         for (Integer k : keys) {
92             bh.consume(map.get(k));
93         }
94     }
95 
96 }