@@ -6,6 +6,7 @@ package pprof
6
6
7
7
import (
8
8
"context"
9
+ "fmt"
9
10
"reflect"
10
11
"slices"
11
12
"strings"
@@ -111,3 +112,75 @@ func TestLabelMapStringer(t *testing.T) {
111
112
}
112
113
}
113
114
}
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