]> Cypherpunks repositories - gostls13.git/commitdiff
runtime/pprof: add label benchmark
authorFelix Geisendörfer <felix.geisendoerfer@datadoghq.com>
Tue, 26 Mar 2024 19:23:30 +0000 (20:23 +0100)
committerGopher Robot <gobot@golang.org>
Thu, 7 Nov 2024 19:24:54 +0000 (19:24 +0000)
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 <iant@golang.org>
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
src/runtime/pprof/label_test.go

index 38d9e80dfc2a58df6e4093e0611433df4113c96f..5cab9f21a5701ae790128c4ef9a5fd745fa74b99 100644 (file)
@@ -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
+}