-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathevent.go
More file actions
294 lines (281 loc) · 7.55 KB
/
event.go
File metadata and controls
294 lines (281 loc) · 7.55 KB
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
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package rapid
import (
"log"
"sort"
)
// counter maps a (stringified) event to a frequency counter
type counter map[string]int
type typeEnum byte
const (
UNKNOWN typeEnum = iota
UINT
INT
FLOAT
)
type numCounter struct {
ints []int64
uints []uint64
floats []float64
t typeEnum
}
// stats maps labels to counters
type stats map[string]counter
type nStats map[string]*numCounter
type counterPair struct {
frequency int
event string
}
// Event records an event for test `t` and
// stores the event for calculating statistics.
//
// Recording events and printing a their statistic is a tool for
// analysing test data generation. It helps to understand if
// your (custom) generators produce values in the expected range.
//
// Each event has a label and an event value. To see the statistics,
// run the tests with `go test -v`.
//
func Event(t *T, label string, value string) {
if t.tb != nil {
t.tb.Helper()
}
t.statMux.Lock()
defer t.statMux.Unlock()
if t.allstats == nil {
t.allstats = make(stats)
}
c, found := t.allstats[label]
if !found {
c = make(counter)
t.allstats[label] = c
}
c[value]++
}
// NumericEvent records a numeric event for given label
// for statistic calculation of minimum, median, arithmetic mean
// and maximum values for that label. Allowed numeric values are
// all signed and unsigned integers and floats. Howver, for each
// label, all numeric values must be consistent, ie. have the the
// same type. Otherwise, a run-time panic will occur.
//
// Recording events and printing a their statistic is a tool for
// analysing test data generation. It helps to understand if
// your (custom) generators produce values in the expected range.
//
// Each event has a label and an event value. To see the statistics,
// run the tests with `go test -v`.
func NumericEvent(t *T, label string, numValue value) {
valType := UNKNOWN
var uintVal uint64
var intVal int64
var floatVal float64
if t.tb != nil {
t.tb.Helper()
}
switch x := numValue.(type) {
case uint8:
valType = UINT
uintVal = uint64(x)
case uint16:
valType = UINT
uintVal = uint64(x)
case uint32:
valType = UINT
uintVal = uint64(x)
case uint64:
valType = UINT
uintVal = uint64(x)
case uint:
valType = UINT
uintVal = uint64(x)
case int8:
valType = INT
intVal = int64(x)
case int16:
valType = INT
intVal = int64(x)
case int32:
valType = INT
intVal = int64(x)
case int64:
valType = INT
intVal = int64(x)
case int:
valType = INT
intVal = int64(x)
case float32:
valType = FLOAT
floatVal = float64(x)
case float64:
valType = FLOAT
floatVal = float64(x)
default:
t.Fatalf("numeric event is not a numeric value")
return
}
t.statMux.Lock()
defer t.statMux.Unlock()
if t.numStats == nil {
t.numStats = make(nStats)
}
c, found := t.numStats[label]
if !found {
newCounter := &numCounter{
ints: []int64{},
uints: []uint64{},
floats: []float64{},
t: valType,
}
t.numStats[label] = newCounter
c = newCounter
}
if valType != c.t {
t.Fatalf("Type of numeric event does not match. Expected: %#v, was %#v", c.t, valType)
}
switch valType {
case UINT:
c.uints = append(c.uints, uintVal)
case INT:
c.ints = append(c.ints, intVal)
case FLOAT:
c.floats = append(c.floats, floatVal)
}
}
func minMaxMeanInt(values []int64) (min, max, median int64, mean float64) {
min, max, mean = values[0], values[0], float64(values[0])
sum := mean
for i := 1; i < len(values); i++ {
if values[i] < min {
min = values[i]
}
if values[i] > max {
max = values[i]
}
sum += float64(values[i])
}
mean = sum / float64(len(values))
sort.Sort(int64Slice(values))
if len(values)%2 == 1 {
median = values[(len(values)-1)/2]
} else {
n := len(values) / 2
median = (values[n] + values[n-1]) / 2
}
return
}
func minMaxMeanUint(values []uint64) (min, max, median uint64, mean float64) {
min, max, mean = values[0], values[0], float64(values[0])
sum := mean
// log.Printf("rapid mean: values[%d] = %v", 0, values[0])
for i := 1; i < len(values); i++ {
if values[i] < min {
min = values[i]
}
if values[i] > max {
max = values[i]
}
// log.Printf("rapid mean: values[%d] = %v", i, values[i])
sum += float64(values[i])
}
mean = sum / float64(len(values))
sort.Sort(uint64Slice(values))
if len(values)%2 == 1 {
median = values[(len(values)-1)/2]
} else {
n := len(values) / 2
median = (values[n] + values[n-1]) / 2
}
return
}
func minMaxMeanFloat(values []float64) (min, max, median, mean float64) {
min, max, mean = values[0], values[0], values[0]
sum := mean
for i := 1; i < len(values); i++ {
if values[i] < min {
min = values[i]
}
if values[i] > max {
max = values[i]
}
sum += values[i]
}
mean = sum / float64(len(values))
sort.Float64s(values)
if len(values)%2 == 1 {
median = values[(len(values)-1)/2]
} else {
n := len(values) / 2
median = (values[n] + values[n-1]) / 2
}
return
}
// printStats logs a table of events and their relative frequency.
func printStats(t *T) {
t.statMux.Lock()
defer t.statMux.Unlock()
if t.tbLog && t.tb != nil {
t.tb.Helper()
}
if len(t.allstats) > 0 {
log.Printf("[rapid] Statistics for %s\n", t.Name())
for label := range t.allstats {
log.Printf("[rapid] Events with label %s", label)
s := t.allstats[label]
events := make([]counterPair, 0)
sum := 0
count := 0
for ev := range s {
sum += s[ev]
count++
events = append(events, counterPair{event: ev, frequency: s[ev]})
}
log.Printf("[rapid] Total of %d different events\n", count)
// we sort twice to sort same frequency alphabetically
sort.Slice(events, func(i, j int) bool { return events[i].event < events[j].event })
sort.SliceStable(events, func(i, j int) bool { return events[i].frequency > events[j].frequency })
for _, ev := range events {
log.Printf("[rapid] %s: %d (%f %%)\n", ev.event, ev.frequency, float32(ev.frequency)/float32(sum)*100.0)
}
}
}
if len(t.numStats) > 0 {
log.Printf("[rapid] Numerical Statistics for %s\n", t.Name())
for label := range t.numStats {
log.Printf("[rapid] Numerical events with label %s", label)
c := t.numStats[label]
switch c.t {
case UINT:
min, max, median, mean := minMaxMeanUint(c.uints)
log.Printf("[rapid] Min: %d\n", min)
log.Printf("[rapid] Median: %d\n", median)
log.Printf("[rapid] Mean: %f\n", mean)
log.Printf("[rapid] Max: %d\n", max)
case INT:
min, max, median, mean := minMaxMeanInt(c.ints)
log.Printf("[rapid] Min: %d\n", min)
log.Printf("[rapid] Median: %d\n", median)
log.Printf("[rapid] Mean: %f\n", mean)
log.Printf("[rapid] Max: %d\n", max)
case FLOAT:
min, max, median, mean := minMaxMeanFloat(c.floats)
log.Printf("[rapid] Min: %f\n", min)
log.Printf("[rapid] Median: %f\n", median)
log.Printf("[rapid] Mean: %f\n", mean)
log.Printf("[rapid] Max: %f\n", max)
}
}
}
}
// implementing the sorting interfaces for int64 and uint64 slices
type uint64Slice []uint64
type int64Slice []int64
func (x uint64Slice) Len() int { return len(x) }
func (x uint64Slice) Less(i, j int) bool { return x[i] < x[j] }
func (x uint64Slice) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x int64Slice) Len() int { return len(x) }
func (x int64Slice) Less(i, j int) bool { return x[i] < x[j] }
func (x int64Slice) Swap(i, j int) { x[i], x[j] = x[j], x[i] }