|
| 1 | +/* |
| 2 | + * Copyright 2017 VMware, Inc. |
| 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 | +package io.micrometer.benchmark.tracer; |
| 17 | + |
| 18 | +import brave.Span; |
| 19 | +import brave.Tracing; |
| 20 | +import brave.handler.SpanHandler; |
| 21 | +import brave.propagation.ThreadLocalCurrentTraceContext; |
| 22 | +import brave.propagation.TraceContextOrSamplingFlags; |
| 23 | +import brave.sampler.Sampler; |
| 24 | +import io.micrometer.tracing.Tracer; |
| 25 | +import io.micrometer.tracing.brave.bridge.BraveBaggageManager; |
| 26 | +import io.micrometer.tracing.brave.bridge.BraveCurrentTraceContext; |
| 27 | +import io.micrometer.tracing.brave.bridge.BraveTracer; |
| 28 | +import org.openjdk.jmh.annotations.*; |
| 29 | +import org.openjdk.jmh.infra.Blackhole; |
| 30 | + |
| 31 | +@BenchmarkMode(Mode.Throughput) |
| 32 | +public class BraveTracerBenchmark implements MicrometerTracingBenchmarks { |
| 33 | + |
| 34 | + @State(Scope.Benchmark) |
| 35 | + public static class MicrometerTracingState { |
| 36 | + |
| 37 | + @Param({ "5" }) |
| 38 | + public int childSpanCount; |
| 39 | + |
| 40 | + ThreadLocalCurrentTraceContext braveCurrentTraceContext; |
| 41 | + |
| 42 | + BraveCurrentTraceContext bridgeContext; |
| 43 | + |
| 44 | + Tracing tracing; |
| 45 | + |
| 46 | + brave.Tracer braveTracer; |
| 47 | + |
| 48 | + Tracer tracer; |
| 49 | + |
| 50 | + @Setup |
| 51 | + public void setup() { |
| 52 | + this.braveCurrentTraceContext = ThreadLocalCurrentTraceContext.newBuilder().build(); |
| 53 | + this.bridgeContext = new BraveCurrentTraceContext(this.braveCurrentTraceContext); |
| 54 | + this.tracing = Tracing.newBuilder() |
| 55 | + .currentTraceContext(this.braveCurrentTraceContext) |
| 56 | + .sampler(Sampler.NEVER_SAMPLE) |
| 57 | + .addSpanHandler(SpanHandler.NOOP) |
| 58 | + .build(); |
| 59 | + this.tracing.setNoop(true); |
| 60 | + this.braveTracer = this.tracing.tracer(); |
| 61 | + this.tracer = new BraveTracer(this.braveTracer, this.bridgeContext, new BraveBaggageManager()); |
| 62 | + } |
| 63 | + |
| 64 | + @TearDown |
| 65 | + public void close() { |
| 66 | + this.tracing.close(); |
| 67 | + } |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + @Benchmark |
| 72 | + public void micrometerTracing(MicrometerTracingState state, Blackhole blackhole) { |
| 73 | + micrometerTracing(state.tracer, state.childSpanCount, blackhole); |
| 74 | + } |
| 75 | + |
| 76 | + @Benchmark |
| 77 | + public io.micrometer.tracing.Span micrometerTracingNewSpan(MicrometerTracingState state) { |
| 78 | + return micrometerTracingNewSpan(state.tracer); |
| 79 | + } |
| 80 | + |
| 81 | + @Benchmark |
| 82 | + public void micrometerTracingWithScope(MicrometerTracingState state, Blackhole blackhole) { |
| 83 | + micrometerTracingWithScope(state.tracer, state.childSpanCount, blackhole); |
| 84 | + } |
| 85 | + |
| 86 | + @State(Scope.Benchmark) |
| 87 | + public static class BraveState { |
| 88 | + |
| 89 | + @Param({ "5" }) |
| 90 | + public int childSpanCount; |
| 91 | + |
| 92 | + ThreadLocalCurrentTraceContext braveCurrentTraceContext; |
| 93 | + |
| 94 | + Tracing tracing; |
| 95 | + |
| 96 | + brave.Tracer tracer; |
| 97 | + |
| 98 | + @Setup |
| 99 | + public void setup() { |
| 100 | + this.braveCurrentTraceContext = ThreadLocalCurrentTraceContext.newBuilder().build(); |
| 101 | + this.tracing = Tracing.newBuilder() |
| 102 | + .currentTraceContext(this.braveCurrentTraceContext) |
| 103 | + .sampler(Sampler.NEVER_SAMPLE) |
| 104 | + .addSpanHandler(SpanHandler.NOOP) |
| 105 | + .build(); |
| 106 | + this.tracing.setNoop(true); |
| 107 | + this.tracer = this.tracing.tracer(); |
| 108 | + } |
| 109 | + |
| 110 | + @TearDown |
| 111 | + public void close() { |
| 112 | + this.tracing.close(); |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + @Benchmark |
| 118 | + public void braveTracing(BraveState state, Blackhole blackhole) { |
| 119 | + brave.Span parentSpan = state.tracer.nextSpan().name("parent-span").start(); |
| 120 | + TraceContextOrSamplingFlags traceContext = TraceContextOrSamplingFlags.create(parentSpan.context()); |
| 121 | + for (int i = 0; i < state.childSpanCount; i++) { |
| 122 | + brave.Span span = state.tracer.nextSpan(traceContext).name("new-span" + i); |
| 123 | + span.start().tag("key", "value").annotate("event").finish(); |
| 124 | + blackhole.consume(span); |
| 125 | + } |
| 126 | + parentSpan.finish(); |
| 127 | + blackhole.consume(parentSpan); |
| 128 | + } |
| 129 | + |
| 130 | + @Benchmark |
| 131 | + public Span braveTracingNewSpan(BraveState state, Blackhole blackhole) { |
| 132 | + brave.Span span = state.tracer.nextSpan().name("child-span").start(); |
| 133 | + span.finish(); |
| 134 | + return span; |
| 135 | + } |
| 136 | + |
| 137 | + @Benchmark |
| 138 | + public void braveTracingWithScope(BraveState state, Blackhole blackhole) { |
| 139 | + brave.Span parentSpan = state.tracer.nextSpan().name("parent-span").start(); |
| 140 | + try (brave.Tracer.SpanInScope spanInScope = state.tracer.withSpanInScope(parentSpan)) { |
| 141 | + for (int i = 0; i < state.childSpanCount; i++) { |
| 142 | + brave.Span childSpan = state.tracer.nextSpan().name("new-span" + i); |
| 143 | + try (brave.Tracer.SpanInScope spanInScope2 = state.tracer.withSpanInScope(childSpan.start())) { |
| 144 | + childSpan.tag("key", "value").annotate("event"); |
| 145 | + } |
| 146 | + childSpan.finish(); |
| 147 | + blackhole.consume(childSpan); |
| 148 | + } |
| 149 | + } |
| 150 | + parentSpan.finish(); |
| 151 | + blackhole.consume(parentSpan); |
| 152 | + } |
| 153 | + |
| 154 | +} |
0 commit comments