Skip to content

Commit aefed68

Browse files
committed
update benchmark test to distribute histogram measurements between buckets
1 parent 4415931 commit aefed68

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

sdk/metric/benchmark_test.go

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -81,117 +81,127 @@ func benchSyncViews(sc trace.SpanContext, views ...View) func(*testing.B) {
8181
expRdr := NewManualReader(WithAggregationSelector(exponentialAggregationSelector))
8282
expProvider := NewMeterProvider(WithReader(expRdr), WithView(views...))
8383
expMeter := expProvider.Meter("benchSyncViews")
84+
// Precompute histogram values so they are distributed equally to buckets.
85+
histogramBuckets := DefaultAggregationSelector(InstrumentKindHistogram).(AggregationExplicitBucketHistogram).Boundaries
86+
histogramObservations := make([]float64, len(histogramBuckets))
87+
for i, bucket := range histogramBuckets {
88+
histogramObservations[i] = bucket + 1
89+
}
8490
return func(b *testing.B) {
8591
ctx := trace.ContextWithSpanContext(b.Context(), sc)
8692
iCtr, err := meter.Int64Counter("int64-counter")
8793
assert.NoError(b, err)
8894
b.Run("Int64Counter", benchMeasAttrs(func() measF {
89-
return func(s attribute.Set) func() {
95+
return func(s attribute.Set) func(int) {
9096
o := []metric.AddOption{metric.WithAttributeSet(s)}
91-
return func() { iCtr.Add(ctx, 1, o...) }
97+
return func(int) { iCtr.Add(ctx, 1, o...) }
9298
}
9399
}()))
94100

95101
fCtr, err := meter.Float64Counter("float64-counter")
96102
assert.NoError(b, err)
97103
b.Run("Float64Counter", benchMeasAttrs(func() measF {
98-
return func(s attribute.Set) func() {
104+
return func(s attribute.Set) func(int) {
99105
o := []metric.AddOption{metric.WithAttributeSet(s)}
100-
return func() { fCtr.Add(ctx, 1, o...) }
106+
return func(int) { fCtr.Add(ctx, 1, o...) }
101107
}
102108
}()))
103109

104110
iUDCtr, err := meter.Int64UpDownCounter("int64-up-down-counter")
105111
assert.NoError(b, err)
106112
b.Run("Int64UpDownCounter", benchMeasAttrs(func() measF {
107-
return func(s attribute.Set) func() {
113+
return func(s attribute.Set) func(int) {
108114
o := []metric.AddOption{metric.WithAttributeSet(s)}
109-
return func() { iUDCtr.Add(ctx, 1, o...) }
115+
return func(int) { iUDCtr.Add(ctx, 1, o...) }
110116
}
111117
}()))
112118

113119
fUDCtr, err := meter.Float64UpDownCounter("float64-up-down-counter")
114120
assert.NoError(b, err)
115121
b.Run("Float64UpDownCounter", benchMeasAttrs(func() measF {
116-
return func(s attribute.Set) func() {
122+
return func(s attribute.Set) func(int) {
117123
o := []metric.AddOption{metric.WithAttributeSet(s)}
118-
return func() { fUDCtr.Add(ctx, 1, o...) }
124+
return func(int) { fUDCtr.Add(ctx, 1, o...) }
119125
}
120126
}()))
121127

122128
iGauge, err := meter.Int64Gauge("int64-gauge")
123129
assert.NoError(b, err)
124130
b.Run("Int64Gauge", benchMeasAttrs(func() measF {
125-
return func(s attribute.Set) func() {
131+
return func(s attribute.Set) func(int) {
126132
o := []metric.RecordOption{metric.WithAttributeSet(s)}
127-
return func() { iGauge.Record(ctx, 1, o...) }
133+
return func(int) { iGauge.Record(ctx, 1, o...) }
128134
}
129135
}()))
130136

131137
fGauge, err := meter.Float64Gauge("float64-gauge")
132138
assert.NoError(b, err)
133139
b.Run("Float64Gauge", benchMeasAttrs(func() measF {
134-
return func(s attribute.Set) func() {
140+
return func(s attribute.Set) func(int) {
135141
o := []metric.RecordOption{metric.WithAttributeSet(s)}
136-
return func() { fGauge.Record(ctx, 1, o...) }
142+
return func(int) { fGauge.Record(ctx, 1, o...) }
137143
}
138144
}()))
139145

140146
iHist, err := meter.Int64Histogram("int64-histogram")
141147
assert.NoError(b, err)
142148
b.Run("Int64Histogram", benchMeasAttrs(func() measF {
143-
return func(s attribute.Set) func() {
149+
return func(s attribute.Set) func(int) {
144150
o := []metric.RecordOption{metric.WithAttributeSet(s)}
145-
return func() { iHist.Record(ctx, 1, o...) }
151+
return func(i int) { iHist.Record(ctx, int64(histogramObservations[i%len(histogramObservations)]), o...) }
146152
}
147153
}()))
148154

149155
fHist, err := meter.Float64Histogram("float64-histogram")
150156
assert.NoError(b, err)
151157
b.Run("Float64Histogram", benchMeasAttrs(func() measF {
152-
return func(s attribute.Set) func() {
158+
return func(s attribute.Set) func(i int) {
153159
o := []metric.RecordOption{metric.WithAttributeSet(s)}
154-
return func() { fHist.Record(ctx, 1, o...) }
160+
return func(i int) { fHist.Record(ctx, histogramObservations[i%len(histogramObservations)], o...) }
155161
}
156162
}()))
157163

158164
expIHist, err := expMeter.Int64Histogram("exponential-int64-histogram")
159165
assert.NoError(b, err)
160166
b.Run("ExponentialInt64Histogram", benchMeasAttrs(func() measF {
161-
return func(s attribute.Set) func() {
167+
return func(s attribute.Set) func(int) {
162168
o := []metric.RecordOption{metric.WithAttributeSet(s)}
163-
return func() { expIHist.Record(ctx, 1, o...) }
169+
return func(int) { expIHist.Record(ctx, 1, o...) }
164170
}
165171
}()))
166172

167173
expFHist, err := expMeter.Float64Histogram("exponential-float64-histogram")
168174
assert.NoError(b, err)
169175
b.Run("ExponentialFloat64Histogram", benchMeasAttrs(func() measF {
170-
return func(s attribute.Set) func() {
176+
return func(s attribute.Set) func(int) {
171177
o := []metric.RecordOption{metric.WithAttributeSet(s)}
172-
return func() { expFHist.Record(ctx, 1, o...) }
178+
return func(int) { expFHist.Record(ctx, 1, o...) }
173179
}
174180
}()))
175181
}
176182
}
177183

178-
type measF func(s attribute.Set) func()
184+
type measF func(s attribute.Set) func(i int)
179185

180186
func benchMeasAttrs(meas measF) func(*testing.B) {
181187
return func(b *testing.B) {
182188
b.Run("Attributes/0", func(b *testing.B) {
183189
f := meas(*attribute.EmptySet())
184190
b.RunParallel(func(pb *testing.PB) {
191+
i := 0
185192
for pb.Next() {
186-
f()
193+
f(i)
194+
i++
187195
}
188196
})
189197
})
190198
b.Run("Attributes/1", func(b *testing.B) {
191199
f := meas(attribute.NewSet(attribute.Bool("K", true)))
192200
b.RunParallel(func(pb *testing.PB) {
201+
i := 0
193202
for pb.Next() {
194-
f()
203+
f(i)
204+
i++
195205
}
196206
})
197207
})
@@ -204,8 +214,10 @@ func benchMeasAttrs(meas measF) func(*testing.B) {
204214
}
205215
f := meas(attribute.NewSet(attrs...))
206216
b.RunParallel(func(pb *testing.PB) {
217+
i := 0
207218
for pb.Next() {
208-
f()
219+
f(i)
220+
i++
209221
}
210222
})
211223
})

0 commit comments

Comments
 (0)