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 "testing" 18) 19 20func BenchmarkCounterWithLabelValues(b *testing.B) { 21 m := NewCounterVec( 22 CounterOpts{ 23 Name: "benchmark_counter", 24 Help: "A counter to benchmark it.", 25 }, 26 []string{"one", "two", "three"}, 27 ) 28 b.ReportAllocs() 29 b.ResetTimer() 30 for i := 0; i < b.N; i++ { 31 m.WithLabelValues("eins", "zwei", "drei").Inc() 32 } 33} 34 35func BenchmarkCounterWithMappedLabels(b *testing.B) { 36 m := NewCounterVec( 37 CounterOpts{ 38 Name: "benchmark_counter", 39 Help: "A counter to benchmark it.", 40 }, 41 []string{"one", "two", "three"}, 42 ) 43 b.ReportAllocs() 44 b.ResetTimer() 45 for i := 0; i < b.N; i++ { 46 m.With(Labels{"two": "zwei", "one": "eins", "three": "drei"}).Inc() 47 } 48} 49 50func BenchmarkCounterWithPreparedMappedLabels(b *testing.B) { 51 m := NewCounterVec( 52 CounterOpts{ 53 Name: "benchmark_counter", 54 Help: "A counter to benchmark it.", 55 }, 56 []string{"one", "two", "three"}, 57 ) 58 b.ReportAllocs() 59 b.ResetTimer() 60 labels := Labels{"two": "zwei", "one": "eins", "three": "drei"} 61 for i := 0; i < b.N; i++ { 62 m.With(labels).Inc() 63 } 64} 65 66func BenchmarkCounterNoLabels(b *testing.B) { 67 m := NewCounter(CounterOpts{ 68 Name: "benchmark_counter", 69 Help: "A counter to benchmark it.", 70 }) 71 b.ReportAllocs() 72 b.ResetTimer() 73 for i := 0; i < b.N; i++ { 74 m.Inc() 75 } 76} 77 78func BenchmarkGaugeWithLabelValues(b *testing.B) { 79 m := NewGaugeVec( 80 GaugeOpts{ 81 Name: "benchmark_gauge", 82 Help: "A gauge to benchmark it.", 83 }, 84 []string{"one", "two", "three"}, 85 ) 86 b.ReportAllocs() 87 b.ResetTimer() 88 for i := 0; i < b.N; i++ { 89 m.WithLabelValues("eins", "zwei", "drei").Set(3.1415) 90 } 91} 92 93func BenchmarkGaugeNoLabels(b *testing.B) { 94 m := NewGauge(GaugeOpts{ 95 Name: "benchmark_gauge", 96 Help: "A gauge to benchmark it.", 97 }) 98 b.ReportAllocs() 99 b.ResetTimer() 100 for i := 0; i < b.N; i++ { 101 m.Set(3.1415) 102 } 103} 104 105func BenchmarkSummaryWithLabelValues(b *testing.B) { 106 m := NewSummaryVec( 107 SummaryOpts{ 108 Name: "benchmark_summary", 109 Help: "A summary to benchmark it.", 110 }, 111 []string{"one", "two", "three"}, 112 ) 113 b.ReportAllocs() 114 b.ResetTimer() 115 for i := 0; i < b.N; i++ { 116 m.WithLabelValues("eins", "zwei", "drei").Observe(3.1415) 117 } 118} 119 120func BenchmarkSummaryNoLabels(b *testing.B) { 121 m := NewSummary(SummaryOpts{ 122 Name: "benchmark_summary", 123 Help: "A summary to benchmark it.", 124 }, 125 ) 126 b.ReportAllocs() 127 b.ResetTimer() 128 for i := 0; i < b.N; i++ { 129 m.Observe(3.1415) 130 } 131} 132 133func BenchmarkHistogramWithLabelValues(b *testing.B) { 134 m := NewHistogramVec( 135 HistogramOpts{ 136 Name: "benchmark_histogram", 137 Help: "A histogram to benchmark it.", 138 }, 139 []string{"one", "two", "three"}, 140 ) 141 b.ReportAllocs() 142 b.ResetTimer() 143 for i := 0; i < b.N; i++ { 144 m.WithLabelValues("eins", "zwei", "drei").Observe(3.1415) 145 } 146} 147 148func BenchmarkHistogramNoLabels(b *testing.B) { 149 m := NewHistogram(HistogramOpts{ 150 Name: "benchmark_histogram", 151 Help: "A histogram to benchmark it.", 152 }, 153 ) 154 b.ReportAllocs() 155 b.ResetTimer() 156 for i := 0; i < b.N; i++ { 157 m.Observe(3.1415) 158 } 159} 160