1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-10 00:29:12 +02:00
opentelemetry-go/propagators/b3_benchmark_test.go
Tyler Yahn f1dad21e47
Move propagators out of API and into their own package (#1118)
* Add new propagators package

* Move B3 propagator to propagators

Update all `api/trace` dependencies in the propagator.

Export the isDeferred and isDebug methods of the SpanContext. These are
needed by the B3 propagator to track trace state.

* Move W3C trace context propagator to propagators

* Update package docs with supported encodings

* Move unified propagators code to own file

* Update b3 exported documentation

* Update trace_context exported documentation

* Add code examples for B3 propagator

* Add TraceContext example code

* Remove internal package

Move common testing declarations to the propagators_test.go file.

* Add changes to Changelog

* Add test to check default propagators
2020-09-08 19:07:59 -04:00

104 lines
2.3 KiB
Go

// 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.
package propagators_test
import (
"context"
"net/http"
"testing"
"go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/propagators"
)
func BenchmarkExtractB3(b *testing.B) {
testGroup := []struct {
name string
tests []extractTest
}{
{
name: "valid headers",
tests: extractHeaders,
},
{
name: "invalid headers",
tests: extractInvalidHeaders,
},
}
for _, tg := range testGroup {
propagator := propagators.B3{}
for _, tt := range tg.tests {
traceBenchmark(tg.name+"/"+tt.name, b, func(b *testing.B) {
ctx := context.Background()
req, _ := http.NewRequest("GET", "http://example.com", nil)
for h, v := range tt.headers {
req.Header.Set(h, v)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = propagator.Extract(ctx, req.Header)
}
})
}
}
}
func BenchmarkInjectB3(b *testing.B) {
testGroup := []struct {
name string
tests []injectTest
}{
{
name: "valid headers",
tests: injectHeader,
},
{
name: "invalid headers",
tests: injectInvalidHeader,
},
}
for _, tg := range testGroup {
for _, tt := range tg.tests {
propagator := propagators.B3{InjectEncoding: tt.encoding}
traceBenchmark(tg.name+"/"+tt.name, b, func(b *testing.B) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
ctx := trace.ContextWithSpan(
context.Background(),
testSpan{sc: tt.sc},
)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
propagator.Inject(ctx, req.Header)
}
})
}
}
}
func traceBenchmark(name string, b *testing.B, fn func(*testing.B)) {
b.Run(name, func(b *testing.B) {
b.ReportAllocs()
fn(b)
})
b.Run(name, func(b *testing.B) {
b.ReportAllocs()
fn(b)
})
}