Debian Codegraph @

timer.go

raw symbols SCIP
1 package metrics
2
3 import (
4 "sync"
5 "time"
6 )
7
8 // Timers capture the duration and rate of events.
9 type Timer interface {
10 Count() int64
11 Max() int64
12 Mean() float64
13 Min() int64
14 Percentile(float64) float64
15 Percentiles([]float64) []float64
16 Rate1() float64
17 Rate5() float64
18 Rate15() float64
19 RateMean() float64
20 Snapshot() Timer
21 StdDev() float64
22 Sum() int64
23 Time(func())
24 Update(time.Duration)
25 UpdateSince(time.Time)
26 Variance() float64
27 }
28
29 // GetOrRegisterTimer returns an existing Timer or constructs and registers a
30 // new StandardTimer.
31 func GetOrRegisterTimer(name string, r Registry) Timer {
32 if nil == r {
33 r = DefaultRegistry
34 }
35 return r.GetOrRegister(name, NewTimer).(Timer)
36 }
37
38 // NewCustomTimer constructs a new StandardTimer from a Histogram and a Meter.
39 func NewCustomTimer(h Histogram, m Meter) Timer {
40 if UseNilMetrics {
41 return NilTimer{}
42 }
43 return &StandardTimer{
44 histogram: h,
45 meter: m,
46 }
47 }
48
49 // NewRegisteredTimer constructs and registers a new StandardTimer.
50 func NewRegisteredTimer(name string, r Registry) Timer {
51 c := NewTimer()
52 if nil == r {
53 r = DefaultRegistry
54 }
55 r.Register(name, c)
56 return c
57 }
58
59 // NewTimer constructs a new StandardTimer using an exponentially-decaying
60 // sample with the same reservoir size and alpha as UNIX load averages.
61 func NewTimer() Timer {
62 if UseNilMetrics {
63 return NilTimer{}
64 }
65 return &StandardTimer{
66 histogram: NewHistogram(NewExpDecaySample(1028, 0.015)),
67 meter: NewMeter(),
68 }
69 }
70
71 // NilTimer is a no-op Timer.
72 type NilTimer struct {
73 h Histogram
74 m Meter
75 }
76
77 // Count is a no-op.
78 func (NilTimer) Count() int64 { return 0 }
79
80 // Max is a no-op.
81 func (NilTimer) Max() int64 { return 0 }
82
83 // Mean is a no-op.
84 func (NilTimer) Mean() float64 { return 0.0 }
85
86 // Min is a no-op.
87 func (NilTimer) Min() int64 { return 0 }
88
89 // Percentile is a no-op.
90 func (NilTimer) Percentile(p float64) float64 { return 0.0 }
91
92 // Percentiles is a no-op.
93 func (NilTimer) Percentiles(ps []float64) []float64 {
94 return make([]float64, len(ps))
95 }
96
97 // Rate1 is a no-op.
98 func (NilTimer) Rate1() float64 { return 0.0 }
99
100 // Rate5 is a no-op.
101 func (NilTimer) Rate5() float64 { return 0.0 }
102
103 // Rate15 is a no-op.
104 func (NilTimer) Rate15() float64 { return 0.0 }
105
106 // RateMean is a no-op.
107 func (NilTimer) RateMean() float64 { return 0.0 }
108
109 // Snapshot is a no-op.
110 func (NilTimer) Snapshot() Timer { return NilTimer{} }
111
112 // StdDev is a no-op.
113 func (NilTimer) StdDev() float64 { return 0.0 }
114
115 // Sum is a no-op.
116 func (NilTimer) Sum() int64 { return 0 }
117
118 // Time is a no-op.
119 func (NilTimer) Time(func()) {}
120
121 // Update is a no-op.
122 func (NilTimer) Update(time.Duration) {}
123
124 // UpdateSince is a no-op.
125 func (NilTimer) UpdateSince(time.Time) {}
126
127 // Variance is a no-op.
128 func (NilTimer) Variance() float64 { return 0.0 }
129
130 // StandardTimer is the standard implementation of a Timer and uses a Histogram
131 // and Meter.
132 type StandardTimer struct {
133 histogram Histogram
134 meter Meter
135 mutex sync.Mutex
136 }
137
138 // Count returns the number of events recorded.
139 func (t *StandardTimer) Count() int64 {
140 return t.histogram.Count()
141 }
142
143 // Max returns the maximum value in the sample.
144 func (t *StandardTimer) Max() int64 {
145 return t.histogram.Max()
146 }
147
148 // Mean returns the mean of the values in the sample.
149 func (t *StandardTimer) Mean() float64 {
150 return t.histogram.Mean()
151 }
152
153 // Min returns the minimum value in the sample.
154 func (t *StandardTimer) Min() int64 {
155 return t.histogram.Min()
156 }
157
158 // Percentile returns an arbitrary percentile of the values in the sample.
159 func (t *StandardTimer) Percentile(p float64) float64 {
160 return t.histogram.Percentile(p)
161 }
162
163 // Percentiles returns a slice of arbitrary percentiles of the values in the
164 // sample.
165 func (t *StandardTimer) Percentiles(ps []float64) []float64 {
166 return t.histogram.Percentiles(ps)
167 }
168
169 // Rate1 returns the one-minute moving average rate of events per second.
170 func (t *StandardTimer) Rate1() float64 {
171 return t.meter.Rate1()
172 }
173
174 // Rate5 returns the five-minute moving average rate of events per second.
175 func (t *StandardTimer) Rate5() float64 {
176 return t.meter.Rate5()
177 }
178
179 // Rate15 returns the fifteen-minute moving average rate of events per second.
180 func (t *StandardTimer) Rate15() float64 {
181 return t.meter.Rate15()
182 }
183
184 // RateMean returns the meter's mean rate of events per second.
185 func (t *StandardTimer) RateMean() float64 {
186 return t.meter.RateMean()
187 }
188
189 // Snapshot returns a read-only copy of the timer.
190 func (t *StandardTimer) Snapshot() Timer {
191 t.mutex.Lock()
192 defer t.mutex.Unlock()
193 return &TimerSnapshot{
194 histogram: t.histogram.Snapshot().(*HistogramSnapshot),
195 meter: t.meter.Snapshot().(*MeterSnapshot),
196 }
197 }
198
199 // StdDev returns the standard deviation of the values in the sample.
200 func (t *StandardTimer) StdDev() float64 {
201 return t.histogram.StdDev()
202 }
203
204 // Sum returns the sum in the sample.
205 func (t *StandardTimer) Sum() int64 {
206 return t.histogram.Sum()
207 }
208
209 // Record the duration of the execution of the given function.
210 func (t *StandardTimer) Time(f func()) {
211 ts := time.Now()
212 f()
213 t.Update(time.Since(ts))
214 }
215
216 // Record the duration of an event.
217 func (t *StandardTimer) Update(d time.Duration) {
218 t.mutex.Lock()
219 defer t.mutex.Unlock()
220 t.histogram.Update(int64(d))
221 t.meter.Mark(1)
222 }
223
224 // Record the duration of an event that started at a time and ends now.
225 func (t *StandardTimer) UpdateSince(ts time.Time) {
226 t.mutex.Lock()
227 defer t.mutex.Unlock()
228 t.histogram.Update(int64(time.Since(ts)))
229 t.meter.Mark(1)
230 }
231
232 // Variance returns the variance of the values in the sample.
233 func (t *StandardTimer) Variance() float64 {
234 return t.histogram.Variance()
235 }
236
237 // TimerSnapshot is a read-only copy of another Timer.
238 type TimerSnapshot struct {
239 histogram *HistogramSnapshot
240 meter *MeterSnapshot
241 }
242
243 // Count returns the number of events recorded at the time the snapshot was
244 // taken.
245 func (t *TimerSnapshot) Count() int64 { return t.histogram.Count() }
246
247 // Max returns the maximum value at the time the snapshot was taken.
248 func (t *TimerSnapshot) Max() int64 { return t.histogram.Max() }
249
250 // Mean returns the mean value at the time the snapshot was taken.
251 func (t *TimerSnapshot) Mean() float64 { return t.histogram.Mean() }
252
253 // Min returns the minimum value at the time the snapshot was taken.
254 func (t *TimerSnapshot) Min() int64 { return t.histogram.Min() }
255
256 // Percentile returns an arbitrary percentile of sampled values at the time the
257 // snapshot was taken.
258 func (t *TimerSnapshot) Percentile(p float64) float64 {
259 return t.histogram.Percentile(p)
260 }
261
262 // Percentiles returns a slice of arbitrary percentiles of sampled values at
263 // the time the snapshot was taken.
264 func (t *TimerSnapshot) Percentiles(ps []float64) []float64 {
265 return t.histogram.Percentiles(ps)
266 }
267
268 // Rate1 returns the one-minute moving average rate of events per second at the
269 // time the snapshot was taken.
270 func (t *TimerSnapshot) Rate1() float64 { return t.meter.Rate1() }
271
272 // Rate5 returns the five-minute moving average rate of events per second at
273 // the time the snapshot was taken.
274 func (t *TimerSnapshot) Rate5() float64 { return t.meter.Rate5() }
275
276 // Rate15 returns the fifteen-minute moving average rate of events per second
277 // at the time the snapshot was taken.
278 func (t *TimerSnapshot) Rate15() float64 { return t.meter.Rate15() }
279
280 // RateMean returns the meter's mean rate of events per second at the time the
281 // snapshot was taken.
282 func (t *TimerSnapshot) RateMean() float64 { return t.meter.RateMean() }
283
284 // Snapshot returns the snapshot.
285 func (t *TimerSnapshot) Snapshot() Timer { return t }
286
287 // StdDev returns the standard deviation of the values at the time the snapshot
288 // was taken.
289 func (t *TimerSnapshot) StdDev() float64 { return t.histogram.StdDev() }
290
291 // Sum returns the sum at the time the snapshot was taken.
292 func (t *TimerSnapshot) Sum() int64 { return t.histogram.Sum() }
293
294 // Time panics.
295 func (*TimerSnapshot) Time(func()) {
296 panic("Time called on a TimerSnapshot")
297 }
298
299 // Update panics.
300 func (*TimerSnapshot) Update(time.Duration) {
301 panic("Update called on a TimerSnapshot")
302 }
303
304 // UpdateSince panics.
305 func (*TimerSnapshot) UpdateSince(time.Time) {
306 panic("UpdateSince called on a TimerSnapshot")
307 }
308
309 // Variance returns the variance of the values at the time the snapshot was
310 // taken.
311 func (t *TimerSnapshot) Variance() float64 { return t.histogram.Variance() }