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