|
| 1 | +package com.uber.m3.tally; |
| 2 | + |
| 3 | +import com.uber.m3.util.Duration; |
| 4 | +import com.uber.m3.util.ImmutableMap; |
| 5 | +import org.openjdk.jmh.annotations.*; |
| 6 | +import org.openjdk.jmh.infra.Blackhole; |
| 7 | + |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | + |
| 10 | +@BenchmarkMode(Mode.Throughput) |
| 11 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 12 | +@Fork(value = 2, jvmArgsAppend = { "-server", "-XX:+UseG1GC" }) |
| 13 | +public class ScopeImplConcurrent { |
| 14 | + private static String KEYS[] = new String[]{ |
| 15 | + " ", "0", "@", "P", |
| 16 | + }; |
| 17 | + |
| 18 | + @Benchmark |
| 19 | + public void hotkeyLockContention(Blackhole bh, BenchmarkState state) { |
| 20 | + ImmutableMap<String, String> common = new ImmutableMap.Builder<String, String>().build(); |
| 21 | + for (int i = 0; i < 10000; i++) { |
| 22 | + |
| 23 | + for (String key : KEYS) { |
| 24 | + Scope scope = state.scope.computeSubscopeIfAbsent(key, common); |
| 25 | + assert scope != null; |
| 26 | + bh.consume(scope); |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + @State(org.openjdk.jmh.annotations.Scope.Benchmark) |
| 32 | + public static class BenchmarkState { |
| 33 | + |
| 34 | + private ScopeImpl scope; |
| 35 | + |
| 36 | + @Setup |
| 37 | + public void setup() { |
| 38 | + this.scope = |
| 39 | + (ScopeImpl) new RootScopeBuilder() |
| 40 | + .reporter(new TestStatsReporter()) |
| 41 | + .reportEvery(Duration.MAX_VALUE); |
| 42 | + |
| 43 | + for (String key : KEYS) { |
| 44 | + scope.computeSubscopeIfAbsent(key, new ImmutableMap.Builder<String, String>().build()); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @TearDown |
| 49 | + public void teardown() { |
| 50 | + scope.close(); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments