|
| 1 | +package datadog.trace.api.naming; |
| 2 | + |
| 3 | +import static java.util.concurrent.TimeUnit.MICROSECONDS; |
| 4 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 5 | + |
| 6 | +import datadog.trace.api.Config; |
| 7 | +import datadog.trace.bootstrap.instrumentation.api.AgentSpan; |
| 8 | +import datadog.trace.core.BlackholeWriter; |
| 9 | +import datadog.trace.core.CoreTracer; |
| 10 | +import datadog.trace.core.TraceCounters; |
| 11 | +import java.util.WeakHashMap; |
| 12 | +import java.util.function.Supplier; |
| 13 | +import org.openjdk.jmh.annotations.Benchmark; |
| 14 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 15 | +import org.openjdk.jmh.annotations.Fork; |
| 16 | +import org.openjdk.jmh.annotations.Level; |
| 17 | +import org.openjdk.jmh.annotations.Measurement; |
| 18 | +import org.openjdk.jmh.annotations.Mode; |
| 19 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 20 | +import org.openjdk.jmh.annotations.Param; |
| 21 | +import org.openjdk.jmh.annotations.Scope; |
| 22 | +import org.openjdk.jmh.annotations.Setup; |
| 23 | +import org.openjdk.jmh.annotations.State; |
| 24 | +import org.openjdk.jmh.annotations.Warmup; |
| 25 | +import org.openjdk.jmh.infra.Blackhole; |
| 26 | + |
| 27 | +/** |
| 28 | + * Benchmark (pinThreadServiceName) Mode Cnt Score Error Units |
| 29 | + * MessagingNamingBenchmark.complexSupplierServiceName false avgt 3 0.710 ± 0.040 us/op |
| 30 | + * MessagingNamingBenchmark.complexSupplierServiceName true avgt 3 0.718 ± 0.105 us/op |
| 31 | + * MessagingNamingBenchmark.constantServiceName false avgt 3 0.668 ± 0.024 us/op |
| 32 | + * MessagingNamingBenchmark.constantSupplierServiceName false avgt 3 0.666 ± 0.044 us/op |
| 33 | + */ |
| 34 | +@State(Scope.Benchmark) |
| 35 | +@Warmup(iterations = 1, time = 15, timeUnit = SECONDS) |
| 36 | +@Measurement(iterations = 3, time = 30, timeUnit = SECONDS) |
| 37 | +@BenchmarkMode(Mode.AverageTime) |
| 38 | +@OutputTimeUnit(MICROSECONDS) |
| 39 | +@Fork(value = 1) |
| 40 | +public class MessagingNamingBenchmark { |
| 41 | + |
| 42 | + CoreTracer tracer; |
| 43 | + |
| 44 | + WeakHashMap<ClassLoader, String> weakCache; |
| 45 | + |
| 46 | + private static final Supplier<String> constantSupplier = () -> "constant"; |
| 47 | + |
| 48 | + private final Supplier<String> complexSupplier = |
| 49 | + () -> { |
| 50 | + String ret = weakCache.get(Thread.currentThread().getContextClassLoader()); |
| 51 | + if (ret == null) { |
| 52 | + ret = Config.get().getServiceName(); |
| 53 | + } |
| 54 | + return ret; |
| 55 | + }; |
| 56 | + |
| 57 | + @Param({"false", "true"}) |
| 58 | + boolean pinThreadServiceName; |
| 59 | + |
| 60 | + @Setup(Level.Iteration) |
| 61 | + public void init(Blackhole blackhole) { |
| 62 | + tracer = |
| 63 | + CoreTracer.builder() |
| 64 | + .writer(new BlackholeWriter(blackhole, new TraceCounters(), 0)) |
| 65 | + .strictTraceWrites(false) |
| 66 | + .build(); |
| 67 | + weakCache = new WeakHashMap<>(); |
| 68 | + if (pinThreadServiceName) { |
| 69 | + weakCache.put(Thread.currentThread().getContextClassLoader(), constantSupplier.get()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Benchmark |
| 74 | + public void constantServiceName(Blackhole blackhole) { |
| 75 | + final AgentSpan span = tracer.startSpan("", ""); |
| 76 | + span.setServiceName("constant"); |
| 77 | + span.finish(); |
| 78 | + blackhole.consume(span); |
| 79 | + } |
| 80 | + |
| 81 | + @Benchmark |
| 82 | + public void constantSupplierServiceName(Blackhole blackhole) { |
| 83 | + final AgentSpan span = tracer.startSpan("", ""); |
| 84 | + span.setServiceName(constantSupplier.get()); |
| 85 | + span.finish(); |
| 86 | + blackhole.consume(span); |
| 87 | + } |
| 88 | + |
| 89 | + @Benchmark |
| 90 | + public void complexSupplierServiceName(Blackhole blackhole) { |
| 91 | + final AgentSpan span = tracer.startSpan("", ""); |
| 92 | + span.setServiceName(complexSupplier.get()); |
| 93 | + span.finish(); |
| 94 | + blackhole.consume(span); |
| 95 | + } |
| 96 | +} |
0 commit comments