-
Notifications
You must be signed in to change notification settings - Fork 33
/
floatdist.go
184 lines (161 loc) · 4.51 KB
/
floatdist.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
// Copyright (C) 2016 Space Monkey, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// WARNING: THE NON-M4 VERSIONS OF THIS FILE ARE GENERATED BY GO GENERATE!
// ONLY MAKE CHANGES TO THE M4 FILE
//
package monkit
import (
"sort"
)
// FloatDist keeps statistics about values such as
// low/high/recent/average/quantiles. Not threadsafe. Construct with
// NewFloatDist(). Fields are expected to be read from but not written to.
type FloatDist struct {
// Low and High are the lowest and highest values observed since
// construction or the last reset.
Low, High float64
// Recent is the last observed value.
Recent float64
// Count is the number of observed values since construction or the last
// reset.
Count int64
// Sum is the sum of all the observed values since construction or the last
// reset.
Sum float64
key SeriesKey
reservoir [ReservoirSize]float32
rng xorshift128
sorted bool
}
func initFloatDist(v *FloatDist, key SeriesKey) {
v.key = key
v.rng = newXORShift128()
}
// NewFloatDist creates a distribution of float64s.
func NewFloatDist(key SeriesKey) (d *FloatDist) {
d = &FloatDist{}
initFloatDist(d, key)
return d
}
// Insert adds a value to the distribution, updating appropriate values.
func (d *FloatDist) Insert(val float64) {
if d.Count != 0 {
if val < d.Low {
d.Low = val
}
if val > d.High {
d.High = val
}
} else {
d.Low = val
d.High = val
}
d.Recent = val
d.Sum += val
index := d.Count
d.Count += 1
if index < ReservoirSize {
d.reservoir[index] = float32(val)
d.sorted = false
} else {
window := d.Count
// careful, the capitalization of Window is important
if Window > 0 && window > Window {
window = Window
}
// fast, but kind of biased. probably okay
j := d.rng.Uint64() % uint64(window)
if j < ReservoirSize {
d.reservoir[int(j)] = float32(val)
d.sorted = false
}
}
}
// FullAverage calculates and returns the average of all inserted values.
func (d *FloatDist) FullAverage() float64 {
if d.Count > 0 {
return d.Sum / float64(d.Count)
}
return 0
}
// ReservoirAverage calculates the average of the current reservoir.
func (d *FloatDist) ReservoirAverage() float64 {
amount := ReservoirSize
if d.Count < int64(amount) {
amount = int(d.Count)
}
if amount <= 0 {
return 0
}
var sum float32
for i := 0; i < amount; i++ {
sum += d.reservoir[i]
}
return float64(sum / float32(amount))
}
// Query will return the approximate value at the given quantile from the
// reservoir, where 0 <= quantile <= 1.
func (d *FloatDist) Query(quantile float64) float64 {
rlen := int(ReservoirSize)
if int64(rlen) > d.Count {
rlen = int(d.Count)
}
if rlen < 2 {
return float64(d.reservoir[0])
}
reservoir := d.reservoir[:rlen]
if !d.sorted {
sort.Sort(float32Slice(reservoir))
d.sorted = true
}
if quantile <= 0 {
return float64(reservoir[0])
}
if quantile >= 1 {
return float64(reservoir[rlen-1])
}
idx_float := quantile * float64(rlen-1)
idx := int(idx_float)
diff := idx_float - float64(idx)
prior := float64(reservoir[idx])
return float64(prior + diff*(float64(reservoir[idx+1])-prior))
}
// Copy returns a full copy of the entire distribution.
func (d *FloatDist) Copy() *FloatDist {
cp := *d
cp.rng = newXORShift128()
return &cp
}
func (d *FloatDist) Reset() {
d.Low, d.High, d.Recent, d.Count, d.Sum = 0, 0, 0, 0, 0
// resetting count will reset the quantile reservoir
}
func (d *FloatDist) Stats(cb func(key SeriesKey, field string, val float64)) {
count := d.Count
cb(d.key, "count", float64(count))
if count > 0 {
cb(d.key, "sum", d.toFloat64(d.Sum))
cb(d.key, "min", d.toFloat64(d.Low))
cb(d.key, "max", d.toFloat64(d.High))
cb(d.key, "rmin", d.toFloat64(d.Query(0)))
cb(d.key, "ravg", d.toFloat64(d.ReservoirAverage()))
cb(d.key, "r10", d.toFloat64(d.Query(.1)))
cb(d.key, "r50", d.toFloat64(d.Query(.5)))
cb(d.key, "r90", d.toFloat64(d.Query(.9)))
cb(d.key, "r99", d.toFloat64(d.Query(.99)))
cb(d.key, "rmax", d.toFloat64(d.Query(1)))
cb(d.key, "recent", d.toFloat64(d.Recent))
}
}