Debian Codegraph @

propagation/trace_context_benchmark_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package propagation_test
5
6 import (
7 "context"
8 "net/http"
9 "testing"
10
11 "go.opentelemetry.io/otel/propagation"
12 "go.opentelemetry.io/otel/trace"
13 )
14
15 func BenchmarkInject(b *testing.B) {
16 var t propagation.TraceContext
17
18 injectSubBenchmarks(b, func(ctx context.Context, b *testing.B) {
19 h := http.Header{}
20 b.ResetTimer()
21 for i := 0; i < b.N; i++ {
22 t.Inject(ctx, propagation.HeaderCarrier(h))
23 }
24 })
25 }
26
27 func injectSubBenchmarks(b *testing.B, fn func(context.Context, *testing.B)) {
28 b.Run("SampledSpanContext", func(b *testing.B) {
29 b.ReportAllocs()
30 sc := trace.NewSpanContext(trace.SpanContextConfig{
31 TraceID: traceID,
32 SpanID: spanID,
33 TraceFlags: trace.FlagsSampled,
34 })
35 ctx := trace.ContextWithRemoteSpanContext(b.Context(), sc)
36 fn(ctx, b)
37 })
38
39 b.Run("WithoutSpanContext", func(b *testing.B) {
40 b.ReportAllocs()
41 ctx := b.Context()
42 fn(ctx, b)
43 })
44 }
45
46 func BenchmarkExtract(b *testing.B) {
47 extractSubBenchmarks(b, func(b *testing.B, req *http.Request) {
48 var propagator propagation.TraceContext
49 ctx := b.Context()
50 b.ResetTimer()
51 for i := 0; i < b.N; i++ {
52 propagator.Extract(ctx, propagation.HeaderCarrier(req.Header))
53 }
54 })
55 }
56
57 func extractSubBenchmarks(b *testing.B, fn func(*testing.B, *http.Request)) {
58 b.Run("Sampled", func(b *testing.B) {
59 req, _ := http.NewRequestWithContext(b.Context(), http.MethodGet, "http://example.com", http.NoBody)
60 req.Header.Set("traceparent", "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01")
61 b.ReportAllocs()
62
63 fn(b, req)
64 })
65
66 b.Run("BogusVersion", func(b *testing.B) {
67 req, _ := http.NewRequestWithContext(b.Context(), http.MethodGet, "http://example.com", http.NoBody)
68 req.Header.Set("traceparent", "qw-00000000000000000000000000000000-0000000000000000-01")
69 b.ReportAllocs()
70 fn(b, req)
71 })
72
73 b.Run("FutureAdditionalData", func(b *testing.B) {
74 req, _ := http.NewRequestWithContext(b.Context(), http.MethodGet, "http://example.com", http.NoBody)
75 req.Header.Set("traceparent", "02-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-09-XYZxsf09")
76 b.ReportAllocs()
77 fn(b, req)
78 })
79 }