...

Source file src/go.opencensus.io/trace/benchmark_test.go

Documentation: go.opencensus.io/trace

     1  // Copyright 2018, OpenCensus 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 trace
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  )
    21  
    22  func BenchmarkStartEndSpan(b *testing.B) {
    23  	traceBenchmark(b, func(b *testing.B) {
    24  		ctx := context.Background()
    25  		b.ResetTimer()
    26  		for i := 0; i < b.N; i++ {
    27  			_, span := StartSpan(ctx, "/foo")
    28  			span.End()
    29  		}
    30  	})
    31  }
    32  
    33  func BenchmarkSpanWithAnnotations_4(b *testing.B) {
    34  	traceBenchmark(b, func(b *testing.B) {
    35  		ctx := context.Background()
    36  		b.ResetTimer()
    37  
    38  		for i := 0; i < b.N; i++ {
    39  			_, span := StartSpan(ctx, "/foo")
    40  			span.AddAttributes(
    41  				BoolAttribute("key1", false),
    42  				StringAttribute("key2", "hello"),
    43  				Int64Attribute("key3", 123),
    44  				Float64Attribute("key4", 123.456),
    45  			)
    46  			span.End()
    47  		}
    48  	})
    49  }
    50  
    51  func BenchmarkSpanWithAnnotations_8(b *testing.B) {
    52  	traceBenchmark(b, func(b *testing.B) {
    53  		ctx := context.Background()
    54  		b.ResetTimer()
    55  
    56  		for i := 0; i < b.N; i++ {
    57  			_, span := StartSpan(ctx, "/foo")
    58  			span.AddAttributes(
    59  				BoolAttribute("key1", false),
    60  				BoolAttribute("key2", true),
    61  				StringAttribute("key3", "hello"),
    62  				StringAttribute("key4", "hello"),
    63  				Int64Attribute("key5", 123),
    64  				Int64Attribute("key6", 456),
    65  				Float64Attribute("key7", 123.456),
    66  				Float64Attribute("key8", 456.789),
    67  			)
    68  			span.End()
    69  		}
    70  	})
    71  }
    72  
    73  func BenchmarkTraceID_DotString(b *testing.B) {
    74  	traceBenchmark(b, func(b *testing.B) {
    75  		t := TraceID{0x0D, 0x0E, 0x0A, 0x0D, 0x0B, 0x0E, 0x0E, 0x0F, 0x0F, 0x0E, 0x0E, 0x0B, 0x0D, 0x0A, 0x0E, 0x0D}
    76  		want := "0d0e0a0d0b0e0e0f0f0e0e0b0d0a0e0d"
    77  		for i := 0; i < b.N; i++ {
    78  			if got := t.String(); got != want {
    79  				b.Fatalf("got = %q want = %q", got, want)
    80  			}
    81  		}
    82  	})
    83  }
    84  
    85  func BenchmarkSpanID_DotString(b *testing.B) {
    86  	traceBenchmark(b, func(b *testing.B) {
    87  		s := SpanID{0x0D, 0x0E, 0x0A, 0x0D, 0x0B, 0x0E, 0x0E, 0x0F}
    88  		want := "0d0e0a0d0b0e0e0f"
    89  		for i := 0; i < b.N; i++ {
    90  			if got := s.String(); got != want {
    91  				b.Fatalf("got = %q want = %q", got, want)
    92  			}
    93  		}
    94  	})
    95  }
    96  
    97  func traceBenchmark(b *testing.B, fn func(*testing.B)) {
    98  	b.Run("AlwaysSample", func(b *testing.B) {
    99  		b.ReportAllocs()
   100  		ApplyConfig(Config{DefaultSampler: AlwaysSample()})
   101  		fn(b)
   102  	})
   103  	b.Run("NeverSample", func(b *testing.B) {
   104  		b.ReportAllocs()
   105  		ApplyConfig(Config{DefaultSampler: NeverSample()})
   106  		fn(b)
   107  	})
   108  }
   109  

View as plain text