-
Notifications
You must be signed in to change notification settings - Fork 8
/
timers.go
132 lines (111 loc) · 2.2 KB
/
timers.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
package metrics
import (
"fmt"
"math"
"sort"
"sync"
"time"
)
var timers chan Timer
// for aggregating timers
var agg = struct {
count, sum map[string]int
taken map[string][]int
m sync.RWMutex
}{
count: make(map[string]int),
sum: make(map[string]int),
taken: make(map[string][]int),
}
// Timer is for timing events
type Timer struct {
start time.Time
id string
taken int
stopped bool
}
type TimerStats struct {
ID string
Count int
Average int
Percentile95 int
Percentile50 int
}
func init() {
timers = make(chan Timer, 300)
go func() {
for m := range timers {
agg.m.Lock()
agg.count[m.id]++
agg.sum[m.id] += m.taken
agg.taken[m.id] = append(agg.taken[m.id], m.taken)
agg.m.Unlock()
}
}()
}
// Start returns started Timer.
func Start() Timer {
return Timer{
start: time.Now().UTC(),
}
}
// Stops the timer
func (t *Timer) Stop() {
t.taken = int(time.Since(t.start) / time.Millisecond)
t.stopped = true
}
// Stops the timer if it is not already stopped. Tracks the time taken
// in milliseconds with identity id.
func (t *Timer) Track(id string) error {
if !t.stopped {
t.Stop()
}
t.id = id
select {
case timers <- *t:
default:
return fmt.Errorf("failed to track timer %s took %d", t.id, t.taken)
}
return nil
}
// Returns the time taken between start and stop in milliseconds.
func (t *Timer) Taken() int {
return t.taken
}
func ReadTimers() []TimerStats {
var s []TimerStats
agg.m.Lock()
for k, v := range agg.count {
s = append(s, TimerStats{
ID: k,
Count: agg.count[k],
Average: agg.sum[k] / v,
Percentile50: percentile(0.5, agg.taken[k]),
Percentile95: percentile(0.95, agg.taken[k]),
})
delete(agg.count, k)
delete(agg.sum, k)
delete(agg.taken, k)
}
agg.m.Unlock()
return s
}
// calculates the kth percentile of v
func percentile(k float64, v []int) (value int) {
if !sort.IntsAreSorted(v) {
sort.Ints(v)
}
p := k * float64(len(v))
if p != math.Trunc(p) {
idx := int(math.Ceil(p))
if idx <= len(v) {
value = v[int(math.Ceil(p))-1]
}
} else {
idx := int(math.Trunc(p))
if idx < len(v) {
value = int((v[idx-1] + v[idx]) / 2)
}
}
return
}