...
1 package metrics
2
3 import (
4 "sync"
5 "time"
6 )
7
8
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 Stop()
23 Sum() int64
24 Time(func())
25 Update(time.Duration)
26 UpdateSince(time.Time)
27 Variance() float64
28 }
29
30
31
32
33
34 func GetOrRegisterTimer(name string, r Registry) Timer {
35 if nil == r {
36 r = DefaultRegistry
37 }
38 return r.GetOrRegister(name, NewTimer).(Timer)
39 }
40
41
42
43 func NewCustomTimer(h Histogram, m Meter) Timer {
44 if UseNilMetrics {
45 return NilTimer{}
46 }
47 return &StandardTimer{
48 histogram: h,
49 meter: m,
50 }
51 }
52
53
54
55
56 func NewRegisteredTimer(name string, r Registry) Timer {
57 c := NewTimer()
58 if nil == r {
59 r = DefaultRegistry
60 }
61 r.Register(name, c)
62 return c
63 }
64
65
66
67
68 func NewTimer() Timer {
69 if UseNilMetrics {
70 return NilTimer{}
71 }
72 return &StandardTimer{
73 histogram: NewHistogram(NewExpDecaySample(1028, 0.015)),
74 meter: NewMeter(),
75 }
76 }
77
78
79 type NilTimer struct {
80 h Histogram
81 m Meter
82 }
83
84
85 func (NilTimer) Count() int64 { return 0 }
86
87
88 func (NilTimer) Max() int64 { return 0 }
89
90
91 func (NilTimer) Mean() float64 { return 0.0 }
92
93
94 func (NilTimer) Min() int64 { return 0 }
95
96
97 func (NilTimer) Percentile(p float64) float64 { return 0.0 }
98
99
100 func (NilTimer) Percentiles(ps []float64) []float64 {
101 return make([]float64, len(ps))
102 }
103
104
105 func (NilTimer) Rate1() float64 { return 0.0 }
106
107
108 func (NilTimer) Rate5() float64 { return 0.0 }
109
110
111 func (NilTimer) Rate15() float64 { return 0.0 }
112
113
114 func (NilTimer) RateMean() float64 { return 0.0 }
115
116
117 func (NilTimer) Snapshot() Timer { return NilTimer{} }
118
119
120 func (NilTimer) StdDev() float64 { return 0.0 }
121
122
123 func (NilTimer) Stop() {}
124
125
126 func (NilTimer) Sum() int64 { return 0 }
127
128
129 func (NilTimer) Time(func()) {}
130
131
132 func (NilTimer) Update(time.Duration) {}
133
134
135 func (NilTimer) UpdateSince(time.Time) {}
136
137
138 func (NilTimer) Variance() float64 { return 0.0 }
139
140
141
142 type StandardTimer struct {
143 histogram Histogram
144 meter Meter
145 mutex sync.Mutex
146 }
147
148
149 func (t *StandardTimer) Count() int64 {
150 return t.histogram.Count()
151 }
152
153
154 func (t *StandardTimer) Max() int64 {
155 return t.histogram.Max()
156 }
157
158
159 func (t *StandardTimer) Mean() float64 {
160 return t.histogram.Mean()
161 }
162
163
164 func (t *StandardTimer) Min() int64 {
165 return t.histogram.Min()
166 }
167
168
169 func (t *StandardTimer) Percentile(p float64) float64 {
170 return t.histogram.Percentile(p)
171 }
172
173
174
175 func (t *StandardTimer) Percentiles(ps []float64) []float64 {
176 return t.histogram.Percentiles(ps)
177 }
178
179
180 func (t *StandardTimer) Rate1() float64 {
181 return t.meter.Rate1()
182 }
183
184
185 func (t *StandardTimer) Rate5() float64 {
186 return t.meter.Rate5()
187 }
188
189
190 func (t *StandardTimer) Rate15() float64 {
191 return t.meter.Rate15()
192 }
193
194
195 func (t *StandardTimer) RateMean() float64 {
196 return t.meter.RateMean()
197 }
198
199
200 func (t *StandardTimer) Snapshot() Timer {
201 t.mutex.Lock()
202 defer t.mutex.Unlock()
203 return &TimerSnapshot{
204 histogram: t.histogram.Snapshot().(*HistogramSnapshot),
205 meter: t.meter.Snapshot().(*MeterSnapshot),
206 }
207 }
208
209
210 func (t *StandardTimer) StdDev() float64 {
211 return t.histogram.StdDev()
212 }
213
214
215 func (t *StandardTimer) Stop() {
216 t.meter.Stop()
217 }
218
219
220 func (t *StandardTimer) Sum() int64 {
221 return t.histogram.Sum()
222 }
223
224
225 func (t *StandardTimer) Time(f func()) {
226 ts := time.Now()
227 f()
228 t.Update(time.Since(ts))
229 }
230
231
232 func (t *StandardTimer) Update(d time.Duration) {
233 t.mutex.Lock()
234 defer t.mutex.Unlock()
235 t.histogram.Update(int64(d))
236 t.meter.Mark(1)
237 }
238
239
240 func (t *StandardTimer) UpdateSince(ts time.Time) {
241 t.mutex.Lock()
242 defer t.mutex.Unlock()
243 t.histogram.Update(int64(time.Since(ts)))
244 t.meter.Mark(1)
245 }
246
247
248 func (t *StandardTimer) Variance() float64 {
249 return t.histogram.Variance()
250 }
251
252
253 type TimerSnapshot struct {
254 histogram *HistogramSnapshot
255 meter *MeterSnapshot
256 }
257
258
259
260 func (t *TimerSnapshot) Count() int64 { return t.histogram.Count() }
261
262
263 func (t *TimerSnapshot) Max() int64 { return t.histogram.Max() }
264
265
266 func (t *TimerSnapshot) Mean() float64 { return t.histogram.Mean() }
267
268
269 func (t *TimerSnapshot) Min() int64 { return t.histogram.Min() }
270
271
272
273 func (t *TimerSnapshot) Percentile(p float64) float64 {
274 return t.histogram.Percentile(p)
275 }
276
277
278
279 func (t *TimerSnapshot) Percentiles(ps []float64) []float64 {
280 return t.histogram.Percentiles(ps)
281 }
282
283
284
285 func (t *TimerSnapshot) Rate1() float64 { return t.meter.Rate1() }
286
287
288
289 func (t *TimerSnapshot) Rate5() float64 { return t.meter.Rate5() }
290
291
292
293 func (t *TimerSnapshot) Rate15() float64 { return t.meter.Rate15() }
294
295
296
297 func (t *TimerSnapshot) RateMean() float64 { return t.meter.RateMean() }
298
299
300 func (t *TimerSnapshot) Snapshot() Timer { return t }
301
302
303
304 func (t *TimerSnapshot) StdDev() float64 { return t.histogram.StdDev() }
305
306
307 func (t *TimerSnapshot) Stop() {}
308
309
310 func (t *TimerSnapshot) Sum() int64 { return t.histogram.Sum() }
311
312
313 func (*TimerSnapshot) Time(func()) {
314 panic("Time called on a TimerSnapshot")
315 }
316
317
318 func (*TimerSnapshot) Update(time.Duration) {
319 panic("Update called on a TimerSnapshot")
320 }
321
322
323 func (*TimerSnapshot) UpdateSince(time.Time) {
324 panic("UpdateSince called on a TimerSnapshot")
325 }
326
327
328
329 func (t *TimerSnapshot) Variance() float64 { return t.histogram.Variance() }
330
View as plain text