This repository was archived by the owner on May 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats.go
311 lines (272 loc) · 8.4 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Copyright (C) 2015 zulily, 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.
*/
package reckon
import "math"
const (
// MaxExampleKeys sets an upper bound on the number of example keys that will
// be captured during sampling
MaxExampleKeys = 10
// MaxExampleElements sets an upper bound on the number of example elements that
// will be captured during sampling
MaxExampleElements = 10
// MaxExampleValues sets an upper bound on the number of example values that
// will be captured during sampling
MaxExampleValues = 10
)
// Statistics are basic descriptive statistics that summarize data in a frequency table
type Statistics struct {
Mean float64
Min int
Max int
StdDev float64
}
// NewStatistics creates a new zero-valued Statistics instance
func NewStatistics() *Statistics {
return &Statistics{
Mean: math.NaN(),
StdDev: math.NaN(),
}
}
// powerOfTwo returns the smallest power of two that is greater than or equal to `n`
func powerOfTwo(n int) int {
p := 1
for p < n {
p = p * 2
}
return p
}
// ComputePowerOfTwoFreq converts a frequency map into a new frequency map,
// where each map key is the smallest power of two that is greater than or
// equal to the original map key.
func ComputePowerOfTwoFreq(m map[int]int64) map[int]int64 {
pf := make(map[int]int64)
for k, v := range m {
p := powerOfTwo(k)
if existing, ok := pf[p]; ok {
pf[p] = existing + v
} else {
pf[p] = v
}
}
return pf
}
// ComputeStatistics computes basic descriptive statistics about a frequency map
func ComputeStatistics(m map[int]int64) Statistics {
stats := NewStatistics()
if len(m) == 0 {
return *stats
}
min := math.MaxInt32
max := math.MinInt32
accum, count, sd := int64(0), int64(0), float64(0)
for k, v := range m {
if k < min {
min = k
}
if k > max {
max = k
}
accum += int64(k) * v
count += v
}
mean := float64(accum) / float64(count)
for k, v := range m {
kf, vf := float64(k), float64(v)
sd += ((kf - mean) * (kf - mean)) * vf
}
return Statistics{
Mean: mean,
Min: min,
Max: max,
StdDev: math.Sqrt(sd / float64(count-1)),
}
}
// add adds `elem` to the "set" (a map[<type>]bool is an idiomatic golang "set") if the
// current size of the set is less than `maxsize`
func add(set map[string]bool, elem string, maxsize int) {
if len(set) >= maxsize {
return
}
set[elem] = true
}
// Results stores data about sampled redis data structures. Map keys represent
// lengths/sizes, while map values represent the frequency with which those
// lengths/sizes occurred in the sampled data. Example keys are stored in
// golang "sets", which are maps with bool values.
type Results struct {
Name string
KeyCount int64
// Strings
StringSizes map[int]int64
StringKeys map[string]bool
StringValues map[string]bool
// Sets
SetSizes map[int]int64
SetElementSizes map[int]int64
SetKeys map[string]bool
SetElements map[string]bool
// Sorted Sets
SortedSetSizes map[int]int64
SortedSetElementSizes map[int]int64
SortedSetKeys map[string]bool
SortedSetElements map[string]bool
// Hashes
HashSizes map[int]int64
HashElementSizes map[int]int64
HashValueSizes map[int]int64
HashKeys map[string]bool
HashElements map[string]bool
HashValues map[string]bool
// Lists
ListSizes map[int]int64
ListElementSizes map[int]int64
ListKeys map[string]bool
ListElements map[string]bool
}
// NewResults constructs a new, zero-valued Results struct
func NewResults() *Results {
return &Results{
StringSizes: make(map[int]int64),
StringKeys: make(map[string]bool),
StringValues: make(map[string]bool),
SetSizes: make(map[int]int64),
SetElementSizes: make(map[int]int64),
SetKeys: make(map[string]bool),
SetElements: make(map[string]bool),
SortedSetSizes: make(map[int]int64),
SortedSetElementSizes: make(map[int]int64),
SortedSetKeys: make(map[string]bool),
SortedSetElements: make(map[string]bool),
HashSizes: make(map[int]int64),
HashElementSizes: make(map[int]int64),
HashValueSizes: make(map[int]int64),
HashKeys: make(map[string]bool),
HashElements: make(map[string]bool),
HashValues: make(map[string]bool),
ListSizes: make(map[int]int64),
ListElementSizes: make(map[int]int64),
ListKeys: make(map[string]bool),
ListElements: make(map[string]bool),
}
}
// merge inserts all key/value pairs in `b` into `a`. If `b` contains keys
// that are present in `a`, their values will be summed
func merge(a map[int]int64, b map[int]int64) {
for k, v := range b {
a[k] += v
}
}
// union performs a set union of `a` and `b`, storing the results in `a`
func union(a map[string]bool, b map[string]bool) {
for k := range b {
a[k] = true
}
}
// trim creates a new set, consisting of up to `n` random members from set `s`.
// If `len(s)` < `n`, the returned map will be of length `len(s)`. Set `s`
// remains unmodified.
func trim(s map[string]bool, n int) map[string]bool {
t := make(map[string]bool)
// map iteration is random in golang!
for k := range s {
t[k] = true
if len(t) == n {
break
}
}
return t
}
// trimAndSum removes entries from the frequency map that comprise less than
// `threshold` % of the total, returning the sum of the **original** map
func trimAndSum(m map[int]int64, threshold float64) int64 {
var s int64
var sum float64
for _, v := range m {
s += v
}
sum = float64(s)
for k, v := range m {
if float64(v)/sum <= threshold {
delete(m, k)
}
}
return s
}
// Merge adds the results from `other` into the method receiver. This method
// can be used to combine sampling results from multiple redis instances into a
// single result set.
func (r *Results) Merge(other *Results) {
r.KeyCount += other.KeyCount
// union all sets
union(r.StringKeys, other.StringKeys)
union(r.StringValues, other.StringValues)
union(r.SetKeys, other.SetKeys)
union(r.SetElements, other.SetElements)
union(r.SortedSetKeys, other.SortedSetKeys)
union(r.SortedSetElements, other.SortedSetElements)
union(r.HashKeys, other.HashKeys)
union(r.HashElements, other.HashElements)
union(r.HashValues, other.HashValues)
union(r.ListKeys, other.ListKeys)
union(r.ListElements, other.ListElements)
// merge all frequency tables
merge(r.StringSizes, other.StringSizes)
merge(r.SetSizes, other.SetSizes)
merge(r.SetElementSizes, other.SetElementSizes)
merge(r.SortedSetSizes, other.SortedSetSizes)
merge(r.SortedSetElementSizes, other.SortedSetElementSizes)
merge(r.HashSizes, other.HashSizes)
merge(r.HashElementSizes, other.HashElementSizes)
merge(r.HashValueSizes, other.HashValueSizes)
merge(r.ListSizes, other.ListSizes)
merge(r.ListElementSizes, other.ListElementSizes)
}
func (r *Results) observeSet(key string, length int, member string) {
r.KeyCount++
r.SetSizes[length]++
r.SetElementSizes[len(member)]++
add(r.SetKeys, key, MaxExampleKeys)
add(r.SetElements, member, MaxExampleElements)
}
func (r *Results) observeSortedSet(key string, length int, member string) {
r.KeyCount++
r.SortedSetSizes[length]++
r.SortedSetElementSizes[len(member)]++
add(r.SortedSetKeys, key, MaxExampleKeys)
add(r.SortedSetElements, member, MaxExampleElements)
}
func (r *Results) observeHash(key string, length int, field string, value string) {
r.KeyCount++
r.HashSizes[length]++
r.HashValueSizes[len(value)]++
r.HashElementSizes[len(field)]++
add(r.HashKeys, key, MaxExampleKeys)
add(r.HashElements, field, MaxExampleElements)
add(r.HashValues, value, MaxExampleValues)
}
func (r *Results) observeList(key string, length int, member string) {
r.KeyCount++
r.ListSizes[length]++
r.ListElementSizes[len(member)]++
add(r.ListKeys, key, MaxExampleKeys)
add(r.ListElements, member, MaxExampleElements)
}
func (r *Results) observeString(key, value string) {
r.KeyCount++
r.StringSizes[len(value)]++
add(r.StringKeys, key, MaxExampleKeys)
add(r.StringValues, value, MaxExampleValues)
}