forked from open-telemetry/opentelemetry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_test.go
358 lines (280 loc) · 7.82 KB
/
benchmark_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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright The 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 metric_test
import (
"context"
"fmt"
"math/rand"
"testing"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/metric/sdkapi"
sdk "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/export"
"go.opentelemetry.io/otel/sdk/metric/processor/processortest"
)
type benchFixture struct {
meter metric.Meter
accumulator *sdk.Accumulator
B *testing.B
export.AggregatorSelector
}
func newFixture(b *testing.B) *benchFixture {
b.ReportAllocs()
bf := &benchFixture{
B: b,
AggregatorSelector: processortest.AggregatorSelector(),
}
bf.accumulator = sdk.NewAccumulator(bf)
bf.meter = metric.WrapMeterImpl(bf.accumulator)
return bf
}
func (f *benchFixture) Process(export.Accumulation) error {
return nil
}
func (f *benchFixture) Meter(_ string, _ ...metric.MeterOption) metric.Meter {
return f.meter
}
func (f *benchFixture) meterMust() metric.MeterMust {
return metric.Must(f.meter)
}
func makeLabels(n int) []attribute.KeyValue {
used := map[string]bool{}
l := make([]attribute.KeyValue, n)
for i := 0; i < n; i++ {
var k string
for {
k = fmt.Sprint("k", rand.Intn(1000000000))
if !used[k] {
used[k] = true
break
}
}
l[i] = attribute.String(k, fmt.Sprint("v", rand.Intn(1000000000)))
}
return l
}
func benchmarkLabels(b *testing.B, n int) {
ctx := context.Background()
fix := newFixture(b)
labs := makeLabels(n)
cnt := fix.meterMust().NewInt64Counter("int64.sum")
b.ResetTimer()
for i := 0; i < b.N; i++ {
cnt.Add(ctx, 1, labs...)
}
}
func BenchmarkInt64CounterAddWithLabels_1(b *testing.B) {
benchmarkLabels(b, 1)
}
func BenchmarkInt64CounterAddWithLabels_2(b *testing.B) {
benchmarkLabels(b, 2)
}
func BenchmarkInt64CounterAddWithLabels_4(b *testing.B) {
benchmarkLabels(b, 4)
}
func BenchmarkInt64CounterAddWithLabels_8(b *testing.B) {
benchmarkLabels(b, 8)
}
func BenchmarkInt64CounterAddWithLabels_16(b *testing.B) {
benchmarkLabels(b, 16)
}
// Note: performance does not depend on label set size for the
// benchmarks below--all are benchmarked for a single attribute.
// Iterators
var benchmarkIteratorVar attribute.KeyValue
func benchmarkIterator(b *testing.B, n int) {
labels := attribute.NewSet(makeLabels(n)...)
b.ResetTimer()
for i := 0; i < b.N; i++ {
iter := labels.Iter()
for iter.Next() {
benchmarkIteratorVar = iter.Label()
}
}
}
func BenchmarkIterator_0(b *testing.B) {
benchmarkIterator(b, 0)
}
func BenchmarkIterator_1(b *testing.B) {
benchmarkIterator(b, 1)
}
func BenchmarkIterator_2(b *testing.B) {
benchmarkIterator(b, 2)
}
func BenchmarkIterator_4(b *testing.B) {
benchmarkIterator(b, 4)
}
func BenchmarkIterator_8(b *testing.B) {
benchmarkIterator(b, 8)
}
func BenchmarkIterator_16(b *testing.B) {
benchmarkIterator(b, 16)
}
// Counters
func BenchmarkGlobalInt64CounterAddWithSDK(b *testing.B) {
// Compare with BenchmarkInt64CounterAdd() to see overhead of global
// package. This is in the SDK to avoid the API from depending on the
// SDK.
ctx := context.Background()
fix := newFixture(b)
sdk := global.Meter("test")
global.SetMeterProvider(fix)
labs := []attribute.KeyValue{attribute.String("A", "B")}
cnt := Must(sdk).NewInt64Counter("int64.sum")
b.ResetTimer()
for i := 0; i < b.N; i++ {
cnt.Add(ctx, 1, labs...)
}
}
func BenchmarkInt64CounterAdd(b *testing.B) {
ctx := context.Background()
fix := newFixture(b)
labs := makeLabels(1)
cnt := fix.meterMust().NewInt64Counter("int64.sum")
b.ResetTimer()
for i := 0; i < b.N; i++ {
cnt.Add(ctx, 1, labs...)
}
}
func BenchmarkFloat64CounterAdd(b *testing.B) {
ctx := context.Background()
fix := newFixture(b)
labs := makeLabels(1)
cnt := fix.meterMust().NewFloat64Counter("float64.sum")
b.ResetTimer()
for i := 0; i < b.N; i++ {
cnt.Add(ctx, 1.1, labs...)
}
}
// LastValue
func BenchmarkInt64LastValueAdd(b *testing.B) {
ctx := context.Background()
fix := newFixture(b)
labs := makeLabels(1)
mea := fix.meterMust().NewInt64Histogram("int64.lastvalue")
b.ResetTimer()
for i := 0; i < b.N; i++ {
mea.Record(ctx, int64(i), labs...)
}
}
func BenchmarkFloat64LastValueAdd(b *testing.B) {
ctx := context.Background()
fix := newFixture(b)
labs := makeLabels(1)
mea := fix.meterMust().NewFloat64Histogram("float64.lastvalue")
b.ResetTimer()
for i := 0; i < b.N; i++ {
mea.Record(ctx, float64(i), labs...)
}
}
// Histograms
func BenchmarkInt64HistogramAdd(b *testing.B) {
ctx := context.Background()
fix := newFixture(b)
labs := makeLabels(1)
mea := fix.meterMust().NewInt64Histogram("int64.histogram")
b.ResetTimer()
for i := 0; i < b.N; i++ {
mea.Record(ctx, int64(i), labs...)
}
}
func BenchmarkFloat64HistogramAdd(b *testing.B) {
ctx := context.Background()
fix := newFixture(b)
labs := makeLabels(1)
mea := fix.meterMust().NewFloat64Histogram("float64.histogram")
b.ResetTimer()
for i := 0; i < b.N; i++ {
mea.Record(ctx, float64(i), labs...)
}
}
// Observers
func BenchmarkObserverRegistration(b *testing.B) {
fix := newFixture(b)
names := make([]string, 0, b.N)
for i := 0; i < b.N; i++ {
names = append(names, fmt.Sprintf("test.%d.lastvalue", i))
}
cb := func(_ context.Context, result metric.Int64ObserverResult) {}
b.ResetTimer()
for i := 0; i < b.N; i++ {
fix.meterMust().NewInt64GaugeObserver(names[i], cb)
}
}
func BenchmarkGaugeObserverObservationInt64(b *testing.B) {
ctx := context.Background()
fix := newFixture(b)
labs := makeLabels(1)
_ = fix.meterMust().NewInt64GaugeObserver("test.lastvalue", func(_ context.Context, result metric.Int64ObserverResult) {
for i := 0; i < b.N; i++ {
result.Observe((int64)(i), labs...)
}
})
b.ResetTimer()
fix.accumulator.Collect(ctx)
}
func BenchmarkGaugeObserverObservationFloat64(b *testing.B) {
ctx := context.Background()
fix := newFixture(b)
labs := makeLabels(1)
_ = fix.meterMust().NewFloat64GaugeObserver("test.lastvalue", func(_ context.Context, result metric.Float64ObserverResult) {
for i := 0; i < b.N; i++ {
result.Observe((float64)(i), labs...)
}
})
b.ResetTimer()
fix.accumulator.Collect(ctx)
}
// BatchRecord
func benchmarkBatchRecord8Labels(b *testing.B, numInst int) {
const numLabels = 8
ctx := context.Background()
fix := newFixture(b)
labs := makeLabels(numLabels)
var meas []sdkapi.Measurement
for i := 0; i < numInst; i++ {
inst := fix.meterMust().NewInt64Counter(fmt.Sprintf("int64.%d.sum", i))
meas = append(meas, inst.Measurement(1))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
fix.accumulator.RecordBatch(ctx, labs, meas...)
}
}
func BenchmarkBatchRecord8Labels_1Instrument(b *testing.B) {
benchmarkBatchRecord8Labels(b, 1)
}
func BenchmarkBatchRecord_8Labels_2Instruments(b *testing.B) {
benchmarkBatchRecord8Labels(b, 2)
}
func BenchmarkBatchRecord_8Labels_4Instruments(b *testing.B) {
benchmarkBatchRecord8Labels(b, 4)
}
func BenchmarkBatchRecord_8Labels_8Instruments(b *testing.B) {
benchmarkBatchRecord8Labels(b, 8)
}
// Record creation
func BenchmarkRepeatedDirectCalls(b *testing.B) {
ctx := context.Background()
fix := newFixture(b)
c := fix.meterMust().NewInt64Counter("int64.sum")
k := attribute.String("bench", "true")
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Add(ctx, 1, k)
fix.accumulator.Collect(ctx)
}
}