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		},
135		[]string{"one", "two", "three"},
136	)
137	b.ReportAllocs()
138	b.ResetTimer()
139	for i := 0; i < b.N; i++ {
140		m.WithLabelValues("eins", "zwei", "drei").Observe(3.1415)
141	}
142}
143
144func BenchmarkSummaryNoLabels(b *testing.B) {
145	m := NewSummary(SummaryOpts{
146		Name: "benchmark_summary",
147		Help: "A summary to benchmark it.",
148	},
149	)
150	b.ReportAllocs()
151	b.ResetTimer()
152	for i := 0; i < b.N; i++ {
153		m.Observe(3.1415)
154	}
155}
156
157func BenchmarkHistogramWithLabelValues(b *testing.B) {
158	m := NewHistogramVec(
159		HistogramOpts{
160			Name: "benchmark_histogram",
161			Help: "A histogram to benchmark it.",
162		},
163		[]string{"one", "two", "three"},
164	)
165	b.ReportAllocs()
166	b.ResetTimer()
167	for i := 0; i < b.N; i++ {
168		m.WithLabelValues("eins", "zwei", "drei").Observe(3.1415)
169	}
170}
171
172func BenchmarkHistogramNoLabels(b *testing.B) {
173	m := NewHistogram(HistogramOpts{
174		Name: "benchmark_histogram",
175		Help: "A histogram to benchmark it.",
176	},
177	)
178	b.ReportAllocs()
179	b.ResetTimer()
180	for i := 0; i < b.N; i++ {
181		m.Observe(3.1415)
182	}
183}
184