2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-01-28 20:13:46 +02:00
|
|
|
package testtrace_test
|
2019-10-14 23:36:34 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/trace"
|
|
|
|
mocktrace "go.opentelemetry.io/otel/internal/trace"
|
2019-10-14 23:36:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkInject(b *testing.B) {
|
2020-01-28 20:13:46 +02:00
|
|
|
var t trace.TraceContext
|
2019-10-14 23:36:34 +02:00
|
|
|
|
|
|
|
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) {
|
2019-10-28 19:05:06 +02:00
|
|
|
var id uint64
|
2020-05-02 14:17:11 +02:00
|
|
|
spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
|
2020-05-02 14:23:09 +02:00
|
|
|
traceID, _ := trace.IDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
|
2019-10-23 08:01:33 +02:00
|
|
|
|
2019-10-14 23:36:34 +02:00
|
|
|
mockTracer := &mocktrace.MockTracer{
|
|
|
|
Sampled: false,
|
2019-10-16 19:24:38 +02:00
|
|
|
StartSpanID: &id,
|
2019-10-14 23:36:34 +02:00
|
|
|
}
|
|
|
|
b.ReportAllocs()
|
2020-05-02 14:17:11 +02:00
|
|
|
sc := trace.SpanContext{
|
2019-10-14 23:36:34 +02:00
|
|
|
TraceID: traceID,
|
|
|
|
SpanID: spanID,
|
2020-05-02 14:17:11 +02:00
|
|
|
TraceFlags: trace.TraceFlagsSampled,
|
2019-10-14 23:36:34 +02:00
|
|
|
}
|
2020-02-04 18:55:03 +02:00
|
|
|
ctx := trace.ContextWithRemoteSpanContext(context.Background(), sc)
|
|
|
|
ctx, _ = mockTracer.Start(ctx, "inject")
|
2019-10-14 23:36:34 +02:00
|
|
|
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) {
|
2020-01-28 20:13:46 +02:00
|
|
|
var propagator trace.TraceContext
|
2019-10-14 23:36:34 +02:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|