| 1 |
// Copyright 2014 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package prometheus |
| 15 |
|
| 16 |
import ( |
| 17 |
"sync" |
| 18 |
"testing" |
| 19 |
) |
| 20 |
|
| 21 |
func BenchmarkCounter(b *testing.B) { |
| 22 |
type fns []func(*CounterVec) Counter |
| 23 |
|
| 24 |
twoConstraint := func(_ string) string { |
| 25 |
return "two" |
| 26 |
} |
| 27 |
|
| 28 |
deLV := func(m *CounterVec) Counter { |
| 29 |
return m.WithLabelValues("eins", "zwei", "drei") |
| 30 |
} |
| 31 |
frLV := func(m *CounterVec) Counter { |
| 32 |
return m.WithLabelValues("une", "deux", "trois") |
| 33 |
} |
| 34 |
nlLV := func(m *CounterVec) Counter { |
| 35 |
return m.WithLabelValues("een", "twee", "drie") |
| 36 |
} |
| 37 |
|
| 38 |
deML := func(m *CounterVec) Counter { |
| 39 |
return m.With(Labels{"two": "zwei", "one": "eins", "three": "drei"}) |
| 40 |
} |
| 41 |
frML := func(m *CounterVec) Counter { |
| 42 |
return m.With(Labels{"two": "deux", "one": "une", "three": "trois"}) |
| 43 |
} |
| 44 |
nlML := func(m *CounterVec) Counter { |
| 45 |
return m.With(Labels{"two": "twee", "one": "een", "three": "drie"}) |
| 46 |
} |
| 47 |
|
| 48 |
deLabels := Labels{"two": "zwei", "one": "eins", "three": "drei"} |
| 49 |
dePML := func(m *CounterVec) Counter { |
| 50 |
return m.With(deLabels) |
| 51 |
} |
| 52 |
frLabels := Labels{"two": "deux", "one": "une", "three": "trois"} |
| 53 |
frPML := func(m *CounterVec) Counter { |
| 54 |
return m.With(frLabels) |
| 55 |
} |
| 56 |
nlLabels := Labels{"two": "twee", "one": "een", "three": "drie"} |
| 57 |
nlPML := func(m *CounterVec) Counter { |
| 58 |
return m.With(nlLabels) |
| 59 |
} |
| 60 |
|
| 61 |
table := []struct { |
| 62 |
name string |
| 63 |
constraint LabelConstraint |
| 64 |
counters fns |
| 65 |
}{ |
| 66 |
{"With Label Values", nil, fns{deLV}}, |
| 67 |
{"With Label Values and Constraint", twoConstraint, fns{deLV}}, |
| 68 |
{"With triple Label Values", nil, fns{deLV, frLV, nlLV}}, |
| 69 |
{"With triple Label Values and Constraint", twoConstraint, fns{deLV, frLV, nlLV}}, |
| 70 |
{"With repeated Label Values", nil, fns{deLV, deLV}}, |
| 71 |
{"With repeated Label Values and Constraint", twoConstraint, fns{deLV, deLV}}, |
| 72 |
{"With Mapped Labels", nil, fns{deML}}, |
| 73 |
{"With Mapped Labels and Constraint", twoConstraint, fns{deML}}, |
| 74 |
{"With triple Mapped Labels", nil, fns{deML, frML, nlML}}, |
| 75 |
{"With triple Mapped Labels and Constraint", twoConstraint, fns{deML, frML, nlML}}, |
| 76 |
{"With repeated Mapped Labels", nil, fns{deML, deML}}, |
| 77 |
{"With repeated Mapped Labels and Constraint", twoConstraint, fns{deML, deML}}, |
| 78 |
{"With Prepared Mapped Labels", nil, fns{dePML}}, |
| 79 |
{"With Prepared Mapped Labels and Constraint", twoConstraint, fns{dePML}}, |
| 80 |
{"With triple Prepared Mapped Labels", nil, fns{dePML, frPML, nlPML}}, |
| 81 |
{"With triple Prepared Mapped Labels and Constraint", twoConstraint, fns{dePML, frPML, nlPML}}, |
| 82 |
{"With repeated Prepared Mapped Labels", nil, fns{dePML, dePML}}, |
| 83 |
{"With repeated Prepared Mapped Labels and Constraint", twoConstraint, fns{dePML, dePML}}, |
| 84 |
} |
| 85 |
|
| 86 |
for _, t := range table { |
| 87 |
b.Run(t.name, func(b *testing.B) { |
| 88 |
m := V2.NewCounterVec( |
| 89 |
CounterVecOpts{ |
| 90 |
CounterOpts: CounterOpts{ |
| 91 |
Name: "benchmark_counter", |
| 92 |
Help: "A counter to benchmark it.", |
| 93 |
}, |
| 94 |
VariableLabels: ConstrainedLabels{ |
| 95 |
ConstrainedLabel{Name: "one"}, |
| 96 |
ConstrainedLabel{Name: "two", Constraint: t.constraint}, |
| 97 |
ConstrainedLabel{Name: "three"}, |
| 98 |
}, |
| 99 |
}, |
| 100 |
) |
| 101 |
b.ReportAllocs() |
| 102 |
b.ResetTimer() |
| 103 |
for i := 0; i < b.N; i++ { |
| 104 |
for _, fn := range t.counters { |
| 105 |
fn(m).Inc() |
| 106 |
} |
| 107 |
} |
| 108 |
}) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
func BenchmarkCounterWithLabelValuesConcurrent(b *testing.B) { |
| 113 |
m := NewCounterVec( |
| 114 |
CounterOpts{ |
| 115 |
Name: "benchmark_counter", |
| 116 |
Help: "A counter to benchmark it.", |
| 117 |
}, |
| 118 |
[]string{"one", "two", "three"}, |
| 119 |
) |
| 120 |
b.ReportAllocs() |
| 121 |
b.ResetTimer() |
| 122 |
wg := sync.WaitGroup{} |
| 123 |
for i := 0; i < 10; i++ { |
| 124 |
wg.Add(1) |
| 125 |
go func() { |
| 126 |
for j := 0; j < b.N/10; j++ { |
| 127 |
m.WithLabelValues("eins", "zwei", "drei").Inc() |
| 128 |
} |
| 129 |
wg.Done() |
| 130 |
}() |
| 131 |
} |
| 132 |
wg.Wait() |
| 133 |
} |
| 134 |
|
| 135 |
func BenchmarkCounterNoLabels(b *testing.B) { |
| 136 |
m := NewCounter(CounterOpts{ |
| 137 |
Name: "benchmark_counter", |
| 138 |
Help: "A counter to benchmark it.", |
| 139 |
}) |
| 140 |
b.ReportAllocs() |
| 141 |
b.ResetTimer() |
| 142 |
for i := 0; i < b.N; i++ { |
| 143 |
m.Inc() |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func BenchmarkGaugeWithLabelValues(b *testing.B) { |
| 148 |
m := NewGaugeVec( |
| 149 |
GaugeOpts{ |
| 150 |
Name: "benchmark_gauge", |
| 151 |
Help: "A gauge to benchmark it.", |
| 152 |
}, |
| 153 |
[]string{"one", "two", "three"}, |
| 154 |
) |
| 155 |
b.ReportAllocs() |
| 156 |
b.ResetTimer() |
| 157 |
for i := 0; i < b.N; i++ { |
| 158 |
m.WithLabelValues("eins", "zwei", "drei").Set(3.1415) |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
func BenchmarkGaugeNoLabels(b *testing.B) { |
| 163 |
m := NewGauge(GaugeOpts{ |
| 164 |
Name: "benchmark_gauge", |
| 165 |
Help: "A gauge to benchmark it.", |
| 166 |
}) |
| 167 |
b.ReportAllocs() |
| 168 |
b.ResetTimer() |
| 169 |
for i := 0; i < b.N; i++ { |
| 170 |
m.Set(3.1415) |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
func BenchmarkSummaryWithLabelValues(b *testing.B) { |
| 175 |
m := NewSummaryVec( |
| 176 |
SummaryOpts{ |
| 177 |
Name: "benchmark_summary", |
| 178 |
Help: "A summary to benchmark it.", |
| 179 |
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, |
| 180 |
}, |
| 181 |
[]string{"one", "two", "three"}, |
| 182 |
) |
| 183 |
b.ReportAllocs() |
| 184 |
b.ResetTimer() |
| 185 |
for i := 0; i < b.N; i++ { |
| 186 |
m.WithLabelValues("eins", "zwei", "drei").Observe(3.1415) |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
func BenchmarkSummaryNoLabels(b *testing.B) { |
| 191 |
m := NewSummary(SummaryOpts{ |
| 192 |
Name: "benchmark_summary", |
| 193 |
Help: "A summary to benchmark it.", |
| 194 |
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, |
| 195 |
}, |
| 196 |
) |
| 197 |
b.ReportAllocs() |
| 198 |
b.ResetTimer() |
| 199 |
for i := 0; i < b.N; i++ { |
| 200 |
m.Observe(3.1415) |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
func BenchmarkHistogramWithLabelValues(b *testing.B) { |
| 205 |
m := NewHistogramVec( |
| 206 |
HistogramOpts{ |
| 207 |
Name: "benchmark_histogram", |
| 208 |
Help: "A histogram to benchmark it.", |
| 209 |
}, |
| 210 |
[]string{"one", "two", "three"}, |
| 211 |
) |
| 212 |
b.ReportAllocs() |
| 213 |
b.ResetTimer() |
| 214 |
for i := 0; i < b.N; i++ { |
| 215 |
m.WithLabelValues("eins", "zwei", "drei").Observe(3.1415) |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
func BenchmarkHistogramNoLabels(b *testing.B) { |
| 220 |
m := NewHistogram(HistogramOpts{ |
| 221 |
Name: "benchmark_histogram", |
| 222 |
Help: "A histogram to benchmark it.", |
| 223 |
}, |
| 224 |
) |
| 225 |
b.ReportAllocs() |
| 226 |
b.ResetTimer() |
| 227 |
for i := 0; i < b.N; i++ { |
| 228 |
m.Observe(3.1415) |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
func BenchmarkParallelCounter(b *testing.B) { |
| 233 |
c := NewCounter(CounterOpts{ |
| 234 |
Name: "benchmark_counter", |
| 235 |
Help: "A Counter to benchmark it.", |
| 236 |
}) |
| 237 |
b.ReportAllocs() |
| 238 |
b.ResetTimer() |
| 239 |
b.RunParallel(func(pb *testing.PB) { |
| 240 |
for pb.Next() { |
| 241 |
c.Inc() |
| 242 |
} |
| 243 |
}) |
| 244 |
} |