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.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.util.Arrays;
36 import java.util.Collections;
37 import java.util.Random;
38 import java.util.concurrent.TimeUnit;
39 import java.util.stream.IntStream;
40 
41 @Fork(1)
42 @Warmup(iterations = 3, time = 1)
43 @Measurement(iterations = 5, time = 1)
44 @OutputTimeUnit(TimeUnit.MICROSECONDS)
45 @BenchmarkMode(Mode.AverageTime)
46 @State(Scope.Thread)
47 public class MapBase {
48 
49     @Param({"500", "1000000"})
50     public int size;
51 
52     @Param({"42"})
53     public int seed;
54 
55     public Random rnd;
56     public Integer[] keys;
57     public Integer[] nonKeys;
58 
59     public void init(int size) {
60         Integer[] all;
61         if (seed != 0) {
62             rnd = new Random(seed);
63             all = rnd.ints().distinct().limit(size * 2).boxed().toArray(Integer[]::new);
64             Collections.shuffle(Arrays.asList(all), rnd);
65         } else {
66             rnd = new Random();
67             all = IntStream.range(0, size * 2).boxed().toArray(Integer[]::new);
68             Collections.shuffle(Arrays.asList(all));
69         }
70         keys = Arrays.copyOfRange(all, 0, size);
71         nonKeys = Arrays.copyOfRange(all, size, size * 2);
72 
73     }
74 }