|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 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 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Random; |
| 22 | +import java.util.function.Function; |
| 23 | + |
| 24 | +import org.openjdk.jmh.annotations.Benchmark; |
| 25 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 26 | +import org.openjdk.jmh.annotations.Level; |
| 27 | +import org.openjdk.jmh.annotations.Mode; |
| 28 | +import org.openjdk.jmh.annotations.Param; |
| 29 | +import org.openjdk.jmh.annotations.Scope; |
| 30 | +import org.openjdk.jmh.annotations.Setup; |
| 31 | +import org.openjdk.jmh.annotations.State; |
| 32 | +import org.openjdk.jmh.infra.Blackhole; |
| 33 | + |
| 34 | +/** |
| 35 | + * Benchmarks for {@link ConcurrentLruCache}. |
| 36 | + * @author Brian Clozel |
| 37 | + */ |
| 38 | +@BenchmarkMode(Mode.Throughput) |
| 39 | +public class ConcurrentLruCacheBenchmark { |
| 40 | + |
| 41 | + @Benchmark |
| 42 | + public void lruCache(BenchmarkData data, Blackhole bh) { |
| 43 | + for (String element : data.elements) { |
| 44 | + String value = data.lruCache.get(element); |
| 45 | + bh.consume(value); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @State(Scope.Benchmark) |
| 50 | + public static class BenchmarkData { |
| 51 | + |
| 52 | + ConcurrentLruCache<String, String> lruCache; |
| 53 | + |
| 54 | + @Param({"100"}) |
| 55 | + public int capacity; |
| 56 | + |
| 57 | + @Param({"0.1"}) |
| 58 | + public float cacheMissRate; |
| 59 | + |
| 60 | + public List<String> elements; |
| 61 | + |
| 62 | + public Function<String, String> generator; |
| 63 | + |
| 64 | + @Setup(Level.Iteration) |
| 65 | + public void setup() { |
| 66 | + this.generator = key -> key + "value"; |
| 67 | + this.lruCache = new ConcurrentLruCache<>(this.capacity, this.generator); |
| 68 | + Assert.isTrue(this.cacheMissRate < 1, "cache miss rate should be < 1"); |
| 69 | + Random random = new Random(); |
| 70 | + int elementsCount = Math.round(this.capacity * (1 + this.cacheMissRate)); |
| 71 | + this.elements = new ArrayList<>(elementsCount); |
| 72 | + random.ints(elementsCount).forEach(value -> this.elements.add(String.valueOf(value))); |
| 73 | + this.elements.sort(String::compareTo); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments