import (
"context"
+ "fmt"
"reflect"
"slices"
"strings"
}
}
}
+
+func BenchmarkLabels(b *testing.B) {
+ b.Run("set-one", func(b *testing.B) {
+ b.ReportAllocs()
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ Do(context.Background(), Labels("key", "value"), func(context.Context) {})
+ }
+ })
+
+ b.Run("merge-one", func(b *testing.B) {
+ ctx := WithLabels(context.Background(), Labels("key1", "val1"))
+
+ b.ReportAllocs()
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ Do(ctx, Labels("key2", "value2"), func(context.Context) {})
+ }
+ })
+
+ b.Run("overwrite-one", func(b *testing.B) {
+ ctx := WithLabels(context.Background(), Labels("key", "val"))
+
+ b.ReportAllocs()
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ Do(ctx, Labels("key", "value"), func(context.Context) {})
+ }
+ })
+
+ for _, scenario := range []string{"ordered", "unordered"} {
+ var labels []string
+ for i := 0; i < 10; i++ {
+ labels = append(labels, fmt.Sprintf("key%03d", i), fmt.Sprintf("value%03d", i))
+ }
+ if scenario == "unordered" {
+ labels[0], labels[len(labels)-1] = labels[len(labels)-1], labels[0]
+ }
+
+ b.Run(scenario, func(b *testing.B) {
+ b.Run("set-many", func(b *testing.B) {
+ b.ReportAllocs()
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ Do(context.Background(), Labels(labels...), func(context.Context) {})
+ }
+ })
+
+ b.Run("merge-many", func(b *testing.B) {
+ ctx := WithLabels(context.Background(), Labels(labels[:len(labels)/2]...))
+
+ b.ResetTimer()
+ b.ReportAllocs()
+ for i := 0; i < b.N; i++ {
+ Do(ctx, Labels(labels[len(labels)/2:]...), func(context.Context) {})
+ }
+ })
+
+ b.Run("overwrite-many", func(b *testing.B) {
+ ctx := WithLabels(context.Background(), Labels(labels...))
+
+ b.ReportAllocs()
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ Do(ctx, Labels(labels...), func(context.Context) {})
+ }
+ })
+ })
+ }
+
+ // TODO: hit slow path in Labels
+}