From 411ba0ae8608a0829a185d83f122d83c8a51c754 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Felix=20Geisend=C3=B6rfer?= Date: Tue, 26 Mar 2024 20:23:30 +0100 Subject: [PATCH] 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 Reviewed-by: David Chase LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Pratt --- src/runtime/pprof/label_test.go | 73 +++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/src/runtime/pprof/label_test.go b/src/runtime/pprof/label_test.go index 38d9e80dfc..5cab9f21a5 100644 --- a/src/runtime/pprof/label_test.go +++ b/src/runtime/pprof/label_test.go @@ -6,6 +6,7 @@ package pprof import ( "context" + "fmt" "reflect" "slices" "strings" @@ -111,3 +112,75 @@ func TestLabelMapStringer(t *testing.T) { } } } + +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 +} -- 2.48.1