...

Source file src/go.opentelemetry.io/otel/propagation/trace_context_benchmark_test.go

Documentation: go.opentelemetry.io/otel/propagation

     1  // Copyright The OpenTelemetry Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package propagation_test
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  	"testing"
    21  
    22  	"go.opentelemetry.io/otel/propagation"
    23  	"go.opentelemetry.io/otel/trace"
    24  )
    25  
    26  func BenchmarkInject(b *testing.B) {
    27  	var t propagation.TraceContext
    28  
    29  	injectSubBenchmarks(b, func(ctx context.Context, b *testing.B) {
    30  		h := http.Header{}
    31  		b.ResetTimer()
    32  		for i := 0; i < b.N; i++ {
    33  			t.Inject(ctx, propagation.HeaderCarrier(h))
    34  		}
    35  	})
    36  }
    37  
    38  func injectSubBenchmarks(b *testing.B, fn func(context.Context, *testing.B)) {
    39  	b.Run("SampledSpanContext", func(b *testing.B) {
    40  		b.ReportAllocs()
    41  		sc := trace.NewSpanContext(trace.SpanContextConfig{
    42  			TraceID:    traceID,
    43  			SpanID:     spanID,
    44  			TraceFlags: trace.FlagsSampled,
    45  		})
    46  		ctx := trace.ContextWithRemoteSpanContext(context.Background(), sc)
    47  		fn(ctx, b)
    48  	})
    49  
    50  	b.Run("WithoutSpanContext", func(b *testing.B) {
    51  		b.ReportAllocs()
    52  		ctx := context.Background()
    53  		fn(ctx, b)
    54  	})
    55  }
    56  
    57  func BenchmarkExtract(b *testing.B) {
    58  	extractSubBenchmarks(b, func(b *testing.B, req *http.Request) {
    59  		var propagator propagation.TraceContext
    60  		ctx := context.Background()
    61  		b.ResetTimer()
    62  		for i := 0; i < b.N; i++ {
    63  			propagator.Extract(ctx, propagation.HeaderCarrier(req.Header))
    64  		}
    65  	})
    66  }
    67  
    68  func extractSubBenchmarks(b *testing.B, fn func(*testing.B, *http.Request)) {
    69  	b.Run("Sampled", func(b *testing.B) {
    70  		req, _ := http.NewRequest("GET", "http://example.com", nil)
    71  		req.Header.Set("traceparent", "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01")
    72  		b.ReportAllocs()
    73  
    74  		fn(b, req)
    75  	})
    76  
    77  	b.Run("BogusVersion", func(b *testing.B) {
    78  		req, _ := http.NewRequest("GET", "http://example.com", nil)
    79  		req.Header.Set("traceparent", "qw-00000000000000000000000000000000-0000000000000000-01")
    80  		b.ReportAllocs()
    81  		fn(b, req)
    82  	})
    83  
    84  	b.Run("FutureAdditionalData", func(b *testing.B) {
    85  		req, _ := http.NewRequest("GET", "http://example.com", nil)
    86  		req.Header.Set("traceparent", "02-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-09-XYZxsf09")
    87  		b.ReportAllocs()
    88  		fn(b, req)
    89  	})
    90  }
    91  

View as plain text