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 14package prometheus 15 16import ( 17 "sync" 18 "testing" 19) 20 21func BenchmarkCounterWithLabelValues(b *testing.B) { 22 m := NewCounterVec( 23 CounterOpts{ 24 Name: "benchmark_counter", 25 Help: "A counter to benchmark it.", 26 }, 27 []string{"one", "two", "three"}, 28 ) 29 b.ReportAllocs() 30 b.ResetTimer() 31 for i := 0; i < b.N; i++ { 32 m.WithLabelValues("eins", "zwei", "drei").Inc() 33 } 34} 35 36func BenchmarkCounterWithLabelValuesConcurrent(b *testing.B) { 37 m := NewCounterVec( 38 CounterOpts{ 39 Name: "benchmark_counter", 40 Help: "A counter to benchmark it.", 41 }, 42 []string{"one", "two", "three"}, 43 ) 44 b.ReportAllocs() 45 b.ResetTimer() 46 wg := sync.WaitGroup{} 47 for i := 0; i < 10; i++ { 48 wg.Add(1) 49 go func() { 50 for j := 0; j < b.N/10; j++ { 51 m.WithLabelValues("eins", "zwei", "drei").Inc() 52 } 53 wg.Done() 54 }() 55 } 56 wg.Wait() 57} 58 59func BenchmarkCounterWithMappedLabels(b *testing.B) { 60 m := NewCounterVec( 61 CounterOpts{ 62 Name: "benchmark_counter", 63 Help: "A counter to benchmark it.", 64 }, 65 []string{"one", "two", "three"}, 66 ) 67 b.ReportAllocs() 68 b.ResetTimer() 69 for i := 0; i < b.N; i++ { 70 m.With(Labels{"two": "zwei", "one": "eins", "three": "drei"}).Inc() 71 } 72} 73 74func BenchmarkCounterWithPreparedMappedLabels(b *testing.B) { 75 m := NewCounterVec( 76 CounterOpts{ 77 Name: "benchmark_counter", 78 Help: "A counter to benchmark it.", 79 }, 80 []string{"one", "two", "three"}, 81 ) 82 b.ReportAllocs() 83 b.ResetTimer() 84 labels := Labels{"two": "zwei", "one": "eins", "three": "drei"} 85 for i := 0; i < b.N; i++ { 86 m.With(labels).Inc() 87 } 88} 89 90func BenchmarkCounterNoLabels(b *testing.B) { 91 m := NewCounter(CounterOpts{ 92 Name: "benchmark_counter", 93 Help: "A counter to benchmark it.", 94 }) 95 b.ReportAllocs() 96 b.ResetTimer() 97 for i := 0; i < b.N; i++ { 98 m.Inc() 99 } 100} 101 102func BenchmarkGaugeWithLabelValues(b *testing.B) { 103 m := NewGaugeVec( 104 GaugeOpts{ 105 Name: "benchmark_gauge", 106 Help: "A gauge to benchmark it.", 107 }, 108 []string{"one", "two", "three"}, 109 ) 110 b.ReportAllocs() 111 b.ResetTimer() 112 for i := 0; i < b.N; i++ { 113 m.WithLabelValues("eins", "zwei", "drei").Set(3.1415) 114 } 115} 116 117func BenchmarkGaugeNoLabels(b *testing.B) { 118 m := NewGauge(GaugeOpts{ 119 Name: "benchmark_gauge", 120 Help: "A gauge to benchmark it.", 121 }) 122 b.ReportAllocs() 123 b.ResetTimer() 124 for i := 0; i < b.N; i++ { 125 m.Set(3.1415) 126 } 127} 128 129func BenchmarkSummaryWithLabelValues(b *testing.B) { 130 m := NewSummaryVec( 131 SummaryOpts{ 132 Name: "benchmark_summary", 133 Help: "A summary to benchmark it.", 134 Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, 135 }, 136 []string{"one", "two", "three"}, 137 ) 138 b.ReportAllocs() 139 b.ResetTimer() 140 for i := 0; i < b.N; i++ { 141 m.WithLabelValues("eins", "zwei", "drei").Observe(3.1415) 142 } 143} 144 145func BenchmarkSummaryNoLabels(b *testing.B) { 146 m := NewSummary(SummaryOpts{ 147 Name: "benchmark_summary", 148 Help: "A summary to benchmark it.", 149 Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, 150 }, 151 ) 152 b.ReportAllocs() 153 b.ResetTimer() 154 for i := 0; i < b.N; i++ { 155 m.Observe(3.1415) 156 } 157} 158 159func BenchmarkHistogramWithLabelValues(b *testing.B) { 160 m := NewHistogramVec( 161 HistogramOpts{ 162 Name: "benchmark_histogram", 163 Help: "A histogram to benchmark it.", 164 }, 165 []string{"one", "two", "three"}, 166 ) 167 b.ReportAllocs() 168 b.ResetTimer() 169 for i := 0; i < b.N; i++ { 170 m.WithLabelValues("eins", "zwei", "drei").Observe(3.1415) 171 } 172} 173 174func BenchmarkHistogramNoLabels(b *testing.B) { 175 m := NewHistogram(HistogramOpts{ 176 Name: "benchmark_histogram", 177 Help: "A histogram to benchmark it.", 178 }, 179 ) 180 b.ReportAllocs() 181 b.ResetTimer() 182 for i := 0; i < b.N; i++ { 183 m.Observe(3.1415) 184 } 185} 186 187func BenchmarkParallelCounter(b *testing.B) { 188 c := NewCounter(CounterOpts{ 189 Name: "benchmark_counter", 190 Help: "A Counter to benchmark it.", 191 }) 192 b.ReportAllocs() 193 b.ResetTimer() 194 b.RunParallel(func(pb *testing.PB) { 195 for pb.Next() { 196 c.Inc() 197 } 198 }) 199} 200