|
14 | 14 | package prometheus
|
15 | 15 |
|
16 | 16 | import (
|
| 17 | + "fmt" |
17 | 18 | "reflect"
|
18 | 19 | "testing"
|
19 | 20 | "time"
|
@@ -192,49 +193,87 @@ func TestMakeLabelPairs(t *testing.T) {
|
192 | 193 | }
|
193 | 194 | }
|
194 | 195 |
|
195 |
| -func Benchmark_MakeLabelPairs(b *testing.B) { |
196 |
| - benchFunc := func(desc *Desc, variableLabelValues []string) { |
197 |
| - MakeLabelPairs(desc, variableLabelValues) |
198 |
| - } |
| 196 | +var new1LabelDescFunc = func() *Desc { |
| 197 | + return NewDesc( |
| 198 | + "metric", |
| 199 | + "help", |
| 200 | + []string{"var-label-1"}, |
| 201 | + Labels{"const-label-1": "value"}) |
| 202 | +} |
| 203 | + |
| 204 | +var new3LabelsDescFunc = func() *Desc { |
| 205 | + return NewDesc( |
| 206 | + "metric", |
| 207 | + "help", |
| 208 | + []string{"var-label-1", "var-label-3", "var-label-2"}, |
| 209 | + Labels{"const-label-1": "value", "const-label-3": "value", "const-label-2": "value"}) |
| 210 | +} |
| 211 | + |
| 212 | +var new10LabelsDescFunc = func() *Desc { |
| 213 | + return NewDesc( |
| 214 | + "metric", |
| 215 | + "help", |
| 216 | + []string{"var-label-5", "var-label-1", "var-label-3", "var-label-2", "var-label-10", "var-label-4", "var-label-7", "var-label-8", "var-label-9", "var-label-6"}, |
| 217 | + Labels{"const-label-4": "value", "const-label-1": "value", "const-label-7": "value", "const-label-2": "value", "const-label-9": "value", "const-label-8": "value", "const-label-10": "value", "const-label-3": "value", "const-label-6": "value", "const-label-5": "value"}) |
| 218 | +} |
199 | 219 |
|
200 |
| - benchmarks := []struct { |
201 |
| - name string |
202 |
| - bench func(desc *Desc, variableLabelValues []string) |
| 220 | +func BenchmarkMakeLabelPairs(b *testing.B) { |
| 221 | + for _, bm := range []struct { |
203 | 222 | desc *Desc
|
204 |
| - variableLabelValues []string |
| 223 | + makeLabelPairValues []string |
205 | 224 | }{
|
206 | 225 | {
|
207 |
| - name: "1 label", |
208 |
| - desc: NewDesc( |
209 |
| - "metric", |
210 |
| - "help", |
211 |
| - []string{"var-label-1"}, |
212 |
| - Labels{"const-label-1": "value"}), |
213 |
| - variableLabelValues: []string{"value"}, |
| 226 | + desc: new1LabelDescFunc(), |
| 227 | + makeLabelPairValues: []string{"value"}, |
214 | 228 | },
|
215 | 229 | {
|
216 |
| - name: "3 labels", |
217 |
| - desc: NewDesc( |
218 |
| - "metric", |
219 |
| - "help", |
220 |
| - []string{"var-label-1", "var-label-3", "var-label-2"}, |
221 |
| - Labels{"const-label-1": "value", "const-label-3": "value", "const-label-2": "value"}), |
222 |
| - variableLabelValues: []string{"value", "value", "value"}, |
| 230 | + desc: new3LabelsDescFunc(), |
| 231 | + makeLabelPairValues: []string{"value", "value", "value"}, |
223 | 232 | },
|
224 | 233 | {
|
225 |
| - name: "10 labels", |
226 |
| - desc: NewDesc( |
227 |
| - "metric", |
228 |
| - "help", |
229 |
| - []string{"var-label-5", "var-label-1", "var-label-3", "var-label-2", "var-label-10", "var-label-4", "var-label-7", "var-label-8", "var-label-9"}, |
230 |
| - Labels{"const-label-4": "value", "const-label-1": "value", "const-label-7": "value", "const-label-2": "value", "const-label-9": "value", "const-label-8": "value", "const-label-10": "value", "const-label-3": "value", "const-label-6": "value", "const-label-5": "value"}), |
231 |
| - variableLabelValues: []string{"value", "value", "value", "value", "value", "value", "value", "value", "value", "value"}, |
| 234 | + desc: new10LabelsDescFunc(), |
| 235 | + makeLabelPairValues: []string{"value", "value", "value", "value", "value", "value", "value", "value", "value", "value"}, |
232 | 236 | },
|
233 |
| - } |
234 |
| - for _, bm := range benchmarks { |
235 |
| - b.Run(bm.name, func(b *testing.B) { |
| 237 | + } { |
| 238 | + b.Run(fmt.Sprintf("labels=%v", len(bm.makeLabelPairValues)), func(b *testing.B) { |
236 | 239 | for i := 0; i < b.N; i++ {
|
237 |
| - benchFunc(bm.desc, bm.variableLabelValues) |
| 240 | + MakeLabelPairs(bm.desc, bm.makeLabelPairValues) |
| 241 | + } |
| 242 | + }) |
| 243 | + } |
| 244 | +} |
| 245 | + |
| 246 | +func BenchmarkConstMetricFlow(b *testing.B) { |
| 247 | + for _, bm := range []struct { |
| 248 | + descFunc func() *Desc |
| 249 | + labelValues []string |
| 250 | + }{ |
| 251 | + { |
| 252 | + descFunc: new1LabelDescFunc, |
| 253 | + labelValues: []string{"value"}, |
| 254 | + }, |
| 255 | + { |
| 256 | + descFunc: new3LabelsDescFunc, |
| 257 | + labelValues: []string{"value", "value", "value"}, |
| 258 | + }, |
| 259 | + { |
| 260 | + descFunc: new10LabelsDescFunc, |
| 261 | + labelValues: []string{"value", "value", "value", "value", "value", "value", "value", "value", "value", "value"}, |
| 262 | + }, |
| 263 | + } { |
| 264 | + b.Run(fmt.Sprintf("labels=%v", len(bm.labelValues)), func(b *testing.B) { |
| 265 | + for _, metricsToCreate := range []int{1, 2, 3, 5} { |
| 266 | + b.Run(fmt.Sprintf("metrics=%v", metricsToCreate), func(b *testing.B) { |
| 267 | + for i := 0; i < b.N; i++ { |
| 268 | + desc := bm.descFunc() |
| 269 | + for j := 0; j < metricsToCreate; j++ { |
| 270 | + _, err := NewConstMetric(desc, GaugeValue, 1.0, bm.labelValues...) |
| 271 | + if err != nil { |
| 272 | + b.Fatal(err) |
| 273 | + } |
| 274 | + } |
| 275 | + } |
| 276 | + }) |
238 | 277 | }
|
239 | 278 | })
|
240 | 279 | }
|
|
0 commit comments