/
statsstorage.go
300 lines (257 loc) · 7.84 KB
/
statsstorage.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
package cmd
import (
"fmt"
"math"
"sort"
"sync"
"sync/atomic"
)
type StatsStorage struct {
storage sync.Map
max_latency int64
total_samples int64
total_keys int64
latencies []int64
latencies_arflat_size int64
}
func (s *StatsStorage) StoreLatencySample(latency int64) bool {
inc := int64(1)
s.total_samples++
if latency > s.max_latency {
s.max_latency = latency
}
count, loaded := s.storage.LoadOrStore(latency, &inc)
if loaded {
// latency already exists, increment the counter
atomic.AddInt64(count.(*int64), inc)
} else {
// new key (latency sample) added
s.total_keys++
}
return !loaded
}
func (s *StatsStorage) ForceRecomputeArray() {
//Log("Force recompute array called.")
s.latencies = nil
s.latencies_arflat_size = 0
}
// GetLatencyValuesFlattened
// this version uses more memory and is not required as the compute methods are aware of repetitions
func (s *StatsStorage) GetLatencyValuesFlattened() *[]int64 {
if s.latencies != nil {
//sorted previously and still valid
//Log("Previous latency slice is still valid. return")
return &s.latencies
}
s.latencies = []int64{}
//put the elements there
s.storage.Range(func(key, value interface{}) bool {
repetitions := *value.(*int64)
var i int64
for i = 0; i < repetitions; i++ {
_ = append(s.latencies, *key.(*int64))
}
return true
})
//sort it
sort.Slice(s.latencies, func(i, j int) bool { return s.latencies[i] < s.latencies[j] })
return &s.latencies
}
func (s *StatsStorage) GetLatencyValues() (*[]int64, int64) {
if s.latencies != nil {
//sorted previously and still valid
//Log("Previous latency slice is still valid. return")
return &s.latencies, s.latencies_arflat_size
}
//Log("Recompute latency slice")
s.latencies_arflat_size = 0 // rebuild
// create the slice
s.latencies = []int64{}
//iterate over the map, read the latencies and store them into the slice
s.storage.Range(func(key, value interface{}) bool {
//Log("Appending new value to the slice: %v\n", key.(int64))
s.latencies = append(s.latencies, key.(int64))
s.latencies_arflat_size += *value.(*int64)
return true
})
//sort it
sort.Slice(s.latencies, func(i, j int) bool { return s.latencies[i] < s.latencies[j] })
return &s.latencies, s.latencies_arflat_size
}
func (s *StatsStorage) GetAverage(percent bool) int64 {
latenciesAddr, size := s.GetLatencyValues()
latencies := *latenciesAddr
latency := int64(0)
consumedSamples := int64(0)
limit := int64(0)
if percent {
//default is take 10%, and thus compute the 90%
limit = size / 10
}
pos := size - limit
if pos == 0 { pos = 1}
//find the position in the flat array
for i := 0 ; i < len(latencies); i++ {
latencyValue := latencies[i]
latencySampleQty, _ := s.storage.Load(latencyValue)
latencySamples := *latencySampleQty.(*int64)
if consumedSamples + latencySamples >= pos {
qty := consumedSamples + latencySamples - pos
latency += (latencySamples -qty)*latencyValue
break
} else {
consumedSamples += latencySamples
latency += latencySamples * latencyValue
}
} //for
return latency / pos
}
func (s *StatsStorage) GetMedian(percent bool) int64 {
latenciesAddr, size := s.GetLatencyValues()
latencies := *latenciesAddr
latency := int64(0)
consumedSamples := int64(0)
limit := int64(0)
//interpolate_median := false
if percent {
//default is take 10%, and thus compute the 90%
limit = size / 10
}
median_pos := (size - limit)/2 // small approximation error, no big deal
if median_pos == 0 { median_pos = 1}
//if (size - limit) % 2 > 0 {
// interpolate_median = true
//}
//find the position in the flat array
for i := 0 ; i < len(latencies); i++ {
latencyValue := latencies[i]
latencySampleQty, _ := s.storage.Load(latencyValue)
latencySamples := *latencySampleQty.(*int64)
if consumedSamples + latencySamples >= median_pos {
//qty := consumedSamples + latencySamples - pos
// the latency of median is repeated in this block, just return the latency
latency = latencyValue
break
} else {
consumedSamples += latencySamples
}
} //for
return latency
}
func (s *StatsStorage) GetSTD(percent bool) float64 {
if s.total_samples <= 1 {
return 0
}
latenciesAddr, size := s.GetLatencyValues()
latencies := *latenciesAddr
limit := int64(0)
if percent {
//default is take 10%, and thus compute the 90%
limit = size / 10
}
consumedSamples := int64(0)
pos := size - limit
if pos == 0 { pos = 1}
avg := s.GetAverage(percent)
quad := int64(0)
for i := 0 ; i < len(latencies); i++ {
latencyValue := latencies[i]
latencySampleQty, _ := s.storage.Load(latencyValue)
latencySamples := *latencySampleQty.(*int64)
if consumedSamples + latencySamples >= pos {
qty := consumedSamples + latencySamples - pos
consumedSamples += latencySamples - qty
var j int64
for j = 0; j < latencySamples - qty; j++ {
quad = quad + latencyValue * latencyValue
//fmt.Println(latencyValue, latencySamples, latencyValue * latencyValue, quad)
}
break
} else {
consumedSamples += latencySamples
var j int64
for j = 0; j < latencySamples; j++ {
quad = quad + latencyValue * latencyValue
//fmt.Println(latencyValue, latencySamples, latencyValue * latencyValue, quad)
}
}
} //for
//fmt.Println(quad)
//fmt.Println(consumedSamples * avg*avg)
variance := float64(quad - (consumedSamples * (avg*avg)) / (consumedSamples - 1))
return math.Sqrt(variance)
}
func (s *StatsStorage) GetMax(percent bool) int64 {
latenciesAddr, size := s.GetLatencyValues()
latencies := *latenciesAddr
max_latency := int64(0)
consumedSamples := int64(0)
limit := int64(0)
if percent {
//default is take 10%, and thus compute the 90%
limit = size / 10
} else {
return s.max_latency
}
pos := size - limit
if pos == 0 { pos = 1}
//find the position in the flat array
for i := 0 ; i < len(latencies); i++ {
latencyValue := latencies[i]
latencySampleQty, _ := s.storage.Load(latencyValue)
latencySamples := *latencySampleQty.(*int64)
if consumedSamples + latencySamples >= pos { //may have found the max latency
// the latency of max is repeated in this block, just return the latency
//qty := consumedSamples + latencySamples - pos
max_latency = latencyValue
break
} else {
consumedSamples += latencySamples
}
} //for
return max_latency
}
func (s *StatsStorage) GetPercentile(percentile float32) int64 {
latenciesAddr, size := s.GetLatencyValues()
latencies := *latenciesAddr
consumedSamples := int64(0)
pos := int64(float64(size) * float64(percentile))
latency := int64(0)
//find the position in the flat array
for i := 0 ; i < len(latencies); i++ {
latencyValue := latencies[i]
latencySampleQty, _ := s.storage.Load(latencyValue)
latencySamples := *latencySampleQty.(*int64)
if consumedSamples + latencySamples >= pos {
//qty := consumedSamples + latencySamples - pos
// the latency of percentile is repeated in this block, just return the latency
latency = latencyValue
break
} else {
consumedSamples += latencySamples
}
} //for
return latency
}
func (s *StatsStorage) PrintStatsMap() {
fmt.Print("JSON Latencies: {")
s.storage.Range(func(key, value interface{}) bool {
fmt.Printf("\"%v\":%v,", key, *value.(*int64))
return true
})
fmt.Print("}\n")
//fmt.Println("Latencies:", a)
}
func (s *StatsStorage) ReportStats() string {
var reportline string
s.ForceRecomputeArray()
_, qty := s.GetLatencyValues()
reportline = fmt.Sprintf("iteractions=%v ",s.total_samples)
reportline = fmt.Sprintf("%v size=%v" ,reportline, qty)
reportline = fmt.Sprintf("%v average=%v" ,reportline, s.GetAverage(false))
reportline = fmt.Sprintf("%v std=%v" ,reportline, s.GetSTD(false))
reportline = fmt.Sprintf("%v median=%v" ,reportline, s.GetMedian(false))
reportline = fmt.Sprintf("%v perc90=%v" ,reportline, s.GetPercentile(0.9))
reportline = fmt.Sprintf("%v max=%v" ,reportline, s.GetMax(false))
return reportline
}