forked from thatguystone/cog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.go
209 lines (165 loc) · 4.04 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package statc
import (
"math"
"math/big"
"math/rand"
"sort"
"sync"
"time"
)
// A Timer is used to time how long things take to run
type Timer struct {
mtx sync.Mutex
timerSnap
sampPercent int // Percent of samples to save
samples int64Slice
// Cached names
nStddev, nMean, nMin, nMax, nCount, nP50, nP75, nP90, nP95 Name
}
type int64Slice []int64
type timerSnap struct {
sum int64
sumOfSq *big.Int
stddev int64
min, max int64
count int64
p50, p75, p90, p95 int64 // 50th, 75th, 90th, and 95th percentiles
}
var (
// This amounts to ~2x speedup
bigPool = sync.Pool{
New: func() interface{} {
return big.NewInt(0)
},
}
// Rand is not safe to use concurrently, and the global rand functions all
// use a mutex. Another pool to the rescue!
randPool = sync.Pool{
New: func() interface{} {
return rand.New(rand.NewSource(time.Now().UnixNano()))
},
}
)
func bigInt(i int64) *big.Int {
return bigPool.Get().(*big.Int).SetInt64(i)
}
// NewTimer creates a new timer that outputs stats prefixed with the given
// name. The names are cached internally to limit allocations.
//
// The timer also reports percentiles of data by sampling. The given
// `sampPercent` controls what percent of samples to save for percentile
// calculations (0 - 100).
func NewTimer(name Name, sampPercent int) (t *Timer) {
if sampPercent < 0 {
sampPercent = 0
}
if sampPercent > 100 {
sampPercent = 100
}
t = &Timer{
sampPercent: sampPercent,
nStddev: name.Join("stddev"),
nMean: name.Join("mean"),
nMin: name.Join("min"),
nMax: name.Join("max"),
nCount: name.Join("count"),
nP50: name.Join("p50"),
nP75: name.Join("p75"),
nP90: name.Join("p90"),
nP95: name.Join("p95"),
}
t.timerSnap.reset()
return t
}
// TimeFunc times how long it takes the given function to run
func (t *Timer) TimeFunc(cb func()) {
start := time.Now()
cb()
t.Add(time.Now().Sub(start))
}
// Add adds timing information
func (t *Timer) Add(dd time.Duration) {
d := int64(dd)
sq := bigInt(d)
sq.Mul(sq, sq)
rand := randPool.Get().(*rand.Rand)
keep := t.sampPercent > int(rand.Int31n(100))
randPool.Put(rand)
t.mtx.Lock()
t.count++
t.sumOfSq.Add(t.sumOfSq, sq)
t.sum += d
if d > t.max {
t.max = d
}
if d < t.min {
t.min = d
}
if keep {
t.samples = append(t.samples, d)
}
t.mtx.Unlock()
bigPool.Put(sq)
}
// Snapshot implements Snapshotter
func (t *Timer) Snapshot(a Adder) {
t.snapshot(a, false)
}
func (t *Timer) snapshot(a Adder, ignoreIfEmpty bool) {
nsamps := make(int64Slice, 0, t.count)
tsr := timerSnap{}
tsr.reset()
t.mtx.Lock()
ts := t.timerSnap
t.timerSnap = tsr
samps := t.samples
t.samples = nsamps
t.mtx.Unlock()
sort.Sort(samps)
if ts.min == math.MaxInt64 {
ts.min = 0
}
if ts.count == 0 && ignoreIfEmpty {
return
}
var mean int64
if ts.count > 0 {
// This is the equivalent of: ts.sumOfSq - ((ts.sum * ts.sum) / ts.count)
sumSq := bigInt(ts.sum)
sumSq.Mul(sumSq, sumSq)
cnt := bigInt(ts.count)
sumSq.Div(sumSq, cnt)
bigPool.Put(cnt)
ss := ts.sumOfSq.Sub(ts.sumOfSq, sumSq).Int64()
bigPool.Put(sumSq)
ts.stddev = int64(math.Sqrt(float64(ss / ts.count)))
mean = ts.sum / ts.count
}
if len(samps) > 0 {
l := float64(len(samps))
ts.p50 = samps[int(math.Ceil(l*.50))-1]
ts.p75 = samps[int(math.Ceil(l*.75))-1]
ts.p90 = samps[int(math.Ceil(l*.90))-1]
ts.p95 = samps[int(math.Ceil(l*.95))-1]
}
a.AddInt(t.nStddev, ts.stddev)
a.AddInt(t.nMean, mean)
a.AddInt(t.nMin, ts.min)
a.AddInt(t.nMax, ts.max)
a.AddInt(t.nCount, ts.count)
a.AddInt(t.nP50, ts.p50)
a.AddInt(t.nP75, ts.p75)
a.AddInt(t.nP90, ts.p90)
a.AddInt(t.nP95, ts.p95)
ts.free()
}
func (p int64Slice) Len() int { return len(p) }
func (p int64Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (ts *timerSnap) reset() {
ts.sumOfSq = bigInt(0)
ts.min = math.MaxInt64
}
func (ts *timerSnap) free() {
bigPool.Put(ts.sumOfSq)
}