|
| 1 | +package datadog.trace.core.context; |
| 2 | + |
| 3 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 4 | + |
| 5 | +import datadog.context.ContextScope; |
| 6 | +import datadog.trace.bootstrap.instrumentation.api.AgentSpan; |
| 7 | +import datadog.trace.core.BlackholeWriter; |
| 8 | +import datadog.trace.core.CoreTracer; |
| 9 | +import datadog.trace.core.TraceCounters; |
| 10 | +import java.util.concurrent.CompletableFuture; |
| 11 | +import java.util.stream.IntStream; |
| 12 | +import org.openjdk.jmh.annotations.Benchmark; |
| 13 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 14 | +import org.openjdk.jmh.annotations.Fork; |
| 15 | +import org.openjdk.jmh.annotations.Level; |
| 16 | +import org.openjdk.jmh.annotations.Measurement; |
| 17 | +import org.openjdk.jmh.annotations.Mode; |
| 18 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 19 | +import org.openjdk.jmh.annotations.Scope; |
| 20 | +import org.openjdk.jmh.annotations.Setup; |
| 21 | +import org.openjdk.jmh.annotations.State; |
| 22 | +import org.openjdk.jmh.annotations.Threads; |
| 23 | +import org.openjdk.jmh.annotations.Warmup; |
| 24 | +import org.openjdk.jmh.infra.Blackhole; |
| 25 | + |
| 26 | +@State(Scope.Benchmark) |
| 27 | +@Warmup(iterations = 1, time = 15, timeUnit = SECONDS) |
| 28 | +@Measurement(iterations = 5, time = 30, timeUnit = SECONDS) |
| 29 | +@BenchmarkMode(Mode.Throughput) |
| 30 | +@OutputTimeUnit(SECONDS) |
| 31 | +@Fork(value = 1) |
| 32 | +@Threads(8) |
| 33 | +public class ShortLivedContextBenchmark { |
| 34 | + // CPU token count for Benchmark.consumeCPU() |
| 35 | + private static final long HUGE_CPU_LOAD = 10_000_000; |
| 36 | + private static final long MEDIUM_CPU_LOAD = 1_000_000; |
| 37 | + private static final long SMALL_CPU_LOAD = 1_000; |
| 38 | + private static final long NO_CPU_LOAD = 0; |
| 39 | + // Delays for Thread.sleep in microseconds |
| 40 | + private static final long HUGE_IO_WAIT = 1_000_000; |
| 41 | + private static final long MEDIUM_IO_WAIT = 1_000; |
| 42 | + private static final long SMALL_IO_WAIT = 200; |
| 43 | + private static final long NO_IO_WAIT = 0; |
| 44 | + |
| 45 | + private static final String INSTRUMENTATION = "benchmark"; |
| 46 | + CoreTracer tracer; |
| 47 | + |
| 48 | + @Setup(Level.Iteration) |
| 49 | + public void setup(Blackhole blackhole) { |
| 50 | + this.tracer = |
| 51 | + CoreTracer.builder() |
| 52 | + .writer(new BlackholeWriter(blackhole, new TraceCounters(), 0)) |
| 53 | + .strictTraceWrites(false) |
| 54 | + .build(); |
| 55 | + } |
| 56 | + |
| 57 | + @Benchmark |
| 58 | + public void benchmarkTraceSegment() { |
| 59 | + makeSpan( |
| 60 | + "root", |
| 61 | + MEDIUM_CPU_LOAD, |
| 62 | + 0, |
| 63 | + () -> { |
| 64 | + makeSpan("child1", HUGE_CPU_LOAD, 0); |
| 65 | + makeSpan("child2", SMALL_CPU_LOAD, 0); |
| 66 | + makeSpan( |
| 67 | + "child3", |
| 68 | + NO_CPU_LOAD, |
| 69 | + MEDIUM_IO_WAIT, |
| 70 | + () -> makeSpan("great-child3-1", MEDIUM_CPU_LOAD, 0)); |
| 71 | + makeSpan("child4", NO_CPU_LOAD, NO_IO_WAIT); |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + @Benchmark |
| 76 | + public void benchmarkContextSwap() { |
| 77 | + makeSpan( |
| 78 | + "root", |
| 79 | + MEDIUM_CPU_LOAD, |
| 80 | + 0, |
| 81 | + () -> { |
| 82 | + CompletableFuture<?>[] futures = |
| 83 | + IntStream.range(0, 100) |
| 84 | + .mapToObj( |
| 85 | + spanId -> |
| 86 | + CompletableFuture.runAsync( |
| 87 | + () -> makeSpan("child" + spanId, SMALL_CPU_LOAD, 0))) |
| 88 | + .toArray(CompletableFuture[]::new); |
| 89 | + CompletableFuture.allOf(futures).join(); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + private void makeSpan(String spanName, long cpuLoad, long ioWait) { |
| 94 | + makeSpan(spanName, cpuLoad, ioWait, null); |
| 95 | + } |
| 96 | + |
| 97 | + private void makeSpan(String spanName, long CPU_LOAD, long ioWait, Runnable additionalWork) { |
| 98 | + AgentSpan span = tracer.startSpan(INSTRUMENTATION, spanName); |
| 99 | + try (ContextScope ignored = span.attach()) { |
| 100 | + if (CPU_LOAD > 0) { |
| 101 | + Blackhole.consumeCPU(CPU_LOAD); |
| 102 | + } |
| 103 | + if (ioWait > 0) { |
| 104 | + sleep(ioWait); |
| 105 | + } |
| 106 | + if (additionalWork != null) { |
| 107 | + additionalWork.run(); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private void sleep(long micro) { |
| 113 | + try { |
| 114 | + long millis = micro / 1000; |
| 115 | + int nano = (int) (micro % 1000) * 1000; |
| 116 | + Thread.sleep(millis, nano); |
| 117 | + } catch (InterruptedException e) { |
| 118 | + Thread.currentThread().interrupt(); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments