1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-25 22:41:46 +02:00
Files
opentelemetry-go/attribute/benchmark_test.go

383 lines
8.8 KiB
Go
Raw Normal View History

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package attribute_test
import (
"testing"
"go.opentelemetry.io/otel/attribute"
)
// Store results in a file scope var to ensure compiler does not optimize the
// test away.
var (
outV attribute.Value
outKV attribute.KeyValue
outBool bool
outBoolSlice []bool
outInt64 int64
outInt64Slice []int64
outFloat64 float64
outFloat64Slice []float64
outStr string
outStrSlice []string
)
func benchmarkEmit(kv attribute.KeyValue) func(*testing.B) {
return func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outStr = kv.Value.Emit()
}
}
}
func BenchmarkBool(b *testing.B) {
k, v := "bool", true
kv := attribute.Bool(k, v)
b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outV = attribute.BoolValue(v)
}
})
b.Run("KeyValue", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outKV = attribute.Bool(k, v)
}
})
b.Run("AsBool", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outBool = kv.Value.AsBool()
}
})
b.Run("Emit", benchmarkEmit(kv))
}
func BenchmarkBoolSlice(b *testing.B) {
k, v := "bool slice", []bool{true, false, true}
kv := attribute.BoolSlice(k, v)
b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outV = attribute.BoolSliceValue(v)
}
})
b.Run("KeyValue", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outKV = attribute.BoolSlice(k, v)
}
})
b.Run("AsBoolSlice", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outBoolSlice = kv.Value.AsBoolSlice()
}
})
b.Run("Emit", benchmarkEmit(kv))
}
func BenchmarkInt(b *testing.B) {
k, v := "int", int(42)
kv := attribute.Int(k, v)
b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outV = attribute.IntValue(v)
}
})
b.Run("KeyValue", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outKV = attribute.Int(k, v)
}
})
b.Run("Emit", benchmarkEmit(kv))
}
func BenchmarkIntSlice(b *testing.B) {
k, v := "int slice", []int{42, -3, 12}
kv := attribute.IntSlice(k, v)
b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outV = attribute.IntSliceValue(v)
}
})
b.Run("KeyValue", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outKV = attribute.IntSlice(k, v)
}
})
b.Run("Emit", benchmarkEmit(kv))
}
func BenchmarkInt64(b *testing.B) {
k, v := "int64", int64(42)
kv := attribute.Int64(k, v)
b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outV = attribute.Int64Value(v)
}
})
b.Run("KeyValue", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outKV = attribute.Int64(k, v)
}
})
b.Run("AsInt64", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outInt64 = kv.Value.AsInt64()
}
})
b.Run("Emit", benchmarkEmit(kv))
}
func BenchmarkInt64Slice(b *testing.B) {
k, v := "int64 slice", []int64{42, -3, 12}
kv := attribute.Int64Slice(k, v)
b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outV = attribute.Int64SliceValue(v)
}
})
b.Run("KeyValue", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outKV = attribute.Int64Slice(k, v)
}
})
b.Run("AsInt64Slice", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outInt64Slice = kv.Value.AsInt64Slice()
}
})
b.Run("Emit", benchmarkEmit(kv))
}
func BenchmarkFloat64(b *testing.B) {
k, v := "float64", float64(42)
kv := attribute.Float64(k, v)
b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outV = attribute.Float64Value(v)
}
})
b.Run("KeyValue", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outKV = attribute.Float64(k, v)
}
})
b.Run("AsFloat64", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outFloat64 = kv.Value.AsFloat64()
}
})
b.Run("Emit", benchmarkEmit(kv))
}
func BenchmarkFloat64Slice(b *testing.B) {
k, v := "float64 slice", []float64{42, -3, 12}
kv := attribute.Float64Slice(k, v)
b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outV = attribute.Float64SliceValue(v)
}
})
b.Run("KeyValue", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outKV = attribute.Float64Slice(k, v)
}
})
b.Run("AsFloat64Slice", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outFloat64Slice = kv.Value.AsFloat64Slice()
}
})
b.Run("Emit", benchmarkEmit(kv))
}
func BenchmarkString(b *testing.B) {
k, v := "string", "42"
kv := attribute.String(k, v)
b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outV = attribute.StringValue(v)
}
})
b.Run("KeyValue", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outKV = attribute.String(k, v)
}
})
b.Run("AsString", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outStr = kv.Value.AsString()
}
})
b.Run("Emit", benchmarkEmit(kv))
}
func BenchmarkStringSlice(b *testing.B) {
k, v := "float64 slice", []string{"forty-two", "negative three", "twelve"}
kv := attribute.StringSlice(k, v)
b.Run("Value", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outV = attribute.StringSliceValue(v)
}
})
b.Run("KeyValue", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outKV = attribute.StringSlice(k, v)
}
})
b.Run("AsStringSlice", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
outStrSlice = kv.Value.AsStringSlice()
}
})
b.Run("Emit", benchmarkEmit(kv))
}
Add benchmark for map access using attribute Equivalent (#7123) I am looking into I am looking into https://promlabs.com/blog/2025/07/17/why-i-recommend-native-prometheus-instrumentation-over-opentelemetry/#comparing-counter-increment-performance, and was trying to figure out why incrementing a counter with 10 attributes was so much slower than incrementing a counter with no attributes, or 1 attribute: ``` $ go test -run=xxxxxMatchNothingxxxxx -cpu=1 -test.benchtime=1s -bench=BenchmarkSyncMeasure/NoView/Int64Counter/Attributes goos: linux goarch: amd64 pkg: go.opentelemetry.io/otel/sdk/metric cpu: Intel(R) Xeon(R) CPU @ 2.20GHz BenchmarkSyncMeasure/NoView/Int64Counter/Attributes/0 9905773 121.3 ns/op BenchmarkSyncMeasure/NoView/Int64Counter/Attributes/1 4079145 296.5 ns/op BenchmarkSyncMeasure/NoView/Int64Counter/Attributes/10 781627 1531 ns/op ``` Looking at the profile, most of the time is spent in "runtime.mapKeyError2" within "runtime.mapaccess2". My best guess is that whatever we are using for Equivalent() is not very performant when used as a map key. This seems like a good opportunity to greatly improve the performance of our metrics (and probably other signals) API + SDK. To start, i'm adding a simple benchmark within the attribute package to isolate the issue. Results: ``` $ go test -run '^$' -bench '^BenchmarkEquivalentMapAccess' -benchtime .1s -cpu 1 -benchmem goos: linux goarch: amd64 pkg: go.opentelemetry.io/otel/attribute cpu: Intel(R) Xeon(R) CPU @ 2.20GHz BenchmarkEquivalentMapAccess/Empty 2220508 53.58 ns/op 0 B/op 0 allocs/op BenchmarkEquivalentMapAccess/1_string_attribute 622770 196.7 ns/op 0 B/op 0 allocs/op BenchmarkEquivalentMapAccess/10_string_attributes 77462 1558 ns/op 0 B/op 0 allocs/op BenchmarkEquivalentMapAccess/1_int_attribute 602163 197.7 ns/op 0 B/op 0 allocs/op BenchmarkEquivalentMapAccess/10_int_attributes 76603 1569 ns/op 0 B/op 0 allocs/op ``` This shows that it is the map lookup and storage itself that is making the metrics API+SDK perform much worse with more attributes. Some optimization ideas include: * Most attribute sets are likely to be just numbers and strings. Can we make a fast path for sets that don't include complex attributes? * We encourage improving performance of the metrics API by re-using attribute sets where possible. If we can lazily compute+cache a "faster" map key, that will have a big performance improvement when attribute sets are re-used. * compute a uint64 hash using something like https://github.com/gohugoio/hashstructure, or something similar to what prometheus/client_golang does: https://github.com/prometheus/common/blob/c79a891c6c28ce135a2ac082b721c2dacc2269a8/model/signature.go#L31 --------- Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Flc゛ <four_leaf_clover@foxmail.com>
2025-08-06 14:45:22 -04:00
Add benchmark for set equality (#7262) Forked from https://github.com/open-telemetry/opentelemetry-go/pull/7175 ``` $ go test -timeout 60s -run=xxxxxMatchNothingxxxxx -test.benchtime=10ms -count 6 -cpu 1 -bench=BenchmarkEquals ./... goos: linux goarch: amd64 pkg: go.opentelemetry.io/otel/attribute cpu: Intel(R) Xeon(R) CPU @ 2.20GHz BenchmarkEquals/Empty 1314884 9.008 ns/op BenchmarkEquals/Empty 1875915 8.461 ns/op BenchmarkEquals/Empty 1461613 7.749 ns/op BenchmarkEquals/Empty 1636912 9.359 ns/op BenchmarkEquals/Empty 1863039 6.355 ns/op BenchmarkEquals/Empty 1789053 6.336 ns/op BenchmarkEquals/1_string_attribute 674168 16.92 ns/op BenchmarkEquals/1_string_attribute 701983 16.42 ns/op BenchmarkEquals/1_string_attribute 692001 16.52 ns/op BenchmarkEquals/1_string_attribute 687970 16.29 ns/op BenchmarkEquals/1_string_attribute 751766 16.58 ns/op BenchmarkEquals/1_string_attribute 703534 16.88 ns/op BenchmarkEquals/10_string_attributes 85400 137.1 ns/op BenchmarkEquals/10_string_attributes 91045 136.1 ns/op BenchmarkEquals/10_string_attributes 90973 150.7 ns/op BenchmarkEquals/10_string_attributes 62877 177.5 ns/op BenchmarkEquals/10_string_attributes 90780 194.2 ns/op BenchmarkEquals/10_string_attributes 91058 144.6 ns/op BenchmarkEquals/1_int_attribute 624625 18.72 ns/op BenchmarkEquals/1_int_attribute 689478 16.03 ns/op BenchmarkEquals/1_int_attribute 719173 15.68 ns/op BenchmarkEquals/1_int_attribute 707005 16.18 ns/op BenchmarkEquals/1_int_attribute 752048 15.94 ns/op BenchmarkEquals/1_int_attribute 752034 16.23 ns/op BenchmarkEquals/10_int_attributes 90302 132.5 ns/op BenchmarkEquals/10_int_attributes 89929 131.9 ns/op BenchmarkEquals/10_int_attributes 86578 135.2 ns/op BenchmarkEquals/10_int_attributes 90482 133.1 ns/op BenchmarkEquals/10_int_attributes 90255 132.0 ns/op BenchmarkEquals/10_int_attributes 87615 134.6 ns/op PASS ok go.opentelemetry.io/otel/attribute 0.578s PASS ok go.opentelemetry.io/otel/attribute/internal 0.017s ``` --------- Co-authored-by: Flc゛ <four_leaf_clover@foxmail.com> Co-authored-by: Robert Pająk <pellared@hotmail.com>
2025-08-28 14:13:55 -04:00
func BenchmarkSetEquals(b *testing.B) {
b.Run("Empty", func(b *testing.B) {
benchmarkSetEquals(b, attribute.EmptySet())
})
b.Run("1 string attribute", func(b *testing.B) {
set := attribute.NewSet(attribute.String("string", "42"))
benchmarkSetEquals(b, &set)
})
b.Run("10 string attributes", func(b *testing.B) {
set := attribute.NewSet(
attribute.String("a", "42"),
attribute.String("b", "42"),
attribute.String("c", "42"),
attribute.String("d", "42"),
attribute.String("e", "42"),
attribute.String("f", "42"),
attribute.String("g", "42"),
attribute.String("h", "42"),
attribute.String("i", "42"),
attribute.String("j", "42"),
)
benchmarkSetEquals(b, &set)
})
b.Run("1 int attribute", func(b *testing.B) {
set := attribute.NewSet(attribute.Int("string", 42))
benchmarkSetEquals(b, &set)
})
b.Run("10 int attributes", func(b *testing.B) {
set := attribute.NewSet(
attribute.Int("a", 42),
attribute.Int("b", 42),
attribute.Int("c", 42),
attribute.Int("d", 42),
attribute.Int("e", 42),
attribute.Int("f", 42),
attribute.Int("g", 42),
attribute.Int("h", 42),
attribute.Int("i", 42),
attribute.Int("j", 42),
)
benchmarkSetEquals(b, &set)
})
}
func benchmarkSetEquals(b *testing.B, set *attribute.Set) {
b.ResetTimer()
for range b.N {
if !set.Equals(set) {
b.Fatal("not equal")
}
}
}
Add benchmark for map access using attribute Equivalent (#7123) I am looking into I am looking into https://promlabs.com/blog/2025/07/17/why-i-recommend-native-prometheus-instrumentation-over-opentelemetry/#comparing-counter-increment-performance, and was trying to figure out why incrementing a counter with 10 attributes was so much slower than incrementing a counter with no attributes, or 1 attribute: ``` $ go test -run=xxxxxMatchNothingxxxxx -cpu=1 -test.benchtime=1s -bench=BenchmarkSyncMeasure/NoView/Int64Counter/Attributes goos: linux goarch: amd64 pkg: go.opentelemetry.io/otel/sdk/metric cpu: Intel(R) Xeon(R) CPU @ 2.20GHz BenchmarkSyncMeasure/NoView/Int64Counter/Attributes/0 9905773 121.3 ns/op BenchmarkSyncMeasure/NoView/Int64Counter/Attributes/1 4079145 296.5 ns/op BenchmarkSyncMeasure/NoView/Int64Counter/Attributes/10 781627 1531 ns/op ``` Looking at the profile, most of the time is spent in "runtime.mapKeyError2" within "runtime.mapaccess2". My best guess is that whatever we are using for Equivalent() is not very performant when used as a map key. This seems like a good opportunity to greatly improve the performance of our metrics (and probably other signals) API + SDK. To start, i'm adding a simple benchmark within the attribute package to isolate the issue. Results: ``` $ go test -run '^$' -bench '^BenchmarkEquivalentMapAccess' -benchtime .1s -cpu 1 -benchmem goos: linux goarch: amd64 pkg: go.opentelemetry.io/otel/attribute cpu: Intel(R) Xeon(R) CPU @ 2.20GHz BenchmarkEquivalentMapAccess/Empty 2220508 53.58 ns/op 0 B/op 0 allocs/op BenchmarkEquivalentMapAccess/1_string_attribute 622770 196.7 ns/op 0 B/op 0 allocs/op BenchmarkEquivalentMapAccess/10_string_attributes 77462 1558 ns/op 0 B/op 0 allocs/op BenchmarkEquivalentMapAccess/1_int_attribute 602163 197.7 ns/op 0 B/op 0 allocs/op BenchmarkEquivalentMapAccess/10_int_attributes 76603 1569 ns/op 0 B/op 0 allocs/op ``` This shows that it is the map lookup and storage itself that is making the metrics API+SDK perform much worse with more attributes. Some optimization ideas include: * Most attribute sets are likely to be just numbers and strings. Can we make a fast path for sets that don't include complex attributes? * We encourage improving performance of the metrics API by re-using attribute sets where possible. If we can lazily compute+cache a "faster" map key, that will have a big performance improvement when attribute sets are re-used. * compute a uint64 hash using something like https://github.com/gohugoio/hashstructure, or something similar to what prometheus/client_golang does: https://github.com/prometheus/common/blob/c79a891c6c28ce135a2ac082b721c2dacc2269a8/model/signature.go#L31 --------- Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Flc゛ <four_leaf_clover@foxmail.com>
2025-08-06 14:45:22 -04:00
// BenchmarkEquivalentMapAccess measures how expensive it is to use
// Equivalent() as a map key. This is on the hot path for making synchronous
// measurements on the metrics API/SDK. It will likely be on the hot path for
// the trace and logs API/SDK in the future.
func BenchmarkEquivalentMapAccess(b *testing.B) {
b.Run("Empty", func(b *testing.B) {
benchmarkEquivalentMapAccess(b, attribute.EmptySet())
})
b.Run("1 string attribute", func(b *testing.B) {
set := attribute.NewSet(attribute.String("string", "42"))
benchmarkEquivalentMapAccess(b, &set)
})
b.Run("10 string attributes", func(b *testing.B) {
set := attribute.NewSet(
attribute.String("a", "42"),
attribute.String("b", "42"),
attribute.String("c", "42"),
attribute.String("d", "42"),
attribute.String("e", "42"),
attribute.String("f", "42"),
attribute.String("g", "42"),
attribute.String("h", "42"),
attribute.String("i", "42"),
attribute.String("j", "42"),
)
benchmarkEquivalentMapAccess(b, &set)
})
b.Run("1 int attribute", func(b *testing.B) {
set := attribute.NewSet(attribute.Int("string", 42))
benchmarkEquivalentMapAccess(b, &set)
})
b.Run("10 int attributes", func(b *testing.B) {
set := attribute.NewSet(
attribute.Int("a", 42),
attribute.Int("b", 42),
attribute.Int("c", 42),
attribute.Int("d", 42),
attribute.Int("e", 42),
attribute.Int("f", 42),
attribute.Int("g", 42),
attribute.Int("h", 42),
attribute.Int("i", 42),
attribute.Int("j", 42),
)
benchmarkEquivalentMapAccess(b, &set)
})
}
func benchmarkEquivalentMapAccess(b *testing.B, set *attribute.Set) {
values := map[attribute.Distinct]int{}
b.ResetTimer()
for range b.N {
values[set.Equivalent()]++
}
}