1 /*
  2  * Copyright (c) 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 
 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.Measurement;
 30 import org.openjdk.jmh.annotations.Mode;
 31 import org.openjdk.jmh.annotations.OutputTimeUnit;
 32 import org.openjdk.jmh.annotations.Param;
 33 import org.openjdk.jmh.annotations.Scope;
 34 import org.openjdk.jmh.annotations.Setup;
 35 import org.openjdk.jmh.annotations.State;
 36 import org.openjdk.jmh.annotations.Warmup;
 37 
 38 import java.util.Arrays;
 39 import java.util.HashMap;
 40 import java.util.LinkedHashMap;
 41 import java.util.Map;
 42 import java.util.function.IntFunction;
 43 import java.util.concurrent.TimeUnit;
 44 
 45 /**
 46  * Simple benchmark of toArray.
 47  */
 48 
 49 @BenchmarkMode(Mode.AverageTime)
 50 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 51 @Fork(1)
 52 @State(Scope.Thread)
 53 public class HashMapToArray {
 54 
 55     private IntFunction<Map<Integer, Integer>> mapSupplier;
 56     Map<Integer, Integer> map;
 57 
 58 
 59     @Param(value = {
 60             "org.openjdk.bench.valhalla.corelibs.mapprotos.HashMap",
 61 //            "org.openjdk.bench.valhalla.corelibs.mapprotos.XHashMap",
 62             "java.util.HashMap",
 63         })
 64     private String mapType;
 65 
 66     @Param({"1", "10", "1000", "100000"})
 67     public int size;
 68 
 69     @Setup
 70     public void setup() {
 71         try {
 72             Class<?> mapClass = Class.forName(mapType);
 73             mapSupplier =  (size) -> newInstance(mapClass, size);
 74         } catch (Exception ex) {
 75             System.out.printf("%s: %s%n", mapType, ex.getMessage());
 76             return;
 77         }
 78 
 79         map = mapSupplier.apply(0);
 80         for (int i = 0; i < size; i++) {
 81             map.put(i, i * i);
 82         }
 83     }
 84 
 85     Map<Integer, Integer> newInstance(Class<?> mapClass, int size) {
 86         try {
 87             return (Map<Integer, Integer>)mapClass.getConstructor(int.class).newInstance(size);
 88         } catch (Exception ex) {
 89             throw new RuntimeException("failed", ex);
 90         }
 91     }
 92 
 93     @Benchmark
 94     public Object[] testKeySetToArray() {
 95         return map.keySet().toArray();
 96     }
 97 
 98     @Benchmark
 99     public Object[] testKeySetToArrayTyped() {
100         return map.keySet().toArray(new Integer[0]);
101     }
102 
103     @Benchmark
104     public Object[] testValuesToArray() {
105         return map.values().toArray();
106     }
107 
108     @Benchmark
109     public Object[] testValuesToArrayTyped() {
110         return map.values().toArray(new Integer[0]);
111     }
112 }