-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlpfloat_test.go
291 lines (260 loc) · 6.78 KB
/
lpfloat_test.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
package lpfloat
import (
"fmt"
"math"
"math/rand"
"reflect"
"sort"
"strconv"
"sync"
"testing"
"time"
"unsafe"
)
func TestLPFloat_SpecialValue(t *testing.T) {
specialValues := []float64{0, -0, 1, math.NaN(), math.Inf(1), math.Inf(-1)}
for _, val := range specialValues {
lpf := FromFloat64(val)
if math.IsNaN(val) && math.IsNaN(lpf.ToFloat64()) {
continue
}
if val != lpf.ToFloat64() {
t.Errorf("expected %f, actual %f", val, lpf)
}
}
}
func TestLPFloat_AlmostEqualF64(t *testing.T) {
rand.Seed(int64(time.Now().Nanosecond()))
for i := 0; i < 1000000; i++ {
f := float64(i) * rand.Float64()
lpf := FromFloat64(f)
if !lpf.AlmostEqualF64(f) {
t.Logf("%g, %g, %g%%", f, lpf, 100*(lpf.ToFloat64()-f)/f)
t.Fatalf("%016x, %06x, %02x", *(*uint64)(unsafe.Pointer(&f)), lpf.SignAndExp, lpf.Fraction)
}
}
}
func BenchmarkLPFloat_AlmostEqual(b *testing.B) {
rand.Seed(int64(time.Now().Nanosecond()))
n := rand.NormFloat64()
m := n * (1 + 1/300)
lpN, lpM := FromFloat64(n), FromFloat64(m)
for i := 0; i < b.N; i++ {
_ = lpN.AlmostEqual(lpM)
}
}
func BenchmarkLPFloat_AlmostEqualF64(b *testing.B) {
rand.Seed(int64(time.Now().Nanosecond()))
n := rand.NormFloat64()
m := FromFloat64(n * (1 + 1/300))
for i := 0; i < b.N; i++ {
_ = m.AlmostEqualF64(n)
}
}
func TestLPFloat_Format(t *testing.T) {
codes := []string{"b", "f", "F", "g", "G", "e", "E", "x", "X", "v"}
flags := []string{"", "+", "-", " ", "0", "#"}
rand.Seed(time.Now().UnixNano())
for i := 0; i < 10; i++ {
lpf := FromFloat64(rand.NormFloat64())
expected := fmt.Sprint(lpf.ToFloat64())
actual := fmt.Sprint(lpf)
if expected != actual {
t.Fatalf("print, expected %s, actual %s", expected, actual)
}
for _, code := range codes {
for width := 0; width < 100; width++ {
for fraction := 0; fraction < 100; fraction++ {
for _, flag := range flags {
fmtCode := "%" + flag
if width > 0 {
fmtCode += strconv.Itoa(width)
}
if fraction > 0 {
fmtCode += "." + strconv.Itoa(fraction)
}
fmtCode += code
expected := fmt.Sprintf(fmtCode, lpf.ToFloat64())
actual := fmt.Sprintf(fmtCode, lpf)
if expected != actual {
t.Fatalf("%s format, expected %s, actual %s", code, expected, actual)
}
}
}
}
}
}
}
func BenchmarkAtomicAddFloat64_Parallel(b *testing.B) {
var got, expected float64
var mutex sync.Mutex
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
i++
atomicAddFloat64(&got, float64(i))
}
mutex.Lock()
expected += (1 + float64(i)) * float64(i) / 2
mutex.Unlock()
})
if atomicLoadFloat64(&got) != expected {
b.Fatalf("expected %g, got %g", expected, got)
}
}
func BenchmarkAtomicAddFloat64(b *testing.B) {
var got, expected float64
for i := 0; i < b.N; i++ {
atomicAddFloat64(&got, float64(i+1))
}
expected = (1 + float64(b.N)) * float64(b.N) / 2
if atomicLoadFloat64(&got) != expected {
b.Fatalf("expected %g, got %g", expected, got)
}
}
func TestBuckets(t *testing.T) {
bucketsList := []Buckets{new(UnSyncBuckets), new(SyncBuckets)}
const size = 1000000
const maxVal = 100
const minVal = 0.01
data := randomData(size, minVal, maxVal)
finalData := make([]float64, 0, size*3)
finalData = append(finalData, data...)
finalData = append(finalData, data...)
finalData = append(finalData, data...)
prepareData := func() {
for _, buckets := range bucketsList {
insertBuckets(buckets, data)
if buckets.Total() != size*3 {
t.Fatal("")
}
}
}
check := func() {
plainSummary := calPlainSummary(finalData, DefaultPercentilesCfg())
plainBuckets := calPlainBuckets(finalData)
for i, bucket := range bucketsList {
summary := bucket.Summary(DefaultPercentilesCfg())
if !reflect.DeepEqual(plainSummary, summary) {
t.Fatalf("%T summary,\nexpected:\t%v\nactual:\t%v", bucketsList[i], plainSummary, summary)
}
if !reflect.DeepEqual(plainBuckets, bucket.Buckets()) {
t.Fatalf("%T buckets", bucketsList[i])
}
}
}
reset := func() {
for _, buckets := range bucketsList {
buckets.Reset()
}
}
prepareData()
check()
reset()
prepareData()
check()
}
func BenchmarkLPFloat_FromFloat64(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = FromFloat64(float64(i))
}
}
func BenchmarkLPFloat_ToFloat64(b *testing.B) {
n := FromFloat64(rand.NormFloat64())
for i := 0; i < b.N; i++ {
_ = n.ToFloat64()
}
}
func BenchmarkUnSyncBuckets_Insert(b *testing.B) {
var buckets UnSyncBuckets
b.ReportAllocs()
for i := 0; i < b.N; i++ {
buckets.Insert(float64(i))
}
b.StopTimer()
//b.Logf("%.3g", buckets.Summary(DefaultPercentilesCfg()))
}
func BenchmarkSyncBuckets_Insert(b *testing.B) {
var buckets SyncBuckets
b.ReportAllocs()
for i := 0; i < b.N; i++ {
buckets.Insert(float64(i))
}
}
func BenchmarkSyncBuckets_Insert_Parallel(b *testing.B) {
var buckets SyncBuckets
// prepare random data, from 0 to 100
var data []float64
for i := 1; i < 10000; i++ {
data = append(data, math.Log10(float64(rand.Intn(i+1))))
}
dataLen := len(data)
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := rand.Intn(dataLen)
for pb.Next() {
buckets.Insert(data[i])
i = (i + 1) % dataLen
}
})
}
func insertBuckets(buckets Buckets, data []float64) {
for _, val := range data {
buckets.InsertN(val, 2)
}
for _, val := range data {
buckets.Insert(val)
}
}
func randomData(size int, min, max float64) []float64 {
rand.Seed(int64(time.Now().Nanosecond()))
buckets := make([]float64, 0, size)
for i := 0; i < size; i++ {
val := min + math.Pow(rand.Float64(), 3)*(max-min)
buckets = append(buckets, val)
}
return buckets
}
func calPlainSummary(data []float64, percentilesCfg []float32) Summary {
summary := makeSummary(percentilesCfg)
if len(data) == 0 {
return summary
}
sum := 0.0
for _, val := range data {
sum += val
}
buckets := calPlainBuckets(data)
summary.Total = uint64(len(data))
summary.Sum = FromFloat64(sum)
summary.Avg = FromFloat64(sum / float64(len(data)))
summary.Min = buckets[0].Value
summary.Max = buckets[len(buckets)-1].Value
percentileIdx := 0
var currentTotal uint64
for _, bucket := range buckets {
currentTotal += bucket.Count
for percentileIdx < len(percentilesCfg) &&
float64(currentTotal)*100 >= float64(summary.Total)*float64(percentilesCfg[percentileIdx]) {
summary.Percentiles[percentileIdx].LessThan = bucket.Value
percentileIdx++
}
}
return summary
}
func calPlainBuckets(data []float64) []Bucket {
counter := make(map[LPFloat]uint64)
for _, val := range data {
lpf := FromFloat64(val)
counter[lpf]++
}
buckets := make([]Bucket, 0, len(counter))
for val, count := range counter {
buckets = append(buckets, Bucket{Value: val, Count: count})
}
sort.Slice(buckets, func(i, j int) bool {
return buckets[i].Value.ToFloat64() < buckets[j].Value.ToFloat64()
})
return buckets
}