|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.util; |
| 18 | + |
| 19 | + |
| 20 | +import java.lang.ref.WeakReference; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Random; |
| 26 | +import java.util.WeakHashMap; |
| 27 | +import java.util.function.Function; |
| 28 | + |
| 29 | +import org.openjdk.jmh.annotations.Benchmark; |
| 30 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 31 | +import org.openjdk.jmh.annotations.Level; |
| 32 | +import org.openjdk.jmh.annotations.Mode; |
| 33 | +import org.openjdk.jmh.annotations.Param; |
| 34 | +import org.openjdk.jmh.annotations.Scope; |
| 35 | +import org.openjdk.jmh.annotations.Setup; |
| 36 | +import org.openjdk.jmh.annotations.State; |
| 37 | +import org.openjdk.jmh.infra.Blackhole; |
| 38 | + |
| 39 | +/** |
| 40 | + * Benchmarks for {@link ConcurrentReferenceHashMap}. |
| 41 | + * <p>This benchmark ensures that {@link ConcurrentReferenceHashMap} performs |
| 42 | + * better than {@link java.util.Collections#synchronizedMap(Map)} with |
| 43 | + * concurrent read operations. |
| 44 | + * <p>Typically this can be run with {@code "java -jar spring-core-jmh.jar -t 30 -f 2 ConcurrentReferenceHashMapBenchmark"}. |
| 45 | + * @author Brian Clozel |
| 46 | + */ |
| 47 | +@BenchmarkMode(Mode.Throughput) |
| 48 | +public class ConcurrentReferenceHashMapBenchmark { |
| 49 | + |
| 50 | + @Benchmark |
| 51 | + public void concurrentMap(ConcurrentMapBenchmarkData data, Blackhole bh) { |
| 52 | + for (String element : data.elements) { |
| 53 | + WeakReference<String> value = data.map.get(element); |
| 54 | + bh.consume(value); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @State(Scope.Benchmark) |
| 59 | + public static class ConcurrentMapBenchmarkData { |
| 60 | + |
| 61 | + @Param({"500"}) |
| 62 | + public int capacity; |
| 63 | + private final Function<String, String> generator = key -> key + "value"; |
| 64 | + |
| 65 | + public List<String> elements; |
| 66 | + |
| 67 | + public Map<String, WeakReference<String>> map; |
| 68 | + |
| 69 | + @Setup(Level.Iteration) |
| 70 | + public void setup() { |
| 71 | + this.elements = new ArrayList<>(this.capacity); |
| 72 | + this.map = new ConcurrentReferenceHashMap<>(); |
| 73 | + Random random = new Random(); |
| 74 | + random.ints(this.capacity).forEach(value -> { |
| 75 | + String element = String.valueOf(value); |
| 76 | + this.elements.add(element); |
| 77 | + this.map.put(element, new WeakReference<>(this.generator.apply(element))); |
| 78 | + }); |
| 79 | + this.elements.sort(String::compareTo); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Benchmark |
| 84 | + public void synchronizedMap(SynchronizedMapBenchmarkData data, Blackhole bh) { |
| 85 | + for (String element : data.elements) { |
| 86 | + WeakReference<String> value = data.map.get(element); |
| 87 | + bh.consume(value); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @State(Scope.Benchmark) |
| 92 | + public static class SynchronizedMapBenchmarkData { |
| 93 | + |
| 94 | + @Param({"500"}) |
| 95 | + public int capacity; |
| 96 | + |
| 97 | + private Function<String, String> generator = key -> key + "value"; |
| 98 | + |
| 99 | + public List<String> elements; |
| 100 | + |
| 101 | + public Map<String, WeakReference<String>> map; |
| 102 | + |
| 103 | + |
| 104 | + @Setup(Level.Iteration) |
| 105 | + public void setup() { |
| 106 | + this.elements = new ArrayList<>(this.capacity); |
| 107 | + this.map = Collections.synchronizedMap(new WeakHashMap<>()); |
| 108 | + Random random = new Random(); |
| 109 | + random.ints(this.capacity).forEach(value -> { |
| 110 | + String element = String.valueOf(value); |
| 111 | + this.elements.add(element); |
| 112 | + this.map.put(element, new WeakReference<>(this.generator.apply(element))); |
| 113 | + }); |
| 114 | + this.elements.sort(String::compareTo); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments