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