From 0bd4795cfd44ab887d93332878a8123e32bc0c5d Mon Sep 17 00:00:00 2001 From: rahulpa Date: Mon, 7 Oct 2019 22:14:12 -0700 Subject: [PATCH] add benchmark test. --- propagation/benchmark_b3_propagator_test.go | 127 ++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 propagation/benchmark_b3_propagator_test.go diff --git a/propagation/benchmark_b3_propagator_test.go b/propagation/benchmark_b3_propagator_test.go new file mode 100644 index 00000000000..0eae3eb59eb --- /dev/null +++ b/propagation/benchmark_b3_propagator_test.go @@ -0,0 +1,127 @@ +// Copyright 2019, 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 propagation_test + +import ( + "context" + "net/http" + "testing" + + "go.opentelemetry.io/api/trace" + mocktrace "go.opentelemetry.io/internal/trace" + "go.opentelemetry.io/propagation" +) + +func BenchmarkExtractB3(b *testing.B) { + testGroup := []struct { + singleHeader bool + name string + tests []extractTest + }{ + { + singleHeader: false, + name: "multiple headers", + tests: extractMultipleHeaders, + }, + { + singleHeader: true, + name: "single headers", + tests: extractSingleHeader, + }, + { + singleHeader: false, + name: "invalid multiple headers", + tests: extractInvalidB3MultipleHeaders, + }, + { + singleHeader: true, + name: "invalid single headers", + tests: extractInvalidB3SingleHeader, + }, + } + trace.SetGlobalTracer(&mocktrace.MockTracer{}) + + for _, tg := range testGroup { + propagator := propagation.HttpB3Propagator(tg.singleHeader) + 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.ResetTimer() + _ = propagator.Extract(ctx, req.Header) + }) + } + } +} + +func BenchmarkInjectB3(b *testing.B) { + var id uint64 + testGroup := []struct { + singleHeader bool + name string + tests []injectTest + }{ + { + singleHeader: false, + name: "multiple headers", + tests: injectB3MultipleHeader, + }, + { + singleHeader: true, + name: "single headers", + tests: injectB3SingleleHeader, + }, + } + + mockTracer := &mocktrace.MockTracer{ + Sampled: false, + StartSpanId: &id, + } + trace.SetGlobalTracer(mockTracer) + + for _, tg := range testGroup { + id = 0 + propagator := propagation.HttpB3Propagator(tg.singleHeader) + for _, tt := range tg.tests { + traceBenchmark(tg.name+"/"+tt.name, b, func(b *testing.B) { + req, _ := http.NewRequest("GET", "http://example.com", nil) + ctx := context.Background() + if tt.parentSc.IsValid() { + ctx, _ = mockTracer.Start(ctx, "inject", trace.ChildOf(tt.parentSc)) + } else { + ctx, _ = mockTracer.Start(ctx, "inject") + } + + b.ResetTimer() + 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) + }) +}