forked from influxdata/influxdb-comparisons
/
stats.go
266 lines (231 loc) · 5.21 KB
/
stats.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package bulk_query
import (
"fmt"
"log"
"math"
"sort"
"time"
)
// Stat represents one statistical measurement.
type Stat struct {
Label []byte
Value float64
IsActual bool
}
// Init safely initializes a stat while minimizing heap allocations.
func (s *Stat) Init(label []byte, value float64) {
s.InitWithActual(label, value, true)
}
func (s *Stat) InitWithActual(label []byte, value float64, isActual bool) {
s.Label = s.Label[:0] // clear
s.Label = append(s.Label, label...)
s.Value = value
s.IsActual = isActual
}
// StatGroup collects simple streaming statistics.
type StatGroup struct {
Min float64
Max float64
Mean float64
Sum float64
Count int64
}
type StatsMap map[string]*StatGroup
// Push updates a StatGroup with a new Value.
func (s *StatGroup) Push(n float64) {
if s.Count == 0 {
s.Min = n
s.Max = n
s.Mean = n
s.Count = 1
s.Sum = n
return
}
if n < s.Min {
s.Min = n
}
if n > s.Max {
s.Max = n
}
s.Sum += n
// constant-space mean update:
sum := s.Mean*float64(s.Count) + n
s.Mean = sum / float64(s.Count+1)
s.Count++
}
// String makes a simple description of a StatGroup.
func (s *StatGroup) String() string {
return fmt.Sprintf("min: %f, max: %f, mean: %f, count: %d, sum: %f", s.Min, s.Max, s.Mean, s.Count, s.Sum)
}
type timedStat struct {
timestamp time.Time
value float64
}
type HistoryItem struct {
Value float64
Item int
}
type TimedStatGroup struct {
maxDuraton time.Duration
stats []timedStat
lastAvg float64
lastMedian float64
trendAvg *TrendStat
statHistory []*HistoryItem
}
func NewTimedStatGroup(maxDuration time.Duration, maxTrendSamples int) *TimedStatGroup {
return &TimedStatGroup{maxDuraton: maxDuration, stats: make([]timedStat, 0, 100000), trendAvg: NewTrendStat(maxTrendSamples, true), statHistory: make([]*HistoryItem, 0, 512)}
}
func (m *TimedStatGroup) Push(timestamp time.Time, value float64) {
m.stats = append(m.stats, timedStat{timestamp: timestamp, value: value})
}
func (m *TimedStatGroup) Avg() float64 {
return m.lastAvg
}
func (m *TimedStatGroup) Median() float64 {
return m.lastMedian
}
func (m *TimedStatGroup) UpdateAvg(now time.Time, workers int) (float64, float64) {
newStats := make([]timedStat, 0, len(m.stats))
last := now.Add(-m.maxDuraton)
sum := float64(0)
c := 0
for _, ts := range m.stats {
if ts.timestamp.After(last) {
sum += ts.value
c++
newStats = append(newStats, ts)
}
}
m.stats = nil
m.stats = newStats
l := len(newStats)
if l == 0 {
m.lastMedian = math.NaN()
} else {
sort.Slice(newStats, func(i, j int) bool {
return newStats[i].value < newStats[j].value
})
m.lastMedian = newStats[l/2].value
}
m.lastAvg = sum / float64(c)
m.statHistory = append(m.statHistory, &HistoryItem{m.lastAvg, workers})
m.trendAvg.Add(m.lastAvg)
return m.lastAvg, m.lastMedian
}
func (m *TimedStatGroup) FindHistoryItemBelow(val float64) *HistoryItem {
item := -1
for i := len(m.statHistory) - 2; i >= 0; i-- {
if m.statHistory[i].Value < val && m.statHistory[i+1].Value >= val {
item = i
break
}
}
if item > -1 {
return m.statHistory[item]
}
return nil
}
type TrendStat struct {
x, y []float64
size int
slope float64
intercept float64
skipFirst bool
}
func (ls *TrendStat) Add(y float64) {
c := len(ls.y)
if c == 0 {
if ls.skipFirst {
ls.skipFirst = false
return
}
}
y = y / 1000 // normalize to seconds
if c < ls.size {
ls.y = append(ls.y, y)
c++
if c < 5 { // at least 5 samples required for regression
return
}
} else { // shift left using copy and insert at last position - hopefully no reallocation
y1 := ls.y[1:]
copy(ls.y, y1)
ls.y[ls.size-1] = y
}
if c > ls.size {
panic("Bug in implementation")
}
//var r stats.Regression
var r SimpleRegression
r.hasIntercept = false
for i := 0; i < c; i++ {
r.Update(ls.x[i], ls.y[i]-ls.y[0])
}
ls.slope = r.Slope()
ls.intercept = (r.Intercept() + ls.y[0]) * 1000
}
func NewTrendStat(size int, skipFirst bool) *TrendStat {
log.Printf("Trend statistics using %d samples\n", size)
instance := TrendStat{
size: size,
slope: 0,
skipFirst: skipFirst,
}
instance.x = make([]float64, size, size)
instance.y = make([]float64, 0, size)
for i := 0; i < size; i++ {
instance.x[i] = float64(i) // X is constant array { 0, 1, 2 ... size }
}
return &instance
}
type SimpleRegression struct {
sumX float64
sumXX float64
sumY float64
sumYY float64
sumXY float64
n float64
xbar float64
ybar float64
hasIntercept bool
}
func (sr *SimpleRegression) Update(x, y float64) {
if sr.n == 0 {
sr.xbar = x
sr.ybar = y
} else {
if sr.hasIntercept {
fact1 := 1.0 + sr.n
fact2 := sr.n / (1.0 + sr.n)
dx := x - sr.xbar
dy := y - sr.ybar
sr.sumXX += dx * dx * fact2
sr.sumYY += dy * dy * fact2
sr.sumXY += dx * dy * fact2
sr.xbar += dx / fact1
sr.ybar += dy / fact1
}
}
if !sr.hasIntercept {
sr.sumXX += x * x
sr.sumYY += y * y
sr.sumXY += x * y
}
sr.sumX += x
sr.sumY += y
sr.n++
}
func (sr *SimpleRegression) Intercept() float64 {
if sr.hasIntercept {
return (sr.sumY - sr.Slope()*sr.sumX) / sr.n
} else {
return 0
}
}
func (sr *SimpleRegression) Slope() float64 {
if sr.n < 2 {
return math.NaN()
}
return sr.sumXY / sr.sumXX
}