Debian Codegraph @

prometheus/timer_test.go

raw ◀ ▶
1 Debian patch touch this file
1 // Copyright 2016 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 "reflect"
18 "testing"
•19 "time"
20
21 "google.golang.org/protobuf/proto"
22
23 dto "github.com/prometheus/client_model/go"
24 )
25
26 func TestTimerObserve(t *testing.T) {
27 var (
28 his = NewHistogram(HistogramOpts{Name: "test_histogram"})
29 sum = NewSummary(SummaryOpts{Name: "test_summary"})
30 gauge = NewGauge(GaugeOpts{Name: "test_gauge"})
31 )
32
33 func() {
34 hisTimer := NewTimer(his)
35 sumTimer := NewTimer(sum)
36 gaugeTimer := NewTimer(ObserverFunc(gauge.Set))
37 defer hisTimer.ObserveDuration()
38 defer sumTimer.ObserveDuration()
39 defer gaugeTimer.ObserveDuration()
•40 // Ensure measurable time passes to avoid flaky test on fast systems.
41 time.Sleep(time.Millisecond)
42 }()
43
44 m := &dto.Metric{}
45 his.Write(m)
46 if want, got := uint64(1), m.GetHistogram().GetSampleCount(); want != got {
47 t.Errorf("want %d observations for histogram, got %d", want, got)
48 }
49 m.Reset()
50 sum.Write(m)
51 if want, got := uint64(1), m.GetSummary().GetSampleCount(); want != got {
52 t.Errorf("want %d observations for summary, got %d", want, got)
53 }
54 m.Reset()
55 gauge.Write(m)
56 if got := m.GetGauge().GetValue(); got <= 0 {
57 t.Errorf("want value > 0 for gauge, got %f", got)
58 }
59 }
60
61 func TestTimerObserveWithExemplar(t *testing.T) {
62 var (
63 exemplar = Labels{"foo": "bar"}
64 his = NewHistogram(HistogramOpts{Name: "test_histogram"})
65 sum = NewSummary(SummaryOpts{Name: "test_summary"})
66 gauge = NewGauge(GaugeOpts{Name: "test_gauge"})
67 )
68
69 func() {
70 hisTimer := NewTimer(his)
71 sumTimer := NewTimer(sum)
72 gaugeTimer := NewTimer(ObserverFunc(gauge.Set))
73 defer hisTimer.ObserveDurationWithExemplar(exemplar)
74 // Gauges and summaries does not implement ExemplarObserver, so we expect them to ignore exemplar.
75 defer sumTimer.ObserveDurationWithExemplar(exemplar)
76 defer gaugeTimer.ObserveDurationWithExemplar(exemplar)
•77 // Ensure measurable time passes to avoid flaky test on fast systems.
78 time.Sleep(time.Millisecond)
79 }()
80
81 m := &dto.Metric{}
82 his.Write(m)
83 if want, got := uint64(1), m.GetHistogram().GetSampleCount(); want != got {
84 t.Errorf("want %d observations for histogram, got %d", want, got)
85 }
86 var got []*dto.LabelPair
87 for _, b := range m.GetHistogram().GetBucket() {
88 if b.Exemplar != nil {
89 got = b.Exemplar.GetLabel()
90 break
91 }
92 }
93
94 want := []*dto.LabelPair{{Name: proto.String("foo"), Value: proto.String("bar")}}
95 if !reflect.DeepEqual(got, want) {
96 t.Errorf("expected %v exemplar labels, got %v", want, got)
97 }
98
99 m.Reset()
100 sum.Write(m)
101 if want, got := uint64(1), m.GetSummary().GetSampleCount(); want != got {
102 t.Errorf("want %d observations for summary, got %d", want, got)
103 }
104 m.Reset()
105 gauge.Write(m)
106 if got := m.GetGauge().GetValue(); got <= 0 {
107 t.Errorf("want value > 0 for gauge, got %f", got)
108 }
109 }
110
111 func TestTimerEmpty(t *testing.T) {
112 emptyTimer := NewTimer(nil)
113 emptyTimer.ObserveDuration()
114 // Do nothing, just demonstrate it works without panic.
115 }
116
117 func TestTimerConditionalTiming(t *testing.T) {
118 var (
119 his = NewHistogram(HistogramOpts{
120 Name: "test_histogram",
121 })
122 timeMe = true
123 m = &dto.Metric{}
124 )
125
126 timedFunc := func() {
127 timer := NewTimer(ObserverFunc(func(v float64) {
128 if timeMe {
129 his.Observe(v)
130 }
131 }))
132 defer timer.ObserveDuration()
133 }
134
135 timedFunc() // This will time.
136 his.Write(m)
137 if want, got := uint64(1), m.GetHistogram().GetSampleCount(); want != got {
138 t.Errorf("want %d observations for histogram, got %d", want, got)
139 }
140
141 timeMe = false
142 timedFunc() // This will not time again.
143 m.Reset()
144 his.Write(m)
145 if want, got := uint64(1), m.GetHistogram().GetSampleCount(); want != got {
146 t.Errorf("want %d observations for histogram, got %d", want, got)
147 }
148 }
149
150 func TestTimerByOutcome(t *testing.T) {
151 var (
152 his = NewHistogramVec(
153 HistogramOpts{Name: "test_histogram"},
154 []string{"outcome"},
155 )
156 outcome = "foo"
157 m = &dto.Metric{}
158 )
159
160 timedFunc := func() {
161 timer := NewTimer(ObserverFunc(func(v float64) {
162 his.WithLabelValues(outcome).Observe(v)
163 }))
164 defer timer.ObserveDuration()
165
166 if outcome == "foo" {
167 outcome = "bar"
168 return
169 }
170 outcome = "foo"
171 }
172
173 timedFunc()
174 his.WithLabelValues("foo").(Histogram).Write(m)
175 if want, got := uint64(0), m.GetHistogram().GetSampleCount(); want != got {
176 t.Errorf("want %d observations for 'foo' histogram, got %d", want, got)
177 }
178 m.Reset()
179 his.WithLabelValues("bar").(Histogram).Write(m)
180 if want, got := uint64(1), m.GetHistogram().GetSampleCount(); want != got {
181 t.Errorf("want %d observations for 'bar' histogram, got %d", want, got)
182 }
183
184 timedFunc()
185 m.Reset()
186 his.WithLabelValues("foo").(Histogram).Write(m)
187 if want, got := uint64(1), m.GetHistogram().GetSampleCount(); want != got {
188 t.Errorf("want %d observations for 'foo' histogram, got %d", want, got)
189 }
190 m.Reset()
191 his.WithLabelValues("bar").(Histogram).Write(m)
192 if want, got := uint64(1), m.GetHistogram().GetSampleCount(); want != got {
193 t.Errorf("want %d observations for 'bar' histogram, got %d", want, got)
194 }
195
196 timedFunc()
197 m.Reset()
198 his.WithLabelValues("foo").(Histogram).Write(m)
199 if want, got := uint64(1), m.GetHistogram().GetSampleCount(); want != got {
200 t.Errorf("want %d observations for 'foo' histogram, got %d", want, got)
201 }
202 m.Reset()
203 his.WithLabelValues("bar").(Histogram).Write(m)
204 if want, got := uint64(2), m.GetHistogram().GetSampleCount(); want != got {
205 t.Errorf("want %d observations for 'bar' histogram, got %d", want, got)
206 }
207 }