-
Notifications
You must be signed in to change notification settings - Fork 24
/
serialization_test.go
297 lines (239 loc) · 13.3 KB
/
serialization_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
292
293
294
295
296
297
package tdigest
import (
"bytes"
"encoding/base64"
"math"
"math/rand"
"reflect"
"testing"
)
func TestEncodeDecode(t *testing.T) {
testUints := []uint64{0, 10, 100, 1000, 10000, 65535, 2147483647, 2 * math.MaxUint32}
buf := new(bytes.Buffer)
for _, i := range testUints {
err := encodeUint(buf, i)
if err != nil {
t.Error(err)
}
}
readBuf := bytes.NewReader(buf.Bytes())
for _, i := range testUints {
j, err := decodeUint(readBuf)
if err != nil {
t.Error(err)
}
if i != j {
t.Errorf("Basic encode/decode failed. Got %d, wanted %d", j, i)
}
}
}
func TestSerialization(t *testing.T) {
t1, _ := New()
for i := 0; i < 100; i++ {
_ = t1.Add(rand.Float64())
}
serialized, _ := t1.AsBytes()
t2, err := FromBytes(bytes.NewReader(serialized))
if err != nil {
t.Fatal(err)
}
assertSerialization(t, t1, t2)
err = t2.FromBytes(serialized)
if err != nil {
t.Fatal(err)
}
assertSerialization(t, t1, t2)
var toBuf []byte
toBuf = t1.ToBytes(toBuf)
if !reflect.DeepEqual(serialized, toBuf) {
t.Errorf("ToBytes serialized to something else")
}
// Make sure we don't re-allocate on buffer re-use
toBuf2 := t1.ToBytes(toBuf[:0])
if &toBuf2[0] != &toBuf[0] {
t.Errorf("Expected ToBytes() to re-use supplied slice")
}
if !reflect.DeepEqual(toBuf2, toBuf) {
t.Errorf("ToBytes serialized to something else")
}
t3, _ := New()
err = t3.FromBytes(serialized)
if err != nil {
t.Error(err)
}
assertSerialization(t, t1, t3)
// Mess up t3's internal state, deserialize again.
t3.compression = 2
t3.count = 1000
t3.summary.means = append(t3.summary.means, 2.0)
t3.summary.counts[0] = 0
err = t3.FromBytes(serialized)
if err != nil {
t.Error(err)
}
assertSerialization(t, t1, t3)
wrong := serialized[:50]
err = t3.FromBytes(wrong)
if err == nil {
t.Error("expected error")
}
wrong = wrong[:2]
err = t3.FromBytes(wrong)
if err == nil {
t.Error("expected error")
}
}
func assertSerialization(t *testing.T, t1, t2 *TDigest) {
if t1.Count() != t2.Count() ||
t1.summary.Len() != t2.summary.Len() ||
t1.compression != t2.compression {
t.Errorf("Deserialized to something different. t1=%v t2=%v", t1, t2)
}
b1, err := t1.AsBytes()
if err != nil {
t.Error(err)
}
b2, err := t2.AsBytes()
if err != nil {
t.Error(err)
}
if !bytes.Equal(b1, b2) {
t.Errorf("Deserialized to something different. b1=%q b2=%q", b1, b2)
}
// t2 is fully functional.
err = t2.Add(rand.Float64())
if err != nil {
t.Error(err)
}
err = t2.Compress()
if err != nil {
t.Error(err)
}
}
func TestFromBytesIgnoresCompression(t *testing.T) {
digest := uncheckedNew(Compression(42))
// Instructing FromBytes to use a compression different
// than the one in the payload should be ignored
payload, err := digest.AsBytes()
if err != nil {
t.Error(err)
}
other, err := FromBytes(bytes.NewReader(payload), Compression(100))
if err != nil {
t.Error(err)
}
if other.Compression() != 42 {
t.Errorf("Expected compression to be 42, got %f", other.Compression())
}
}
func TestLargeSerializaton(t *testing.T) {
t1, err := New(Compression(10))
if err != nil {
t.Error(err)
}
for i := 0; i < 100000; i++ {
t1.AddWeighted(rand.Float64(), 1000000000)
}
serialized, _ := t1.AsBytes()
serialized2 := t1.ToBytes(nil)
if !reflect.DeepEqual(serialized, serialized2) {
t.Error("serialized version differ")
}
t2, err := FromBytes(bytes.NewReader(serialized))
if err != nil {
t.Error(err)
}
t3, _ := New()
err = t3.FromBytes(serialized2)
if err != nil {
t.Error(err)
}
assertSerialization(t, t1, t2)
assertSerialization(t, t1, t3)
}
func TestJavaSmallBytesCompat(t *testing.T) {
// Base64 string generated via (<3 clojure):
// (def t (com.tdunning.math.stats.AVLTreeDigest. 100))
// (def r (java.util.Random.))
// (.setSeed r 0xDEADBEEF)
//
// (dotimes [x 100000]
// (.add t (.nextDouble r)))
//
// (def buf (java.nio.ByteBuffer/allocate (.smallByteSize t)))
// (.asSmallBytes t buf)
// (.flip buf)
// (.compress t)
// (def serialized-tdigest (.encodeToString (java.util.Base64/getEncoder) (.array buf)))
//
// (println serialized-tdigest)
serializedJavaTDigestB64 := "AAAAAkBZAAAAAAAAAAAEOzZpD1w24ySbN288eDfDHOI3jwpPN7jIyze1xXM2BzmuNc6x9DdUUcs2o1QFNvb5tzeNwTo2l0VYNgD89jaAiB83GxMBNTdLZzVjwOk3oKiDNxhS4jZ2blc2zTiiN8rlKDc7gN01HN5jNgF8bDYhIGo3BsH5NlbMcDdtCKQ3eJMUNzzuazQuLpY2y0lcNqNDdDcNDr03zOJ1N3ESMjcqxd42omxHNdA+mDbJmlo3KrIGN5i5/DegwGw10QY2NRuEmjdARF42g8qeN8L4yjajFVs1oIo9NvoNwDdrnuk2LeJGNwFHnTgGqu82TzfHN41Syzbd4xU2XjMVN1GPQjbMZOI2l91oNnY8CDdCy7U1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QLiArEC3wK7AoYCpgL7ApMDnwOpA6sDggSdBKQD+gLIAqgCxgLPAtoCvgKWAvUBhgLHAoYD2wLEAvcBxAGdAbsB7QGCApQCmALJAsUCiQOpA68D/AKSAvwB9wGHAukBugGpAaUBngG6AfwBiwKUApsClQKmArECsAKZAuEBkgGsAccBxAGLAVNFWm6PAYoBe5cBwQHhAe0BzgHLAYYBggGbAVhThAGaAaEBhQFJW1p5iQFhOic2NS0mHjJAYGpsYVZcR2VsWF5JOTBKa3hUWGdSSFJXS1NAL0VRQz83LTQ2QDYjFSs+VEpabVQzJjNAWkM1LT5MPVBDKyw5IxcUFyEgPkNNTUUxKRwfHh4PGBkZKSUZEwwZHRcXDxUeGhgdKiYaHhEQCxISEh0REyAdGhoYDxMWFhIWHhEIChQNEA0TDBMMCQ8QEx8mGwsQERcWERcRDQ8TFg4REhILBxMJCQ4UEQQDCAQFDg0EERQLFhIHDA4NBgcKAwQLCQMMEQYDBwcIEAwKBgkFAwQHCAIBCAUDCAEJCwcLBwoFBgYGBQIEBQMCAgMJAwkCAwUBAwMGAwMDAgUFAgEDBgYHBAYBAwcBBQIBBgMCAgYBAQQCAwMBAgMEAgMDAQEBAQEDAQEDAgIBAgECAwECAQMDAwECAwICAQMCAQEBAQEBAgICAQIBAQICAQEBAQEBAQICAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE="
tdigestAsBytes, err := base64.StdEncoding.DecodeString(serializedJavaTDigestB64)
if err != nil {
t.Fatalf(err.Error())
}
tdigest, err := FromBytes(bytes.NewReader(tdigestAsBytes))
if err != nil {
t.Fatalf(err.Error())
}
if tdigest.Count() != 100000 {
t.Fatalf("Expected deserialized t-digest to have a count of 100_000. Got %d", tdigest.Count())
}
assertDifferenceSmallerThan(tdigest, 0.5, 0.02, t)
assertDifferenceSmallerThan(tdigest, 0.1, 0.01, t)
assertDifferenceSmallerThan(tdigest, 0.9, 0.01, t)
assertDifferenceSmallerThan(tdigest, 0.01, 0.005, t)
assertDifferenceSmallerThan(tdigest, 0.99, 0.005, t)
assertDifferenceSmallerThan(tdigest, 0.001, 0.001, t)
assertDifferenceSmallerThan(tdigest, 0.999, 0.001, t)
}
func BenchmarkAsBytes(b *testing.B) {
b.ReportAllocs()
t1, _ := New(Compression(100))
for i := 0; i < 100; i++ {
t1.Add(rand.Float64())
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
t1.AsBytes()
}
}
func BenchmarkToBytes(b *testing.B) {
b.ReportAllocs()
t1, _ := New(Compression(100))
for i := 0; i < 100; i++ {
t1.Add(rand.Float64())
}
b.ResetTimer()
var buf []byte
for n := 0; n < b.N; n++ {
buf = t1.ToBytes(buf)
}
}
func BenchmarkFromBytes(b *testing.B) {
b.ReportAllocs()
t1, _ := New(Compression(100))
for i := 0; i < 100; i++ {
t1.Add(rand.Float64())
}
buf, _ := t1.AsBytes()
reader := bytes.NewReader(buf)
b.ResetTimer()
for n := 0; n < b.N; n++ {
reader.Reset(buf)
FromBytes(reader)
}
}
func BenchmarkFromBytesMethod(b *testing.B) {
b.ReportAllocs()
t1, _ := New(Compression(100))
for i := 0; i < 100; i++ {
t1.Add(rand.Float64())
}
buf, _ := t1.AsBytes()
b.ResetTimer()
var t2 TDigest
for n := 0; n < b.N; n++ {
t2.FromBytes(buf)
}
}