forked from jamiealquiza/tachymeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calcs.go
212 lines (167 loc) · 4.15 KB
/
calcs.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
210
211
212
package tachymeter
import (
"fmt"
"math"
"sort"
"time"
)
// Calc summarizes Tachymeter sample data
// and returns it in the form of a *Metrics.
func (m *Tachymeter) Calc() *Metrics {
metrics := &Metrics{
Histogram: &Histogram{},
Name: m.Name,
}
walltime := m.WallTime
hbins := m.HBins
m.RLock()
metrics.Count = int(m.Count)
metrics.Samples = int(math.Min(float64(metrics.Count), float64(m.Size)))
times := make(timeSlice, metrics.Samples)
copy(times, m.Times[:metrics.Samples])
m.RUnlock()
if len(times) == 0 {
return metrics
}
sort.Sort(times)
metrics.Time.Cumulative = times.cumulative()
var rateTime float64
if walltime != 0 {
rateTime = float64(metrics.Count) / float64(walltime)
} else {
rateTime = float64(metrics.Samples) / float64(metrics.Time.Cumulative)
}
metrics.Rate.Second = rateTime * 1e9
metrics.Time.Avg = times.avg()
metrics.Time.HMean = times.hMean()
metrics.Time.P50 = times[times.Len()/2]
metrics.Time.P75 = times.p(0.75, m.order)
metrics.Time.P95 = times.p(0.95, m.order)
metrics.Time.P99 = times.p(0.99, m.order)
metrics.Time.P999 = times.p(0.999, m.order)
metrics.Time.Long5p = times.long5p()
metrics.Time.Short5p = times.short5p()
metrics.Time.Min = times.min()
metrics.Time.Max = times.max()
metrics.Time.Range = times.srange()
metrics.Time.StdDev = times.stdDev()
metrics.Histogram, metrics.HistogramBinSize = times.hgram(hbins)
return metrics
}
// hgram returns a histogram of event durations in
// b bins, along with the bin size.
func (ts timeSlice) hgram(b int) (*Histogram, time.Duration) {
res := time.Duration(1000)
// Interval is the time range / n bins.
interval := time.Duration(int64(ts.srange()) / int64(b))
high := ts.min() + interval
low := ts.min()
max := ts.max()
hgram := &Histogram{}
pos := 1 // Bin position.
bstring := fmt.Sprintf("%s - %s", low/res*res, high/res*res)
bin := map[string]uint64{}
for _, v := range ts {
// If v fits in the current bin,
// increment the bin count.
if v <= high {
bin[bstring]++
} else {
// If not, prepare the next bin.
*hgram = append(*hgram, bin)
bin = map[string]uint64{}
// Update the high/low range values.
low = high + time.Nanosecond
high += interval
// if we're going into the
// last bin, set high to max.
if pos == b-1 {
high = max
}
bstring = fmt.Sprintf("%s - %s", low/res*res, high/res*res)
// The value didn't fit in the previous
// bin, so the new bin count should
// be incremented.
bin[bstring]++
pos++
}
}
*hgram = append(*hgram, bin)
return hgram, interval
}
// These should be self-explanatory:
func (ts timeSlice) cumulative() time.Duration {
var total float64
for _, t := range ts {
total += float64(t)
}
return time.Duration(total)
}
func (ts timeSlice) hMean() time.Duration {
var total float64
for _, t := range ts {
total += (1 / float64(t))
}
return time.Duration(float64(ts.Len()) / total)
}
func (ts timeSlice) avg() time.Duration {
var total float64
for _, t := range ts {
total += float64(t)
}
return time.Duration(total / float64(ts.Len()))
}
func (ts timeSlice) p(p float64, o Order) time.Duration {
if o == Descending {
p = 1 - p
}
i := int(float64(len(ts))*p+0.5) - 1
if i < 0 {
return ts[0]
}
return ts[i]
}
func (ts timeSlice) stdDev() time.Duration {
m := ts.avg()
s := 0.00
for _, t := range ts {
s += math.Pow(float64(m-t), 2)
}
msq := s / float64(ts.Len())
return time.Duration(math.Sqrt(msq))
}
func (ts timeSlice) long5p() time.Duration {
set := ts[int(float64(ts.Len())*0.95+0.5):]
if len(set) <= 1 {
return ts[ts.Len()-1]
}
var total float64
var i int
for _, n := range set {
total += float64(n)
i++
}
return time.Duration(total / float64(i))
}
func (ts timeSlice) short5p() time.Duration {
set := ts[:int(float64(ts.Len())*0.05+0.5)]
if len(set) <= 1 {
return ts[0]
}
var total float64
var i int
for _, n := range set {
total += float64(n)
i++
}
return time.Duration(total / float64(i))
}
func (ts timeSlice) min() time.Duration {
return ts[0]
}
func (ts timeSlice) max() time.Duration {
return ts[ts.Len()-1]
}
func (ts timeSlice) srange() time.Duration {
return ts.max() - ts.min()
}