1 /*
 2  * Copyright (c) 2016, 2018, 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.BenchmarkMode;
26 import org.openjdk.jmh.annotations.Fork;
27 import org.openjdk.jmh.annotations.Measurement;
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.State;
33 import org.openjdk.jmh.annotations.Warmup;
34 
35 import java.lang.reflect.Method;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.Map;
39 import java.util.Random;
40 import java.util.concurrent.TimeUnit;
41 import java.util.stream.IntStream;
42 
43 @Fork(1)
44 @OutputTimeUnit(TimeUnit.MICROSECONDS)
45 @BenchmarkMode(Mode.AverageTime)
46 @State(Scope.Thread)
47 public class MapBase {
48 
49     @Param({
50             "11",
51             "767",
52             "1572863",
53     })
54     public int size;
55 
56     @Param({
57 //            "0",
58             "42",
59     })
60     public int seed;
61 
62     @Param(value = {
63             "java.util.HashMap",
64             "org.openjdk.bench.valhalla.corelibs.mapprotos.HashMap",
65 //            "org.openjdk.bench.valhalla.corelibs.mapprotos.XHashMap",
66 //            "java.util.HashMap0",
67     })
68     public String mapType;
69 
70     public Random rnd;
71     public Integer[] keys;
72     public Integer[] nonKeys;
73 
74     public void init(int size) {
75         Integer[] all;
76         if (seed != 0) {
77             rnd = new Random(seed);
78             all = rnd.ints().distinct().limit(size * 2).boxed().toArray(Integer[]::new);
79             Collections.shuffle(Arrays.asList(all), rnd);
80         } else {
81             rnd = new Random();
82             all = IntStream.range(0, size * 2).boxed().toArray(Integer[]::new);
83             Collections.shuffle(Arrays.asList(all));
84         }
85         keys = Arrays.copyOfRange(all, 0, size);
86         nonKeys = Arrays.copyOfRange(all, size, size * 2);
87     }
88 
89     void TearDown(Map<Integer, Integer> map) {
90         try {
91             Method m = map.getClass().getMethod("dumpStats", java.io.PrintStream.class);
92             m.invoke(map, System.out);
93         } catch (Throwable nsme) {
94             System.out.println("Stats not available:");
95         }
96     }
97 }