1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +02:00

Add a benchmark for histogram allocations (#3635)

Co-authored-by: Chester Cheung <cheung.zhy.csu@gmail.com>
This commit is contained in:
Aaron Clawson
2023-02-02 12:16:25 -06:00
committed by GitHub
parent aa5122490e
commit 5e8eb855bf
+42
View File
@@ -16,10 +16,12 @@ package metric // import "go.opentelemetry.io/otel/sdk/metric"
import (
"context"
"fmt"
"testing"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
)
func benchCounter(b *testing.B, views ...View) (context.Context, Reader, instrument.Int64Counter) {
@@ -105,3 +107,43 @@ func BenchmarkCounterCollectTenAttrs(b *testing.B) {
_, _ = rdr.Collect(ctx)
}
}
func BenchmarkCollectHistograms(b *testing.B) {
b.Run("1", benchCollectHistograms(1))
b.Run("5", benchCollectHistograms(5))
b.Run("10", benchCollectHistograms(10))
b.Run("25", benchCollectHistograms(25))
}
func benchCollectHistograms(count int) func(*testing.B) {
ctx := context.Background()
r := NewManualReader()
mtr := NewMeterProvider(
WithReader(r),
).Meter("sdk/metric/bench/histogram")
for i := 0; i < count; i++ {
name := fmt.Sprintf("fake data %d", i)
h, _ := mtr.Int64Histogram(name)
h.Record(ctx, 1)
}
// Store bechmark results in a closure to prevent the compiler from
// inlining and skipping the function.
var (
collectedMetrics metricdata.ResourceMetrics
)
return func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
collectedMetrics, _ = r.Collect(ctx)
if len(collectedMetrics.ScopeMetrics[0].Metrics) != count {
b.Fatalf("got %d metrics, want %d", len(collectedMetrics.ScopeMetrics[0].Metrics), count)
}
}
}
}