forked from rcrowley/go-metrics
-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer.go
122 lines (102 loc) · 2.87 KB
/
timer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package metrics
import "time"
// Timers capture the duration and rate of events.
//
// This is an interface so as to encourage other structs to implement
// the Timer API as appropriate.
type Timer interface {
Count() int64
Max() int64
Mean() float64
Min() int64
Percentile(float64) float64
Percentiles([]float64) []float64
Rate1() float64
Rate5() float64
Rate15() float64
RateMean() float64
StdDev() float64
Time(func())
Update(time.Duration)
UpdateSince(time.Time)
}
// The standard implementation of a Timer uses a Histogram and Meter directly.
type StandardTimer struct {
h Histogram
m Meter
}
// Force the compiler to check that StandardTimer implements Timer.
var _ Timer = &StandardTimer{}
// Create a new timer with the given Histogram and Meter.
func NewCustomTimer(h Histogram, m Meter) *StandardTimer {
return &StandardTimer{h, m}
}
// Create a new timer with a standard histogram and meter. The histogram
// will use an exponentially-decaying sample with the same reservoir size
// and alpha as UNIX load averages.
func NewTimer() *StandardTimer {
return &StandardTimer{
NewHistogram(NewExpDecaySample(1028, 0.015)),
NewMeter(),
}
}
// Return the count of inputs.
func (t *StandardTimer) Count() int64 {
return t.h.Count()
}
// Return the maximal value seen.
func (t *StandardTimer) Max() int64 {
return t.h.Max()
}
// Return the mean of all values seen.
func (t *StandardTimer) Mean() float64 {
return t.h.Mean()
}
// Return the minimal value seen.
func (t *StandardTimer) Min() int64 {
return t.h.Min()
}
// Return an arbitrary percentile of all values seen.
func (t *StandardTimer) Percentile(p float64) float64 {
return t.h.Percentile(p)
}
// Return a slice of arbitrary percentiles of all values seen.
func (t *StandardTimer) Percentiles(ps []float64) []float64 {
return t.h.Percentiles(ps)
}
// Return the meter's one-minute moving average rate of events.
func (t *StandardTimer) Rate1() float64 {
return t.m.Rate1()
}
// Return the meter's five-minute moving average rate of events.
func (t *StandardTimer) Rate5() float64 {
return t.m.Rate5()
}
// Return the meter's fifteen-minute moving average rate of events.
func (t *StandardTimer) Rate15() float64 {
return t.m.Rate15()
}
// Return the meter's mean rate of events.
func (t *StandardTimer) RateMean() float64 {
return t.m.RateMean()
}
// Return the standard deviation of all values seen.
func (t *StandardTimer) StdDev() float64 {
return t.h.StdDev()
}
// Record the duration of the execution of the given function.
func (t *StandardTimer) Time(f func()) {
ts := time.Now()
f()
t.Update(time.Since(ts))
}
// Record the duration of an event.
func (t *StandardTimer) Update(d time.Duration) {
t.h.Update(int64(d))
t.m.Mark(1)
}
// Record the duration of an event that started at a time and ends now.
func (t *StandardTimer) UpdateSince(ts time.Time) {
t.h.Update(int64(time.Since(ts)))
t.m.Mark(1)
}