-
Notifications
You must be signed in to change notification settings - Fork 2
/
timer.go
153 lines (117 loc) · 2.9 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package metrics
import "time"
// Timers capture the duration and rate of events.
type Timer interface {
// Return the count of inputs.
Count() int64
// Return the maximal value seen.
Max() int64
// Return the mean of all values seen.
Mean() float64
// Return the minimal value seen.
Min() int64
// Return an arbitrary percentile of all values seen.
Percentile(p float64) float64
// Return a slice of arbitrary percentiles of all values seen.
Percentiles(ps []float64) []float64
// Return the meter's one-minute moving average rate of events.
Rate1() float64
// Return the meter's five-minute moving average rate of events.
Rate5() float64
// Return the meter's fifteen-minute moving average rate of events.
Rate15() float64
// Return the meter's mean rate of events.
RateMean() float64
// Return the standard deviation of all values seen.
StdDev() float64
// Start captures the current time and returns a value which implements Stop
// to log the elapsed time. It should be used like:
//
// defer timer.Start().Stop()
Start() interface {
Stop()
}
// Record the duration of an event.
Update(d time.Duration)
// Record the duration of an event that started at a time and ends now.
UpdateSince(t time.Time)
// Tick the clock to update the moving average.
Tick()
}
type capture struct {
timer Timer
start time.Time
}
func (c *capture) Stop() {
c.timer.UpdateSince(c.start)
}
// The standard implementation of a Timer uses a Histogram and Meter directly.
type timer struct {
h Histogram
m Meter
}
// Create a new timer with the given Histogram and Meter.
func NewCustomTimer(h Histogram, m Meter) Timer {
return &timer{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() Timer {
return &timer{
NewHistogram(NewExpDecaySample(1028, 0.015)),
NewMeter(),
}
}
func (t *timer) Count() int64 {
return t.h.Count()
}
func (t *timer) Max() int64 {
return t.h.Max()
}
func (t *timer) Mean() float64 {
return t.h.Mean()
}
func (t *timer) Min() int64 {
return t.h.Min()
}
func (t *timer) Percentile(p float64) float64 {
return t.h.Percentile(p)
}
func (t *timer) Percentiles(ps []float64) []float64 {
return t.h.Percentiles(ps)
}
func (t *timer) Rate1() float64 {
return t.m.Rate1()
}
func (t *timer) Rate5() float64 {
return t.m.Rate5()
}
func (t *timer) Rate15() float64 {
return t.m.Rate15()
}
func (t *timer) RateMean() float64 {
return t.m.RateMean()
}
func (t *timer) StdDev() float64 {
return t.h.StdDev()
}
func (t *timer) Start() interface {
Stop()
} {
return &capture{
start: time.Now(),
timer: t,
}
}
func (t *timer) Update(d time.Duration) {
t.h.Update(int64(d))
t.m.Mark(1)
}
func (t *timer) UpdateSince(ts time.Time) {
t.h.Update(int64(time.Since(ts)))
t.m.Mark(1)
}
func (t *timer) Tick() {
t.m.Tick()
}