go.undefinedlabs.com/scopeagent@v0.4.2/tracer/bench_test.go (about) 1 package tracer 2 3 import ( 4 "bytes" 5 "fmt" 6 "net/http" 7 "testing" 8 9 "github.com/google/uuid" 10 "github.com/opentracing/opentracing-go" 11 ) 12 13 var tags []string 14 15 func init() { 16 tags = make([]string, 1000) 17 for j := 0; j < len(tags); j++ { 18 tags[j] = fmt.Sprintf("%d", getRandomId()) 19 } 20 } 21 22 func executeOps(sp opentracing.Span, numEvent, numTag, numItems int) { 23 for j := 0; j < numEvent; j++ { 24 sp.LogEvent("event") 25 } 26 for j := 0; j < numTag; j++ { 27 sp.SetTag(tags[j], nil) 28 } 29 for j := 0; j < numItems; j++ { 30 sp.SetBaggageItem(tags[j], tags[j]) 31 } 32 } 33 34 func benchmarkWithOps(b *testing.B, numEvent, numTag, numItems int) { 35 var r CountingRecorder 36 t := New(&r) 37 benchmarkWithOpsAndCB(b, func() opentracing.Span { 38 return t.StartSpan("test") 39 }, numEvent, numTag, numItems) 40 if int(r) != b.N { 41 b.Fatalf("missing traces: expected %d, got %d", b.N, r) 42 } 43 } 44 45 func benchmarkWithOpsAndCB(b *testing.B, create func() opentracing.Span, 46 numEvent, numTag, numItems int) { 47 b.ResetTimer() 48 for i := 0; i < b.N; i++ { 49 sp := create() 50 executeOps(sp, numEvent, numTag, numItems) 51 sp.Finish() 52 } 53 b.StopTimer() 54 } 55 56 func BenchmarkSpan_Empty(b *testing.B) { 57 benchmarkWithOps(b, 0, 0, 0) 58 } 59 60 func BenchmarkSpan_100Events(b *testing.B) { 61 benchmarkWithOps(b, 100, 0, 0) 62 } 63 64 func BenchmarkSpan_1000Events(b *testing.B) { 65 benchmarkWithOps(b, 100, 0, 0) 66 } 67 68 func BenchmarkSpan_100Tags(b *testing.B) { 69 benchmarkWithOps(b, 0, 100, 0) 70 } 71 72 func BenchmarkSpan_1000Tags(b *testing.B) { 73 benchmarkWithOps(b, 0, 100, 0) 74 } 75 76 func BenchmarkSpan_100BaggageItems(b *testing.B) { 77 benchmarkWithOps(b, 0, 0, 100) 78 } 79 80 func BenchmarkTrimmedSpan_100Events_100Tags_100BaggageItems(b *testing.B) { 81 var r CountingRecorder 82 opts := DefaultOptions() 83 opts.TrimUnsampledSpans = true 84 opts.ShouldSample = func(_ uuid.UUID) bool { return false } 85 opts.Recorder = &r 86 t := NewWithOptions(opts) 87 benchmarkWithOpsAndCB(b, func() opentracing.Span { 88 sp := t.StartSpan("test") 89 return sp 90 }, 100, 100, 100) 91 if int(r) != b.N { 92 b.Fatalf("missing traces: expected %d, got %d", b.N, r) 93 } 94 } 95 96 func benchmarkInject(b *testing.B, format opentracing.BuiltinFormat, numItems int) { 97 var r CountingRecorder 98 tracer := New(&r) 99 sp := tracer.StartSpan("testing") 100 executeOps(sp, 0, 0, numItems) 101 var carrier interface{} 102 switch format { 103 case opentracing.TextMap: 104 carrier = opentracing.HTTPHeadersCarrier(http.Header{}) 105 case opentracing.Binary: 106 carrier = &bytes.Buffer{} 107 default: 108 b.Fatalf("unhandled format %d", format) 109 } 110 b.ResetTimer() 111 for i := 0; i < b.N; i++ { 112 err := tracer.Inject(sp.Context(), format, carrier) 113 if err != nil { 114 b.Fatal(err) 115 } 116 } 117 } 118 119 func benchmarkExtract(b *testing.B, format opentracing.BuiltinFormat, numItems int) { 120 var r CountingRecorder 121 tracer := New(&r) 122 sp := tracer.StartSpan("testing") 123 executeOps(sp, 0, 0, numItems) 124 var carrier interface{} 125 switch format { 126 case opentracing.TextMap: 127 carrier = opentracing.HTTPHeadersCarrier(http.Header{}) 128 case opentracing.Binary: 129 carrier = &bytes.Buffer{} 130 default: 131 b.Fatalf("unhandled format %d", format) 132 } 133 if err := tracer.Inject(sp.Context(), format, carrier); err != nil { 134 b.Fatal(err) 135 } 136 137 // We create a new bytes.Buffer every time for tracer.Extract() to keep 138 // this benchmark realistic. 139 var rawBinaryBytes []byte 140 if format == opentracing.Binary { 141 rawBinaryBytes = carrier.(*bytes.Buffer).Bytes() 142 } 143 b.ResetTimer() 144 for i := 0; i < b.N; i++ { 145 if format == opentracing.Binary { 146 carrier = bytes.NewBuffer(rawBinaryBytes) 147 } 148 _, err := tracer.Extract(format, carrier) 149 if err != nil { 150 b.Fatal(err) 151 } 152 } 153 } 154 155 func BenchmarkInject_TextMap_Empty(b *testing.B) { 156 benchmarkInject(b, opentracing.TextMap, 0) 157 } 158 159 func BenchmarkInject_TextMap_100BaggageItems(b *testing.B) { 160 benchmarkInject(b, opentracing.TextMap, 100) 161 } 162 163 func BenchmarkInject_Binary_Empty(b *testing.B) { 164 benchmarkInject(b, opentracing.Binary, 0) 165 } 166 167 func BenchmarkInject_Binary_100BaggageItems(b *testing.B) { 168 benchmarkInject(b, opentracing.Binary, 100) 169 } 170 171 func BenchmarkJoin_TextMap_Empty(b *testing.B) { 172 benchmarkExtract(b, opentracing.TextMap, 0) 173 } 174 175 func BenchmarkJoin_TextMap_100BaggageItems(b *testing.B) { 176 benchmarkExtract(b, opentracing.TextMap, 100) 177 } 178 179 func BenchmarkJoin_Binary_Empty(b *testing.B) { 180 benchmarkExtract(b, opentracing.Binary, 0) 181 } 182 183 func BenchmarkJoin_Binary_100BaggageItems(b *testing.B) { 184 benchmarkExtract(b, opentracing.Binary, 100) 185 }