| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package metric // import "go.opentelemetry.io/otel/sdk/metric" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"runtime" |
| 10 |
"strconv" |
| 11 |
"sync" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
|
| 16 |
"go.opentelemetry.io/otel/attribute" |
| 17 |
"go.opentelemetry.io/otel/metric" |
| 18 |
"go.opentelemetry.io/otel/sdk/metric/exemplar" |
| 19 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 20 |
"go.opentelemetry.io/otel/trace" |
| 21 |
) |
| 22 |
|
| 23 |
var viewBenchmarks = []struct { |
| 24 |
Name string |
| 25 |
Views []View |
| 26 |
}{ |
| 27 |
{"NoView", []View{}}, |
| 28 |
{ |
| 29 |
"DropView", |
| 30 |
[]View{NewView( |
| 31 |
Instrument{Name: "*"}, |
| 32 |
Stream{Aggregation: AggregationDrop{}}, |
| 33 |
)}, |
| 34 |
}, |
| 35 |
{ |
| 36 |
"AttrFilterView", |
| 37 |
[]View{NewView( |
| 38 |
Instrument{Name: "*"}, |
| 39 |
Stream{AttributeFilter: attribute.NewAllowKeysFilter("K")}, |
| 40 |
)}, |
| 41 |
}, |
| 42 |
} |
| 43 |
|
| 44 |
var ( |
| 45 |
sampledSpanContext = trace.NewSpanContext(trace.SpanContextConfig{ |
| 46 |
SpanID: trace.SpanID{0o1}, |
| 47 |
TraceID: trace.TraceID{0o1}, |
| 48 |
TraceFlags: trace.FlagsSampled, |
| 49 |
}) |
| 50 |
notSampledSpanContext = trace.NewSpanContext(trace.SpanContextConfig{ |
| 51 |
SpanID: trace.SpanID{0o1}, |
| 52 |
TraceID: trace.TraceID{0o1}, |
| 53 |
}) |
| 54 |
) |
| 55 |
|
| 56 |
var exemplarBenchmarks = []struct { |
| 57 |
Name string |
| 58 |
SpanContext trace.SpanContext |
| 59 |
}{ |
| 60 |
{"ExemplarsDisabled", notSampledSpanContext}, |
| 61 |
{"ExemplarsEnabled", sampledSpanContext}, |
| 62 |
} |
| 63 |
|
| 64 |
func BenchmarkSyncMeasure(b *testing.B) { |
| 65 |
for _, bc := range viewBenchmarks { |
| 66 |
for _, eb := range exemplarBenchmarks { |
| 67 |
b.Run(fmt.Sprintf("%s/%s", bc.Name, eb.Name), benchSyncViews(eb.SpanContext, bc.Views...)) |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func exponentialAggregationSelector(ik InstrumentKind) Aggregation { |
| 73 |
if ik == InstrumentKindHistogram { |
| 74 |
return AggregationBase2ExponentialHistogram{MaxScale: 20, MaxSize: 160} |
| 75 |
} |
| 76 |
return AggregationDefault{} |
| 77 |
} |
| 78 |
|
| 79 |
func benchSyncViews(sc trace.SpanContext, views ...View) func(*testing.B) { |
| 80 |
rdr := NewManualReader() |
| 81 |
provider := NewMeterProvider(WithReader(rdr), WithView(views...)) |
| 82 |
meter := provider.Meter("benchSyncViews") |
| 83 |
expRdr := NewManualReader(WithAggregationSelector(exponentialAggregationSelector)) |
| 84 |
expProvider := NewMeterProvider(WithReader(expRdr), WithView(views...)) |
| 85 |
expMeter := expProvider.Meter("benchSyncViews") |
| 86 |
// Precompute histogram values so they are distributed equally to buckets. |
| 87 |
histogramBuckets := DefaultAggregationSelector(InstrumentKindHistogram).(AggregationExplicitBucketHistogram).Boundaries |
| 88 |
histogramObservations := make([]float64, len(histogramBuckets)) |
| 89 |
for i, bucket := range histogramBuckets { |
| 90 |
histogramObservations[i] = bucket + 1 |
| 91 |
} |
| 92 |
return func(b *testing.B) { |
| 93 |
ctx := trace.ContextWithSpanContext(b.Context(), sc) |
| 94 |
iCtr, err := meter.Int64Counter("int64-counter") |
| 95 |
assert.NoError(b, err) |
| 96 |
b.Run("Int64Counter", benchMeasAttrs(func() measF { |
| 97 |
return func(s attribute.Set) func(int) { |
| 98 |
o := []metric.AddOption{metric.WithAttributeSet(s)} |
| 99 |
return func(int) { iCtr.Add(ctx, 1, o...) } |
| 100 |
} |
| 101 |
}())) |
| 102 |
|
| 103 |
fCtr, err := meter.Float64Counter("float64-counter") |
| 104 |
assert.NoError(b, err) |
| 105 |
b.Run("Float64Counter", benchMeasAttrs(func() measF { |
| 106 |
return func(s attribute.Set) func(int) { |
| 107 |
o := []metric.AddOption{metric.WithAttributeSet(s)} |
| 108 |
return func(int) { fCtr.Add(ctx, 1, o...) } |
| 109 |
} |
| 110 |
}())) |
| 111 |
|
| 112 |
iUDCtr, err := meter.Int64UpDownCounter("int64-up-down-counter") |
| 113 |
assert.NoError(b, err) |
| 114 |
b.Run("Int64UpDownCounter", benchMeasAttrs(func() measF { |
| 115 |
return func(s attribute.Set) func(int) { |
| 116 |
o := []metric.AddOption{metric.WithAttributeSet(s)} |
| 117 |
return func(int) { iUDCtr.Add(ctx, 1, o...) } |
| 118 |
} |
| 119 |
}())) |
| 120 |
|
| 121 |
fUDCtr, err := meter.Float64UpDownCounter("float64-up-down-counter") |
| 122 |
assert.NoError(b, err) |
| 123 |
b.Run("Float64UpDownCounter", benchMeasAttrs(func() measF { |
| 124 |
return func(s attribute.Set) func(int) { |
| 125 |
o := []metric.AddOption{metric.WithAttributeSet(s)} |
| 126 |
return func(int) { fUDCtr.Add(ctx, 1, o...) } |
| 127 |
} |
| 128 |
}())) |
| 129 |
|
| 130 |
iGauge, err := meter.Int64Gauge("int64-gauge") |
| 131 |
assert.NoError(b, err) |
| 132 |
b.Run("Int64Gauge", benchMeasAttrs(func() measF { |
| 133 |
return func(s attribute.Set) func(int) { |
| 134 |
o := []metric.RecordOption{metric.WithAttributeSet(s)} |
| 135 |
return func(int) { iGauge.Record(ctx, 1, o...) } |
| 136 |
} |
| 137 |
}())) |
| 138 |
|
| 139 |
fGauge, err := meter.Float64Gauge("float64-gauge") |
| 140 |
assert.NoError(b, err) |
| 141 |
b.Run("Float64Gauge", benchMeasAttrs(func() measF { |
| 142 |
return func(s attribute.Set) func(int) { |
| 143 |
o := []metric.RecordOption{metric.WithAttributeSet(s)} |
| 144 |
return func(int) { fGauge.Record(ctx, 1, o...) } |
| 145 |
} |
| 146 |
}())) |
| 147 |
|
| 148 |
iHist, err := meter.Int64Histogram("int64-histogram") |
| 149 |
assert.NoError(b, err) |
| 150 |
b.Run("Int64Histogram", benchMeasAttrs(func() measF { |
| 151 |
return func(s attribute.Set) func(int) { |
| 152 |
o := []metric.RecordOption{metric.WithAttributeSet(s)} |
| 153 |
return func(i int) { iHist.Record(ctx, int64(histogramObservations[i%len(histogramObservations)]), o...) } |
| 154 |
} |
| 155 |
}())) |
| 156 |
|
| 157 |
fHist, err := meter.Float64Histogram("float64-histogram") |
| 158 |
assert.NoError(b, err) |
| 159 |
b.Run("Float64Histogram", benchMeasAttrs(func() measF { |
| 160 |
return func(s attribute.Set) func(i int) { |
| 161 |
o := []metric.RecordOption{metric.WithAttributeSet(s)} |
| 162 |
return func(i int) { fHist.Record(ctx, histogramObservations[i%len(histogramObservations)], o...) } |
| 163 |
} |
| 164 |
}())) |
| 165 |
|
| 166 |
expIHist, err := expMeter.Int64Histogram("exponential-int64-histogram") |
| 167 |
assert.NoError(b, err) |
| 168 |
b.Run("ExponentialInt64Histogram", benchMeasAttrs(func() measF { |
| 169 |
return func(s attribute.Set) func(int) { |
| 170 |
o := []metric.RecordOption{metric.WithAttributeSet(s)} |
| 171 |
return func(int) { expIHist.Record(ctx, 1, o...) } |
| 172 |
} |
| 173 |
}())) |
| 174 |
|
| 175 |
expFHist, err := expMeter.Float64Histogram("exponential-float64-histogram") |
| 176 |
assert.NoError(b, err) |
| 177 |
b.Run("ExponentialFloat64Histogram", benchMeasAttrs(func() measF { |
| 178 |
return func(s attribute.Set) func(int) { |
| 179 |
o := []metric.RecordOption{metric.WithAttributeSet(s)} |
| 180 |
return func(int) { expFHist.Record(ctx, 1, o...) } |
| 181 |
} |
| 182 |
}())) |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
type measF func(s attribute.Set) func(i int) |
| 187 |
|
| 188 |
func benchMeasAttrs(meas measF) func(*testing.B) { |
| 189 |
return func(b *testing.B) { |
| 190 |
b.Run("Attributes/0", func(b *testing.B) { |
| 191 |
f := meas(*attribute.EmptySet()) |
| 192 |
b.RunParallel(func(pb *testing.PB) { |
| 193 |
i := 0 |
| 194 |
for pb.Next() { |
| 195 |
f(i) |
| 196 |
i++ |
| 197 |
} |
| 198 |
}) |
| 199 |
}) |
| 200 |
b.Run("Attributes/1", func(b *testing.B) { |
| 201 |
f := meas(attribute.NewSet(attribute.Bool("K", true))) |
| 202 |
b.RunParallel(func(pb *testing.PB) { |
| 203 |
i := 0 |
| 204 |
for pb.Next() { |
| 205 |
f(i) |
| 206 |
i++ |
| 207 |
} |
| 208 |
}) |
| 209 |
}) |
| 210 |
b.Run("Attributes/10", func(b *testing.B) { |
| 211 |
n := 10 |
| 212 |
attrs := make([]attribute.KeyValue, 0) |
| 213 |
attrs = append(attrs, attribute.Bool("K", true)) |
| 214 |
for i := 2; i < n; i++ { |
| 215 |
attrs = append(attrs, attribute.Int(strconv.Itoa(i), i)) |
| 216 |
} |
| 217 |
f := meas(attribute.NewSet(attrs...)) |
| 218 |
b.RunParallel(func(pb *testing.PB) { |
| 219 |
i := 0 |
| 220 |
for pb.Next() { |
| 221 |
f(i) |
| 222 |
i++ |
| 223 |
} |
| 224 |
}) |
| 225 |
}) |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
func BenchmarkCollect(b *testing.B) { |
| 230 |
for _, bc := range viewBenchmarks { |
| 231 |
b.Run(bc.Name, benchCollectViews(bc.Views...)) |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
func benchCollectViews(views ...View) func(*testing.B) { |
| 236 |
setup := func(name string) (metric.Meter, Reader) { |
| 237 |
r := NewManualReader() |
| 238 |
mp := NewMeterProvider(WithReader(r), WithView(views...)) |
| 239 |
return mp.Meter(name), r |
| 240 |
} |
| 241 |
return func(b *testing.B) { |
| 242 |
b.Run("Int64Counter/1", benchCollectAttrs(func(s attribute.Set) Reader { |
| 243 |
m, r := setup("benchCollectViews/Int64Counter") |
| 244 |
i, err := m.Int64Counter("int64-counter") |
| 245 |
assert.NoError(b, err) |
| 246 |
i.Add(b.Context(), 1, metric.WithAttributeSet(s)) |
| 247 |
return r |
| 248 |
})) |
| 249 |
b.Run("Int64Counter/10", benchCollectAttrs(func(s attribute.Set) Reader { |
| 250 |
m, r := setup("benchCollectViews/Int64Counter") |
| 251 |
i, err := m.Int64Counter("int64-counter") |
| 252 |
assert.NoError(b, err) |
| 253 |
for range 10 { |
| 254 |
i.Add(b.Context(), 1, metric.WithAttributeSet(s)) |
| 255 |
} |
| 256 |
return r |
| 257 |
})) |
| 258 |
|
| 259 |
b.Run("Float64Counter/1", benchCollectAttrs(func(s attribute.Set) Reader { |
| 260 |
m, r := setup("benchCollectViews/Float64Counter") |
| 261 |
i, err := m.Float64Counter("float64-counter") |
| 262 |
assert.NoError(b, err) |
| 263 |
i.Add(b.Context(), 1, metric.WithAttributeSet(s)) |
| 264 |
return r |
| 265 |
})) |
| 266 |
b.Run("Float64Counter/10", benchCollectAttrs(func(s attribute.Set) Reader { |
| 267 |
m, r := setup("benchCollectViews/Float64Counter") |
| 268 |
i, err := m.Float64Counter("float64-counter") |
| 269 |
assert.NoError(b, err) |
| 270 |
for range 10 { |
| 271 |
i.Add(b.Context(), 1, metric.WithAttributeSet(s)) |
| 272 |
} |
| 273 |
return r |
| 274 |
})) |
| 275 |
|
| 276 |
b.Run("Int64UpDownCounter/1", benchCollectAttrs(func(s attribute.Set) Reader { |
| 277 |
m, r := setup("benchCollectViews/Int64UpDownCounter") |
| 278 |
i, err := m.Int64UpDownCounter("int64-up-down-counter") |
| 279 |
assert.NoError(b, err) |
| 280 |
i.Add(b.Context(), 1, metric.WithAttributeSet(s)) |
| 281 |
return r |
| 282 |
})) |
| 283 |
b.Run("Int64UpDownCounter/10", benchCollectAttrs(func(s attribute.Set) Reader { |
| 284 |
m, r := setup("benchCollectViews/Int64UpDownCounter") |
| 285 |
i, err := m.Int64UpDownCounter("int64-up-down-counter") |
| 286 |
assert.NoError(b, err) |
| 287 |
for range 10 { |
| 288 |
i.Add(b.Context(), 1, metric.WithAttributeSet(s)) |
| 289 |
} |
| 290 |
return r |
| 291 |
})) |
| 292 |
|
| 293 |
b.Run("Float64UpDownCounter/1", benchCollectAttrs(func(s attribute.Set) Reader { |
| 294 |
m, r := setup("benchCollectViews/Float64UpDownCounter") |
| 295 |
i, err := m.Float64UpDownCounter("float64-up-down-counter") |
| 296 |
assert.NoError(b, err) |
| 297 |
i.Add(b.Context(), 1, metric.WithAttributeSet(s)) |
| 298 |
return r |
| 299 |
})) |
| 300 |
b.Run("Float64UpDownCounter/10", benchCollectAttrs(func(s attribute.Set) Reader { |
| 301 |
m, r := setup("benchCollectViews/Float64UpDownCounter") |
| 302 |
i, err := m.Float64UpDownCounter("float64-up-down-counter") |
| 303 |
assert.NoError(b, err) |
| 304 |
for range 10 { |
| 305 |
i.Add(b.Context(), 1, metric.WithAttributeSet(s)) |
| 306 |
} |
| 307 |
return r |
| 308 |
})) |
| 309 |
|
| 310 |
b.Run("Int64Histogram/1", benchCollectAttrs(func(s attribute.Set) Reader { |
| 311 |
m, r := setup("benchCollectViews/Int64Histogram") |
| 312 |
i, err := m.Int64Histogram("int64-histogram") |
| 313 |
assert.NoError(b, err) |
| 314 |
i.Record(b.Context(), 1, metric.WithAttributeSet(s)) |
| 315 |
return r |
| 316 |
})) |
| 317 |
b.Run("Int64Histogram/10", benchCollectAttrs(func(s attribute.Set) Reader { |
| 318 |
m, r := setup("benchCollectViews/Int64Histogram") |
| 319 |
i, err := m.Int64Histogram("int64-histogram") |
| 320 |
assert.NoError(b, err) |
| 321 |
for range 10 { |
| 322 |
i.Record(b.Context(), 1, metric.WithAttributeSet(s)) |
| 323 |
} |
| 324 |
return r |
| 325 |
})) |
| 326 |
|
| 327 |
b.Run("Float64Histogram/1", benchCollectAttrs(func(s attribute.Set) Reader { |
| 328 |
m, r := setup("benchCollectViews/Float64Histogram") |
| 329 |
i, err := m.Float64Histogram("float64-histogram") |
| 330 |
assert.NoError(b, err) |
| 331 |
i.Record(b.Context(), 1, metric.WithAttributeSet(s)) |
| 332 |
return r |
| 333 |
})) |
| 334 |
b.Run("Float64Histogram/10", benchCollectAttrs(func(s attribute.Set) Reader { |
| 335 |
m, r := setup("benchCollectViews/Float64Histogram") |
| 336 |
i, err := m.Float64Histogram("float64-histogram") |
| 337 |
assert.NoError(b, err) |
| 338 |
for range 10 { |
| 339 |
i.Record(b.Context(), 1, metric.WithAttributeSet(s)) |
| 340 |
} |
| 341 |
return r |
| 342 |
})) |
| 343 |
|
| 344 |
b.Run("Int64ObservableCounter", benchCollectAttrs(func(s attribute.Set) Reader { |
| 345 |
m, r := setup("benchCollectViews/Int64ObservableCounter") |
| 346 |
_, err := m.Int64ObservableCounter( |
| 347 |
"int64-observable-counter", |
| 348 |
metric.WithInt64Callback(int64Cback(s)), |
| 349 |
) |
| 350 |
assert.NoError(b, err) |
| 351 |
return r |
| 352 |
})) |
| 353 |
|
| 354 |
b.Run("Float64ObservableCounter", benchCollectAttrs(func(s attribute.Set) Reader { |
| 355 |
m, r := setup("benchCollectViews/Float64ObservableCounter") |
| 356 |
_, err := m.Float64ObservableCounter( |
| 357 |
"float64-observable-counter", |
| 358 |
metric.WithFloat64Callback(float64Cback(s)), |
| 359 |
) |
| 360 |
assert.NoError(b, err) |
| 361 |
return r |
| 362 |
})) |
| 363 |
|
| 364 |
b.Run("Int64ObservableUpDownCounter", benchCollectAttrs(func(s attribute.Set) Reader { |
| 365 |
m, r := setup("benchCollectViews/Int64ObservableUpDownCounter") |
| 366 |
_, err := m.Int64ObservableUpDownCounter( |
| 367 |
"int64-observable-up-down-counter", |
| 368 |
metric.WithInt64Callback(int64Cback(s)), |
| 369 |
) |
| 370 |
assert.NoError(b, err) |
| 371 |
return r |
| 372 |
})) |
| 373 |
|
| 374 |
b.Run("Float64ObservableUpDownCounter", benchCollectAttrs(func(s attribute.Set) Reader { |
| 375 |
m, r := setup("benchCollectViews/Float64ObservableUpDownCounter") |
| 376 |
_, err := m.Float64ObservableUpDownCounter( |
| 377 |
"float64-observable-up-down-counter", |
| 378 |
metric.WithFloat64Callback(float64Cback(s)), |
| 379 |
) |
| 380 |
assert.NoError(b, err) |
| 381 |
return r |
| 382 |
})) |
| 383 |
|
| 384 |
b.Run("Int64ObservableGauge", benchCollectAttrs(func(s attribute.Set) Reader { |
| 385 |
m, r := setup("benchCollectViews/Int64ObservableGauge") |
| 386 |
_, err := m.Int64ObservableGauge( |
| 387 |
"int64-observable-gauge", |
| 388 |
metric.WithInt64Callback(int64Cback(s)), |
| 389 |
) |
| 390 |
assert.NoError(b, err) |
| 391 |
return r |
| 392 |
})) |
| 393 |
|
| 394 |
b.Run("Float64ObservableGauge", benchCollectAttrs(func(s attribute.Set) Reader { |
| 395 |
m, r := setup("benchCollectViews/Float64ObservableGauge") |
| 396 |
_, err := m.Float64ObservableGauge( |
| 397 |
"float64-observable-gauge", |
| 398 |
metric.WithFloat64Callback(float64Cback(s)), |
| 399 |
) |
| 400 |
assert.NoError(b, err) |
| 401 |
return r |
| 402 |
})) |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
func int64Cback(s attribute.Set) metric.Int64Callback { |
| 407 |
opt := []metric.ObserveOption{metric.WithAttributeSet(s)} |
| 408 |
return func(_ context.Context, o metric.Int64Observer) error { |
| 409 |
o.Observe(1, opt...) |
| 410 |
return nil |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
func float64Cback(s attribute.Set) metric.Float64Callback { |
| 415 |
opt := []metric.ObserveOption{metric.WithAttributeSet(s)} |
| 416 |
return func(_ context.Context, o metric.Float64Observer) error { |
| 417 |
o.Observe(1, opt...) |
| 418 |
return nil |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
func benchCollectAttrs(setup func(attribute.Set) Reader) func(*testing.B) { |
| 423 |
out := new(metricdata.ResourceMetrics) |
| 424 |
run := func(reader Reader) func(b *testing.B) { |
| 425 |
return func(b *testing.B) { |
| 426 |
b.ReportAllocs() |
| 427 |
for n := 0; n < b.N; n++ { |
| 428 |
_ = reader.Collect(b.Context(), out) |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
return func(b *testing.B) { |
| 433 |
b.Run("Attributes/0", run(setup(*attribute.EmptySet()))) |
| 434 |
|
| 435 |
attrs := []attribute.KeyValue{attribute.Bool("K", true)} |
| 436 |
b.Run("Attributes/1", run(setup(attribute.NewSet(attrs...)))) |
| 437 |
|
| 438 |
for i := 2; i < 10; i++ { |
| 439 |
attrs = append(attrs, attribute.Int(strconv.Itoa(i), i)) |
| 440 |
} |
| 441 |
b.Run("Attributes/10", run(setup(attribute.NewSet(attrs...)))) |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
func BenchmarkExemplars(b *testing.B) { |
| 446 |
ctx := trace.ContextWithSpanContext(b.Context(), sampledSpanContext) |
| 447 |
|
| 448 |
attr := attribute.NewSet( |
| 449 |
attribute.String("user", "Alice"), |
| 450 |
attribute.Bool("admin", true), |
| 451 |
) |
| 452 |
|
| 453 |
setup := func(name string) (metric.Meter, Reader) { |
| 454 |
r := NewManualReader() |
| 455 |
v := NewView(Instrument{Name: "*"}, Stream{ |
| 456 |
AttributeFilter: func(kv attribute.KeyValue) bool { |
| 457 |
return kv.Key == attribute.Key("user") |
| 458 |
}, |
| 459 |
}) |
| 460 |
mp := NewMeterProvider(WithReader(r), WithView(v)) |
| 461 |
return mp.Meter(name), r |
| 462 |
} |
| 463 |
nCPU := runtime.NumCPU() // Size of the fixed reservoir used. |
| 464 |
|
| 465 |
b.Setenv("OTEL_GO_X_EXEMPLAR", "true") |
| 466 |
|
| 467 |
name := fmt.Sprintf("Int64Counter/%d", nCPU) |
| 468 |
b.Run(name, func(b *testing.B) { |
| 469 |
m, r := setup("Int64Counter") |
| 470 |
i, err := m.Int64Counter("int64-counter") |
| 471 |
assert.NoError(b, err) |
| 472 |
|
| 473 |
rm := newRM(metricdata.Sum[int64]{ |
| 474 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 475 |
{Exemplars: make([]metricdata.Exemplar[int64], 0, nCPU)}, |
| 476 |
}, |
| 477 |
}) |
| 478 |
e := &(rm.ScopeMetrics[0].Metrics[0].Data.(metricdata.Sum[int64]).DataPoints[0].Exemplars) |
| 479 |
|
| 480 |
b.ReportAllocs() |
| 481 |
b.ResetTimer() |
| 482 |
for n := 0; n < b.N; n++ { |
| 483 |
for j := 0; j < 2*nCPU; j++ { |
| 484 |
i.Add(ctx, 1, metric.WithAttributeSet(attr)) |
| 485 |
} |
| 486 |
|
| 487 |
_ = r.Collect(ctx, rm) |
| 488 |
assert.Len(b, *e, nCPU) |
| 489 |
} |
| 490 |
}) |
| 491 |
|
| 492 |
name = fmt.Sprintf("Int64Histogram/%d", nCPU) |
| 493 |
b.Run(name, func(b *testing.B) { |
| 494 |
m, r := setup("Int64Counter") |
| 495 |
i, err := m.Int64Histogram("int64-histogram") |
| 496 |
assert.NoError(b, err) |
| 497 |
|
| 498 |
rm := newRM(metricdata.Histogram[int64]{ |
| 499 |
DataPoints: []metricdata.HistogramDataPoint[int64]{ |
| 500 |
{Exemplars: make([]metricdata.Exemplar[int64], 0, 1)}, |
| 501 |
}, |
| 502 |
}) |
| 503 |
e := &(rm.ScopeMetrics[0].Metrics[0].Data.(metricdata.Histogram[int64]).DataPoints[0].Exemplars) |
| 504 |
|
| 505 |
b.ReportAllocs() |
| 506 |
b.ResetTimer() |
| 507 |
for n := 0; n < b.N; n++ { |
| 508 |
for j := 0; j < 2*nCPU; j++ { |
| 509 |
i.Record(ctx, 1, metric.WithAttributeSet(attr)) |
| 510 |
} |
| 511 |
|
| 512 |
_ = r.Collect(ctx, rm) |
| 513 |
assert.Len(b, *e, 1) |
| 514 |
} |
| 515 |
}) |
| 516 |
} |
| 517 |
|
| 518 |
func newRM(a metricdata.Aggregation) *metricdata.ResourceMetrics { |
| 519 |
return &metricdata.ResourceMetrics{ |
| 520 |
ScopeMetrics: []metricdata.ScopeMetrics{ |
| 521 |
{Metrics: []metricdata.Metrics{{Data: a}}}, |
| 522 |
}, |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
// BenchmarkEndToEndCounterAdd measures the performance of adding to a counter, |
| 527 |
// but takes into account the costs of constructing options to pass attributes |
| 528 |
// to the API in different user scenarios: |
| 529 |
// - In the "Precomputed" case, attributes are known ahead of time, and |
| 530 |
// options are not computed for each call. |
| 531 |
// - In the "Dynamic" case, attributes are not known ahead of time, and |
| 532 |
// options are computed for each counter increment. The "Dynamic" case |
| 533 |
// applies performance optimizations that are part of our contributor |
| 534 |
// guidelines. |
| 535 |
// - In the "Naive" case, the user uses the API and SDK in the simplest and |
| 536 |
// most obvious way without applying any performance optimizations. |
| 537 |
func BenchmarkEndToEndCounterAdd(b *testing.B) { |
| 538 |
testCounter := func(b *testing.B, mp metric.MeterProvider) metric.Float64Counter { |
| 539 |
meter := mp.Meter("BenchmarkEndToEndCounterAdd") |
| 540 |
counter, err := meter.Float64Counter("test.counter") |
| 541 |
assert.NoError(b, err) |
| 542 |
return counter |
| 543 |
} |
| 544 |
addOptPool := &sync.Pool{ |
| 545 |
New: func() any { |
| 546 |
const n = 1 // WithAttributeSet |
| 547 |
o := make([]metric.AddOption, 0, n) |
| 548 |
// Return a pointer to avoid extra allocation on Put(). |
| 549 |
return &o |
| 550 |
}, |
| 551 |
} |
| 552 |
ctx := b.Context() |
| 553 |
for _, mp := range []struct { |
| 554 |
name string |
| 555 |
provider func() metric.MeterProvider |
| 556 |
}{ |
| 557 |
{ |
| 558 |
name: "NoFilter", |
| 559 |
provider: func() metric.MeterProvider { |
| 560 |
return NewMeterProvider( |
| 561 |
WithReader(NewManualReader()), |
| 562 |
WithExemplarFilter(exemplar.AlwaysOffFilter), |
| 563 |
) |
| 564 |
}, |
| 565 |
}, |
| 566 |
{ |
| 567 |
name: "Filtered", |
| 568 |
provider: func() metric.MeterProvider { |
| 569 |
view := NewView( |
| 570 |
Instrument{ |
| 571 |
Name: "test.counter", |
| 572 |
}, |
| 573 |
// Filter out one attribute from each call. |
| 574 |
Stream{AttributeFilter: attribute.NewDenyKeysFilter("a")}, |
| 575 |
) |
| 576 |
return NewMeterProvider( |
| 577 |
WithView(view), |
| 578 |
WithReader(NewManualReader()), |
| 579 |
WithExemplarFilter(exemplar.AlwaysOffFilter), |
| 580 |
) |
| 581 |
}, |
| 582 |
}, |
| 583 |
} { |
| 584 |
b.Run(mp.name, func(b *testing.B) { |
| 585 |
for _, attrsLen := range []int{1, 5, 10} { |
| 586 |
attrPool := sync.Pool{ |
| 587 |
New: func() any { |
| 588 |
// Pre-allocate common capacity |
| 589 |
s := make([]attribute.KeyValue, 0, attrsLen) |
| 590 |
// Return a pointer to avoid extra allocation on Put(). |
| 591 |
return &s |
| 592 |
}, |
| 593 |
} |
| 594 |
b.Run(fmt.Sprintf("Attributes/%d", attrsLen), func(b *testing.B) { |
| 595 |
// This case shows the performance of our API + SDK when |
| 596 |
// following our contributor guidance for recording |
| 597 |
// cached attributes by passing attribute.Set: |
| 598 |
// https://github.com/open-telemetry/opentelemetry-go/blob/main/CONTRIBUTING.md#cache-common-attribute-sets-for-repeated-measurements |
| 599 |
b.Run("Precomputed/WithAttributeSet", func(b *testing.B) { |
| 600 |
counter := testCounter(b, mp.provider()) |
| 601 |
precomputedOpts := []metric.AddOption{ |
| 602 |
metric.WithAttributeSet(attribute.NewSet(attributes(attrsLen)...)), |
| 603 |
} |
| 604 |
b.ReportAllocs() |
| 605 |
b.RunParallel(func(pb *testing.PB) { |
| 606 |
for pb.Next() { |
| 607 |
counter.Add(ctx, 1, precomputedOpts...) |
| 608 |
} |
| 609 |
}) |
| 610 |
}) |
| 611 |
// This case shows the performance of our API + SDK when |
| 612 |
// following our contributor guidance for recording |
| 613 |
// cached attributes by passing []attribute.KeyValue: |
| 614 |
// https://github.com/open-telemetry/opentelemetry-go/blob/main/CONTRIBUTING.md#cache-common-attribute-sets-for-repeated-measurements |
| 615 |
b.Run("Precomputed/WithAttributes", func(b *testing.B) { |
| 616 |
counter := testCounter(b, mp.provider()) |
| 617 |
precomputedOpts := []metric.AddOption{metric.WithAttributes(attributes(attrsLen)...)} |
| 618 |
b.ReportAllocs() |
| 619 |
b.RunParallel(func(pb *testing.PB) { |
| 620 |
for pb.Next() { |
| 621 |
counter.Add(ctx, 1, precomputedOpts...) |
| 622 |
} |
| 623 |
}) |
| 624 |
}) |
| 625 |
// This case shows the performance of our API + SDK when |
| 626 |
// following our contributor guidance for recording |
| 627 |
// varying attributes by passing attribute.Set: |
| 628 |
// https://github.com/open-telemetry/opentelemetry-go/blob/main/CONTRIBUTING.md#attribute-and-option-allocation-management |
| 629 |
b.Run("Dynamic/WithAttributeSet", func(b *testing.B) { |
| 630 |
counter := testCounter(b, mp.provider()) |
| 631 |
b.ReportAllocs() |
| 632 |
b.RunParallel(func(pb *testing.PB) { |
| 633 |
for pb.Next() { |
| 634 |
// Wrap in a function so we can use defer. |
| 635 |
func() { |
| 636 |
attrsSlice := attrPool.Get().(*[]attribute.KeyValue) |
| 637 |
defer func() { |
| 638 |
*attrsSlice = (*attrsSlice)[:0] // Reset. |
| 639 |
attrPool.Put(attrsSlice) |
| 640 |
}() |
| 641 |
*attrsSlice = appendAttributes(*attrsSlice, attrsLen) |
| 642 |
addOpt := addOptPool.Get().(*[]metric.AddOption) |
| 643 |
defer func() { |
| 644 |
*addOpt = (*addOpt)[:0] |
| 645 |
addOptPool.Put(addOpt) |
| 646 |
}() |
| 647 |
set := attribute.NewSet(*attrsSlice...) |
| 648 |
*addOpt = append(*addOpt, metric.WithAttributeSet(set)) |
| 649 |
counter.Add(ctx, 1, *addOpt...) |
| 650 |
}() |
| 651 |
} |
| 652 |
}) |
| 653 |
}) |
| 654 |
// This case shows the performance of our API + SDK when |
| 655 |
// following our contributor guidance for recording |
| 656 |
// varying attributes by passing []attribute.KeyValue: |
| 657 |
// https://github.com/open-telemetry/opentelemetry-go/blob/main/CONTRIBUTING.md#attribute-and-option-allocation-management |
| 658 |
b.Run("Dynamic/WithAttributes", func(b *testing.B) { |
| 659 |
counter := testCounter(b, mp.provider()) |
| 660 |
b.ReportAllocs() |
| 661 |
b.RunParallel(func(pb *testing.PB) { |
| 662 |
for pb.Next() { |
| 663 |
// Wrap in a function so we can use defer. |
| 664 |
func() { |
| 665 |
attrsSlice := attrPool.Get().(*[]attribute.KeyValue) |
| 666 |
defer func() { |
| 667 |
*attrsSlice = (*attrsSlice)[:0] // Reset. |
| 668 |
attrPool.Put(attrsSlice) |
| 669 |
}() |
| 670 |
*attrsSlice = appendAttributes(*attrsSlice, attrsLen) |
| 671 |
addOpt := addOptPool.Get().(*[]metric.AddOption) |
| 672 |
defer func() { |
| 673 |
*addOpt = (*addOpt)[:0] |
| 674 |
addOptPool.Put(addOpt) |
| 675 |
}() |
| 676 |
*addOpt = append(*addOpt, metric.WithAttributes(*attrsSlice...)) |
| 677 |
counter.Add(ctx, 1, *addOpt...) |
| 678 |
}() |
| 679 |
} |
| 680 |
}) |
| 681 |
}) |
| 682 |
// This case shows the performance of our API + SDK when |
| 683 |
// users use it in the "obvious" way, without explicitly |
| 684 |
// trying to optimize for performance. |
| 685 |
b.Run("Naive/WithAttributes", func(b *testing.B) { |
| 686 |
counter := testCounter(b, mp.provider()) |
| 687 |
b.ReportAllocs() |
| 688 |
b.RunParallel(func(pb *testing.PB) { |
| 689 |
for pb.Next() { |
| 690 |
counter.Add(ctx, 1, metric.WithAttributes(attributes(attrsLen)...)) |
| 691 |
} |
| 692 |
}) |
| 693 |
}) |
| 694 |
}) |
| 695 |
} |
| 696 |
}) |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
func attributes(number int) []attribute.KeyValue { |
| 701 |
return appendAttributes(make([]attribute.KeyValue, 0, number), number) |
| 702 |
} |
| 703 |
|
| 704 |
func appendAttributes(kvs []attribute.KeyValue, number int) []attribute.KeyValue { |
| 705 |
switch number { |
| 706 |
case 1: |
| 707 |
return append(kvs, |
| 708 |
attribute.String("a", "a"), |
| 709 |
) |
| 710 |
case 5: |
| 711 |
return append(kvs, |
| 712 |
attribute.String("a", "a"), |
| 713 |
attribute.String("b", "b"), |
| 714 |
attribute.String("c", "c"), |
| 715 |
attribute.String("d", "d"), |
| 716 |
attribute.String("e", "e"), |
| 717 |
) |
| 718 |
case 10: |
| 719 |
return append(kvs, |
| 720 |
attribute.String("a", "a"), |
| 721 |
attribute.String("b", "b"), |
| 722 |
attribute.String("c", "c"), |
| 723 |
attribute.String("d", "d"), |
| 724 |
attribute.String("e", "e"), |
| 725 |
attribute.String("f", "f"), |
| 726 |
attribute.String("g", "g"), |
| 727 |
attribute.String("h", "h"), |
| 728 |
attribute.String("i", "i"), |
| 729 |
attribute.String("j", "j"), |
| 730 |
) |
| 731 |
default: |
| 732 |
panic("unknown number of attributes") |
| 733 |
} |
| 734 |
} |