-
Notifications
You must be signed in to change notification settings - Fork 67
/
tsz_test.go
354 lines (304 loc) · 7.32 KB
/
tsz_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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package tsz
import (
"testing"
"time"
"github.com/dgryski/go-tsz/testdata"
)
func TestMarshalBinary(t *testing.T) {
s1 := New(testdata.TwoHoursData[0].T)
for _, p := range testdata.TwoHoursData {
s1.Push(p.T, p.V)
}
it1 := s1.Iter()
it1.Next()
b, err := s1.MarshalBinary()
if err != nil {
t.Error(err)
}
s2 := New(s1.T0)
err = s2.UnmarshalBinary(b)
if err != nil {
t.Error(err)
}
it := s2.Iter()
for _, w := range testdata.TwoHoursData {
if !it.Next() {
t.Fatalf("Next()=false, want true")
}
tt, vv := it.Values()
// t.Logf("it.Values()=(%+v, %+v)\n", time.Unix(int64(tt), 0), vv)
if w.T != tt || w.V != vv {
t.Errorf("Values()=(%v,%v), want (%v,%v)\n", tt, vv, w.T, w.V)
}
}
}
func BenchmarkMarshalBinary(b *testing.B) {
var err error
b.StopTimer()
s1 := New(testdata.TwoHoursData[0].T)
for _, p := range testdata.TwoHoursData {
s1.Push(p.T, p.V)
}
s1.Finish()
b.ReportAllocs()
b.StartTimer()
for i := 0; i < b.N; i++ {
_, err = s1.MarshalBinary()
}
if err != nil {
b.Errorf("Unexpected error: %v\n", err)
}
}
func BenchmarkUnmarshalBinary(b *testing.B) {
var err error
b.StopTimer()
s1 := New(testdata.TwoHoursData[0].T)
for _, p := range testdata.TwoHoursData {
s1.Push(p.T, p.V)
}
s1.Finish()
buf, err := s1.MarshalBinary()
if err != nil {
b.Error(err)
}
b.ReportAllocs()
s2 := New(s1.T0)
b.StartTimer()
for i := 0; i < b.N; i++ {
err = s2.UnmarshalBinary(buf)
}
if err != nil {
b.Errorf("Unexpected error: %v\n", err)
}
}
func TestExampleEncoding(t *testing.T) {
// Example from the paper
t0, _ := time.ParseInLocation("Jan _2 2006 15:04:05", "Mar 24 2015 02:00:00", time.Local)
tunix := uint32(t0.Unix())
s := New(tunix)
tunix += 62
s.Push(tunix, 12)
tunix += 60
s.Push(tunix, 12)
tunix += 60
s.Push(tunix, 24)
// extra tests
// floating point masking/shifting bug
tunix += 60
s.Push(tunix, 13)
tunix += 60
s.Push(tunix, 24)
// delta-of-delta sizes
tunix += 300 // == delta-of-delta of 240
s.Push(tunix, 24)
tunix += 900 // == delta-of-delta of 600
s.Push(tunix, 24)
tunix += 900 + 2050 // == delta-of-delta of 600
s.Push(tunix, 24)
it := s.Iter()
tunix = uint32(t0.Unix())
want := []struct {
t uint32
v float64
}{
{tunix + 62, 12},
{tunix + 122, 12},
{tunix + 182, 24},
{tunix + 242, 13},
{tunix + 302, 24},
{tunix + 602, 24},
{tunix + 1502, 24},
{tunix + 4452, 24},
}
for _, w := range want {
if !it.Next() {
t.Fatalf("Next()=false, want true")
}
tt, vv := it.Values()
if w.t != tt || w.v != vv {
t.Errorf("Values()=(%v,%v), want (%v,%v)\n", tt, vv, w.t, w.v)
}
}
if it.Next() {
t.Fatalf("Next()=true, want false")
}
if err := it.Err(); err != nil {
t.Errorf("it.Err()=%v, want nil", err)
}
}
func TestRoundtrip(t *testing.T) {
s := New(testdata.TwoHoursData[0].T)
for _, p := range testdata.TwoHoursData {
s.Push(p.T, p.V)
}
it := s.Iter()
for _, w := range testdata.TwoHoursData {
if !it.Next() {
t.Fatalf("Next()=false, want true")
}
tt, vv := it.Values()
// t.Logf("it.Values()=(%+v, %+v)\n", time.Unix(int64(tt), 0), vv)
if w.T != tt || w.V != vv {
t.Errorf("Values()=(%v,%v), want (%v,%v)\n", tt, vv, w.T, w.V)
}
}
if it.Next() {
t.Fatalf("Next()=true, want false")
}
if err := it.Err(); err != nil {
t.Errorf("it.Err()=%v, want nil", err)
}
}
func TestConcurrentRoundtripImmediateWrites(t *testing.T) {
testConcurrentRoundtrip(t, time.Duration(0))
}
func TestConcurrentRoundtrip1MsBetweenWrites(t *testing.T) {
testConcurrentRoundtrip(t, time.Millisecond)
}
func TestConcurrentRoundtrip10MsBetweenWrites(t *testing.T) {
testConcurrentRoundtrip(t, 10*time.Millisecond)
}
// Test reading while writing at the same time.
func testConcurrentRoundtrip(t *testing.T, sleep time.Duration) {
s := New(testdata.TwoHoursData[0].T)
//notify the reader about the number of points that have been written.
writeNotify := make(chan int)
// notify the reader when we have finished.
done := make(chan struct{})
// continuously iterate over the values of the series.
// when a write is made, the total number of points in the series
// will be sent over the channel, so we can make sure we are reading
// the correct amount of values.
go func(numPoints chan int, finished chan struct{}) {
written := 0
for {
select {
case written = <-numPoints:
default:
read := 0
it := s.Iter()
// read all of the points in the series.
for it.Next() {
tt, vv := it.Values()
expectedT := testdata.TwoHoursData[read].T
expectedV := testdata.TwoHoursData[read].V
if expectedT != tt || expectedV != vv {
t.Errorf("metric values dont match what was written. (%d, %f) != (%d, %f)\n", tt, vv, expectedT, expectedV)
}
read++
}
//check that the number of points read matches the number of points
// written to the series.
if read != written && read != written+1 {
// check if a point was written while we were running
select {
case written = <-numPoints:
// a new point was written.
if read != written && read != written+1 {
t.Errorf("expexcted %d values in series, got %d", written, read)
}
default:
t.Errorf("expexcted %d values in series, got %d", written, read)
}
}
}
//check if we have finished writing points.
select {
case <-finished:
return
default:
}
}
}(writeNotify, done)
// write points to the series.
for i := 0; i < 100; i++ {
s.Push(testdata.TwoHoursData[i].T, testdata.TwoHoursData[i].V)
writeNotify <- i + 1
time.Sleep(sleep)
}
done <- struct{}{}
}
func BenchmarkEncode(b *testing.B) {
b.SetBytes(int64(len(testdata.TwoHoursData) * 12))
for i := 0; i < b.N; i++ {
s := New(testdata.TwoHoursData[0].T)
for _, tt := range testdata.TwoHoursData {
s.Push(tt.T, tt.V)
}
}
}
func BenchmarkDecodeSeries(b *testing.B) {
b.SetBytes(int64(len(testdata.TwoHoursData) * 12))
s := New(testdata.TwoHoursData[0].T)
for _, tt := range testdata.TwoHoursData {
s.Push(tt.T, tt.V)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
it := s.Iter()
var j int
for it.Next() {
j++
}
}
}
func BenchmarkDecodeByteSlice(b *testing.B) {
b.SetBytes(int64(len(testdata.TwoHoursData) * 12))
s := New(testdata.TwoHoursData[0].T)
for _, tt := range testdata.TwoHoursData {
s.Push(tt.T, tt.V)
}
s.Finish()
bytes := s.Bytes()
buf := make([]byte, len(bytes))
b.ResetTimer()
for i := 0; i < b.N; i++ {
copy(buf, bytes)
it, _ := NewIterator(buf)
var j int
for it.Next() {
j++
}
}
}
func TestEncodeSimilarFloats(t *testing.T) {
tunix := uint32(time.Unix(0, 0).Unix())
s := New(tunix)
want := []struct {
t uint32
v float64
}{
{tunix, 6.00065e+06},
{tunix + 1, 6.000656e+06},
{tunix + 2, 6.000657e+06},
{tunix + 3, 6.000659e+06},
{tunix + 4, 6.000661e+06},
}
for _, v := range want {
s.Push(v.t, v.v)
}
s.Finish()
it := s.Iter()
for _, w := range want {
if !it.Next() {
t.Fatalf("Next()=false, want true")
}
tt, vv := it.Values()
if w.t != tt || w.v != vv {
t.Errorf("Values()=(%v,%v), want (%v,%v)\n", tt, vv, w.v, w.v)
}
}
if it.Next() {
t.Fatalf("Next()=true, want false")
}
if err := it.Err(); err != nil {
t.Errorf("it.Err()=%v, want nil", err)
}
}
func TestBstreamIteratorError(t *testing.T) {
b := newBReader([]byte(""))
_, err := bstreamIterator(b)
if err == nil {
t.Errorf("An error was expected")
}
}