2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-10-08 19:22:15 +02:00
|
|
|
//
|
|
|
|
// 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-09-09 01:07:59 +02:00
|
|
|
package propagators_test
|
2019-10-08 19:22:15 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/trace"
|
2020-09-09 01:07:59 +02:00
|
|
|
"go.opentelemetry.io/otel/propagators"
|
2019-10-08 19:22:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkExtractB3(b *testing.B) {
|
|
|
|
testGroup := []struct {
|
2020-07-08 01:38:52 +02:00
|
|
|
name string
|
|
|
|
tests []extractTest
|
2019-10-08 19:22:15 +02:00
|
|
|
}{
|
|
|
|
{
|
2020-07-08 01:38:52 +02:00
|
|
|
name: "valid headers",
|
|
|
|
tests: extractHeaders,
|
2019-10-08 19:22:15 +02:00
|
|
|
},
|
|
|
|
{
|
2020-07-08 01:38:52 +02:00
|
|
|
name: "invalid headers",
|
|
|
|
tests: extractInvalidHeaders,
|
2019-10-08 19:22:15 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tg := range testGroup {
|
2020-09-09 01:07:59 +02:00
|
|
|
propagator := propagators.B3{}
|
2019-10-08 19:22:15 +02:00
|
|
|
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)
|
|
|
|
}
|
2019-10-15 22:12:56 +02:00
|
|
|
b.ReportAllocs()
|
2019-10-08 19:22:15 +02:00
|
|
|
b.ResetTimer()
|
2019-10-15 22:12:56 +02:00
|
|
|
for i := 0; i < b.N; i++ {
|
2020-02-14 09:16:05 +02:00
|
|
|
_ = propagator.Extract(ctx, req.Header)
|
2019-10-15 22:12:56 +02:00
|
|
|
}
|
2019-10-08 19:22:15 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkInjectB3(b *testing.B) {
|
|
|
|
testGroup := []struct {
|
2020-07-08 01:38:52 +02:00
|
|
|
name string
|
|
|
|
tests []injectTest
|
2019-10-08 19:22:15 +02:00
|
|
|
}{
|
|
|
|
{
|
2020-07-08 01:38:52 +02:00
|
|
|
name: "valid headers",
|
|
|
|
tests: injectHeader,
|
2019-10-08 19:22:15 +02:00
|
|
|
},
|
|
|
|
{
|
2020-07-08 01:38:52 +02:00
|
|
|
name: "invalid headers",
|
|
|
|
tests: injectInvalidHeader,
|
2019-10-08 19:22:15 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tg := range testGroup {
|
|
|
|
for _, tt := range tg.tests {
|
2020-09-09 01:07:59 +02:00
|
|
|
propagator := propagators.B3{InjectEncoding: tt.encoding}
|
2019-10-08 19:22:15 +02:00
|
|
|
traceBenchmark(tg.name+"/"+tt.name, b, func(b *testing.B) {
|
|
|
|
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
2020-07-08 01:38:52 +02:00
|
|
|
ctx := trace.ContextWithSpan(
|
|
|
|
context.Background(),
|
|
|
|
testSpan{sc: tt.sc},
|
|
|
|
)
|
2019-10-15 22:12:56 +02:00
|
|
|
b.ReportAllocs()
|
2019-10-08 19:22:15 +02:00
|
|
|
b.ResetTimer()
|
2019-10-15 22:12:56 +02:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
propagator.Inject(ctx, req.Header)
|
|
|
|
}
|
2019-10-08 19:22:15 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|