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.java.util;
  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.Measurement;
  29 import org.openjdk.jmh.annotations.Mode;
  30 import org.openjdk.jmh.annotations.OutputTimeUnit;
  31 import org.openjdk.jmh.annotations.Param;
  32 import org.openjdk.jmh.annotations.Scope;
  33 import org.openjdk.jmh.annotations.Setup;
  34 import org.openjdk.jmh.annotations.State;
  35 import org.openjdk.jmh.annotations.Warmup;
  36 import org.openjdk.jmh.infra.Blackhole;
  37 
  38 import java.util.Comparator;
  39 import java.util.Map;
  40 import java.util.TreeMap;
  41 import java.util.concurrent.TimeUnit;
  42 import java.util.function.Supplier;
  43 
  44 @BenchmarkMode(Mode.AverageTime)
  45 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  46 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
  47 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
  48 @Fork(3)
  49 @State(Scope.Thread)
  50 public class TreeMapUpdate {
  51     @Param({"10", "1000", "100000"})
  52     public int size;
  53 
  54     @Param({"true", "false"})
  55     public boolean shuffled;
  56 
  57     @Param({"true", "false"})
  58     public boolean comparator;
  59 
  60     private Supplier<Map<Integer, Integer>> supplier;
  61 
  62     @Setup
  63     public void setUp() {
  64         supplier = comparator ? () -> new TreeMap<>(Comparator.reverseOrder()) : TreeMap::new;
  65     }
  66 
  67     private int nextKey(int prevKey) {
  68         return shuffled ? prevKey + 1 : prevKey + 987_654_321;
  69     }
  70 
  71     @Benchmark
  72     public Map<Integer, Integer> put(Blackhole bh) {
  73         int s = size;
  74         Map<Integer, Integer> map = supplier.get();
  75         int key = 0;
  76         for (int i = 0; i < s; i++) {
  77             bh.consume(map.put(key, key));
  78             bh.consume(map.put(key, key));
  79             key = nextKey(key);
  80         }
  81         return map;
  82     }
  83 
  84     @Benchmark
  85     public Map<Integer, Integer> putIfAbsent(Blackhole bh) {
  86         int s = size;
  87         Map<Integer, Integer> map = supplier.get();
  88         int key = 0;
  89         for (int i = 0; i < s; i++) {
  90             bh.consume(map.putIfAbsent(key, key));
  91             bh.consume(map.putIfAbsent(key, key));
  92             key = nextKey(key);
  93         }
  94         return map;
  95     }
  96 
  97     @Benchmark
  98     public Map<Integer, Integer> computeIfAbsent(Blackhole bh) {
  99         int s = size;
 100         Map<Integer, Integer> map = supplier.get();
 101         int key = 0;
 102         for (int i = 0; i < s; i++) {
 103             bh.consume(map.computeIfAbsent(key, k -> k));
 104             bh.consume(map.computeIfAbsent(key, k -> k));
 105             key = nextKey(key);
 106         }
 107         return map;
 108     }
 109 
 110     @Benchmark
 111     public Map<Integer, Integer> compute(Blackhole bh) {
 112         int s = size;
 113         Map<Integer, Integer> map = supplier.get();
 114         int key = 0;
 115         for (int i = 0; i < s; i++) {
 116             bh.consume(map.compute(key, (k, old) -> k * 2));
 117             bh.consume(map.compute(key, (k, old) -> k * 2));
 118             key = nextKey(key);
 119         }
 120         return map;
 121     }
 122 
 123     @Benchmark
 124     public Map<Integer, Integer> computeIfPresent(Blackhole bh) {
 125         int s = size;
 126         Map<Integer, Integer> map = supplier.get();
 127         int key = 0;
 128         for (int i = 0; i < s; i++) {
 129             bh.consume(map.computeIfPresent(key, (k, old) -> old * 2));
 130             map.put(key, key);
 131             bh.consume(map.computeIfPresent(key, (k, old) -> old * 2));
 132             key = nextKey(key);
 133         }
 134         return map;
 135     }
 136 
 137     @Benchmark
 138     public Map<Integer, Integer> merge(Blackhole bh) {
 139         int s = size;
 140         Map<Integer, Integer> map = supplier.get();
 141         int key = 0;
 142         for (int i = 0; i < s; i++) {
 143             bh.consume(map.merge(key, key, Integer::sum));
 144             bh.consume(map.merge(key, key, Integer::sum));
 145             key = nextKey(key);
 146         }
 147         return map;
 148     }
 149 }