forked from open-telemetry/opentelemetry-go-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SDK: Add benchmark test for W3C TraceContext Inject/Extract (#194)
* SDK: Add benchmark test for W3C TraceContext Inject/Extract * SDK: benchmark TraceContext: remove global tracer * SDK: Add benchmarks for TraceContext: rename methods
- Loading branch information
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
propagation/http_trace_context_propagator_benchmark_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package propagation | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"go.opentelemetry.io/api/core" | ||
"go.opentelemetry.io/api/trace" | ||
mocktrace "go.opentelemetry.io/internal/trace" | ||
) | ||
|
||
func BenchmarkInject(b *testing.B) { | ||
t := httpTraceContextPropagator{} | ||
|
||
injectSubBenchmarks(b, func(ctx context.Context, b *testing.B) { | ||
req, _ := http.NewRequest("GET", "http://example.com", nil) | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
t.Inject(ctx, req.Header) | ||
} | ||
}) | ||
} | ||
|
||
func injectSubBenchmarks(b *testing.B, fn func(context.Context, *testing.B)) { | ||
b.Run("SampledSpanContext", func(b *testing.B) { | ||
var ( | ||
id uint64 | ||
spanID = uint64(0x00f067aa0ba902b7) | ||
traceID = core.TraceID{High: 0x4bf92f3577b34da6, Low: 0xa3ce929d0e0e4736} | ||
) | ||
mockTracer := &mocktrace.MockTracer{ | ||
Sampled: false, | ||
StartSpanId: &id, | ||
} | ||
b.ReportAllocs() | ||
sc := core.SpanContext{ | ||
TraceID: traceID, | ||
SpanID: spanID, | ||
TraceFlags: core.TraceFlagsSampled, | ||
} | ||
ctx := context.Background() | ||
ctx, _ = mockTracer.Start(ctx, "inject", trace.ChildOf(sc)) | ||
fn(ctx, b) | ||
}) | ||
|
||
b.Run("WithoutSpanContext", func(b *testing.B) { | ||
b.ReportAllocs() | ||
ctx := context.Background() | ||
fn(ctx, b) | ||
}) | ||
} | ||
|
||
func BenchmarkExtract(b *testing.B) { | ||
extractSubBenchmarks(b, func(b *testing.B, req *http.Request) { | ||
propagator := HttpTraceContextPropagator() | ||
ctx := context.Background() | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
propagator.Extract(ctx, req.Header) | ||
} | ||
}) | ||
} | ||
|
||
func extractSubBenchmarks(b *testing.B, fn func(*testing.B, *http.Request)) { | ||
b.Run("Sampled", func(b *testing.B) { | ||
req, _ := http.NewRequest("GET", "http://example.com", nil) | ||
req.Header.Set("traceparent", "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01") | ||
b.ReportAllocs() | ||
|
||
fn(b, req) | ||
}) | ||
|
||
b.Run("BogusVersion", func(b *testing.B) { | ||
req, _ := http.NewRequest("GET", "http://example.com", nil) | ||
req.Header.Set("traceparent", "qw-00000000000000000000000000000000-0000000000000000-01") | ||
b.ReportAllocs() | ||
fn(b, req) | ||
}) | ||
|
||
b.Run("FutureAdditionalData", func(b *testing.B) { | ||
req, _ := http.NewRequest("GET", "http://example.com", nil) | ||
req.Header.Set("traceparent", "02-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-09-XYZxsf09") | ||
b.ReportAllocs() | ||
fn(b, req) | ||
}) | ||
} |