Skip to content

Commit 89d4f56

Browse files
felixgepull[bot]
authored andcommitted
runtime/pprof: add label benchmark
Add several benchmarks for pprof labels to analyze the impact of follow-up CLs. Change-Id: Ifae39cfe83ec93858fce9e3af6c1be024ba76736 Reviewed-on: https://go-review.googlesource.com/c/go/+/574515 Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: David Chase <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent 2bb6e16 commit 89d4f56

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

src/runtime/pprof/label_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package pprof
66

77
import (
88
"context"
9+
"fmt"
910
"reflect"
1011
"slices"
1112
"strings"
@@ -111,3 +112,75 @@ func TestLabelMapStringer(t *testing.T) {
111112
}
112113
}
113114
}
115+
116+
func BenchmarkLabels(b *testing.B) {
117+
b.Run("set-one", func(b *testing.B) {
118+
b.ReportAllocs()
119+
b.ResetTimer()
120+
for i := 0; i < b.N; i++ {
121+
Do(context.Background(), Labels("key", "value"), func(context.Context) {})
122+
}
123+
})
124+
125+
b.Run("merge-one", func(b *testing.B) {
126+
ctx := WithLabels(context.Background(), Labels("key1", "val1"))
127+
128+
b.ReportAllocs()
129+
b.ResetTimer()
130+
for i := 0; i < b.N; i++ {
131+
Do(ctx, Labels("key2", "value2"), func(context.Context) {})
132+
}
133+
})
134+
135+
b.Run("overwrite-one", func(b *testing.B) {
136+
ctx := WithLabels(context.Background(), Labels("key", "val"))
137+
138+
b.ReportAllocs()
139+
b.ResetTimer()
140+
for i := 0; i < b.N; i++ {
141+
Do(ctx, Labels("key", "value"), func(context.Context) {})
142+
}
143+
})
144+
145+
for _, scenario := range []string{"ordered", "unordered"} {
146+
var labels []string
147+
for i := 0; i < 10; i++ {
148+
labels = append(labels, fmt.Sprintf("key%03d", i), fmt.Sprintf("value%03d", i))
149+
}
150+
if scenario == "unordered" {
151+
labels[0], labels[len(labels)-1] = labels[len(labels)-1], labels[0]
152+
}
153+
154+
b.Run(scenario, func(b *testing.B) {
155+
b.Run("set-many", func(b *testing.B) {
156+
b.ReportAllocs()
157+
b.ResetTimer()
158+
for i := 0; i < b.N; i++ {
159+
Do(context.Background(), Labels(labels...), func(context.Context) {})
160+
}
161+
})
162+
163+
b.Run("merge-many", func(b *testing.B) {
164+
ctx := WithLabels(context.Background(), Labels(labels[:len(labels)/2]...))
165+
166+
b.ResetTimer()
167+
b.ReportAllocs()
168+
for i := 0; i < b.N; i++ {
169+
Do(ctx, Labels(labels[len(labels)/2:]...), func(context.Context) {})
170+
}
171+
})
172+
173+
b.Run("overwrite-many", func(b *testing.B) {
174+
ctx := WithLabels(context.Background(), Labels(labels...))
175+
176+
b.ReportAllocs()
177+
b.ResetTimer()
178+
for i := 0; i < b.N; i++ {
179+
Do(ctx, Labels(labels...), func(context.Context) {})
180+
}
181+
})
182+
})
183+
}
184+
185+
// TODO: hit slow path in Labels
186+
}

0 commit comments

Comments
 (0)