forked from go-avro/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datum_reader_test.go
830 lines (730 loc) · 21.3 KB
/
datum_reader_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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
package avro
import (
"bytes"
"fmt"
"sync"
"testing"
)
// ***********************
// NOTICE this file was changed beginning in November 2016 by the team maintaining
// https://github.com/go-avro/avro. This notice is required to be here due to the
// terms of the Apache license, see LICENSE for details.
// ***********************
//primitives
type primitive struct {
BooleanField bool
IntField int32
LongField int64
FloatField float32
DoubleField float64
BytesField []byte
StringField string
NullField interface{}
}
//TODO replace with encoder <-> decoder tests when decoder is available
//primitive values predefined test data
var (
primitiveBool = true
primitiveInt int32 = 7498
primitiveLong int64 = 7921326876135578931
primitiveFloat float32 = 87612736.5124367
primitiveDouble = 98671578.12563891
primitiveBytes = []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}
primitiveString = "A very long and cute string here!"
primitiveNull interface{}
)
func TestPrimitiveBinding(t *testing.T) {
datumReader := NewSpecificDatumReader()
reader, err := NewDataFileReader("test/primitives.avro", datumReader)
if err != nil {
t.Fatal(err)
}
for reader.HasNext() {
p := &primitive{}
err := reader.Next(p)
if err != nil {
t.Fatal(err)
break
} else {
assert(t, p.BooleanField, primitiveBool)
assert(t, p.IntField, primitiveInt)
assert(t, p.LongField, primitiveLong)
assert(t, p.FloatField, primitiveFloat)
assert(t, p.DoubleField, primitiveDouble)
assert(t, p.BytesField, primitiveBytes)
assert(t, p.StringField, primitiveString)
assert(t, p.NullField, primitiveNull)
}
}
}
//complex
type Complex struct {
StringArray []string
LongArray []int64
EnumField EnumValue
MapOfInts map[string]int32
UnionField string
FixedField []byte
RecordField *testRecord
}
type testRecord struct {
LongRecordField int64
StringRecordField string
IntRecordField int32
FloatRecordField float32
}
//TODO replace with encoder <-> decoder tests when decoder is available
//predefined test data for complex types
var (
complexUnion = "union value"
complexFixed = []byte{0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04}
complexRecordLong int64 = 1925639126735
complexRecordString = "I am a test record"
complexRecordInt int32 = 666
complexRecordFloat float32 = 7171.17
)
func TestComplexBinding(t *testing.T) {
datumReader := NewSpecificDatumReader()
reader, err := NewDataFileReader("test/complex.avro", datumReader)
if err != nil {
t.Fatal(err)
}
recNum := 0
for reader.HasNext() {
recNum++
c := &Complex{}
err := reader.Next(c)
if err != nil {
t.Fatal(err)
break
} else {
prefix := fmt.Sprintf("Rec %d:", recNum)
arrayLength := 5
if len(c.StringArray) != arrayLength {
t.Errorf("%s Expected string array length %d, actual %d", prefix, arrayLength, len(c.StringArray))
}
for i := 0; i < arrayLength; i++ {
if c.StringArray[i] != fmt.Sprintf("string%d", i+1) {
t.Errorf("%s Invalid string: expected %v, actual %v", prefix, fmt.Sprintf("string%d", i+1), c.StringArray[i])
}
}
if len(c.LongArray) != arrayLength {
t.Errorf("Expected long array length %d, actual %d", arrayLength, len(c.LongArray))
}
for i := 0; i < arrayLength; i++ {
if c.LongArray[i] != int64(i+1) {
t.Errorf("Invalid long: expected %v, actual %v", i+1, c.LongArray[i])
}
}
enumValues := []string{"A", "B", "C", "D"}
for i := 0; i < len(enumValues); i++ {
if enumValues[i] != c.EnumField.schema.Symbols[i] {
t.Errorf("Invalid enum value in sequence: expected %v, actual %v", enumValues[i], c.EnumField.schema.Symbols[i])
}
}
if c.EnumField.String() != enumValues[2] {
t.Errorf("Invalid enum value: expected %v, actual %v", enumValues[2], c.EnumField.String())
}
if len(c.MapOfInts) != arrayLength {
t.Errorf("Invalid map length: expected %d, actual %d", arrayLength, len(c.MapOfInts))
}
for k, v := range c.MapOfInts {
if k != fmt.Sprintf("key%d", v) {
t.Errorf("Invalid key for a map value: expected %v, actual %v", fmt.Sprintf("key%d", v), k)
}
}
if c.UnionField != complexUnion {
t.Errorf("Invalid union value: expected %v, actual %v", complexUnion, c.UnionField)
}
assert(t, c.FixedField, complexFixed)
assert(t, c.RecordField.LongRecordField, complexRecordLong)
assert(t, c.RecordField.StringRecordField, complexRecordString)
assert(t, c.RecordField.IntRecordField, complexRecordInt)
assert(t, c.RecordField.FloatRecordField, complexRecordFloat)
}
}
}
//complex within complex
type complexOfComplex struct {
ArrayStringArray [][]string
RecordArray []testRecord
IntOrStringArray []interface{}
RecordMap map[string]testRecord2
IntOrStringMap map[string]interface{}
NullOrRecordUnion *testRecord3
}
type testRecord2 struct {
DoubleRecordField float64
FixedRecordField []byte
}
type testRecord3 struct {
StringArray []string
EnumRecordField EnumValue
}
func TestComplexOfComplexBinding(t *testing.T) {
datumReader := NewSpecificDatumReader()
reader, err := NewDataFileReader("test/complex_of_complex.avro", datumReader)
if err != nil {
t.Fatal(err)
}
for reader.HasNext() {
c := &complexOfComplex{}
err := reader.Next(c)
if err != nil {
t.Fatal(err)
break
} else {
arrayLength := 5
if len(c.ArrayStringArray) != arrayLength {
t.Errorf("Expected array of arrays length %d, actual %d", arrayLength, len(c.ArrayStringArray))
}
for i := 0; i < arrayLength; i++ {
for j := 0; j < arrayLength; j++ {
if c.ArrayStringArray[i][j] != fmt.Sprintf("string%d%d", i, j) {
t.Errorf("Expected array element %s, actual %s", fmt.Sprintf("string%d%d", i, j), c.ArrayStringArray[i][j])
}
}
}
recordArrayLength := 2
if len(c.RecordArray) != recordArrayLength {
t.Errorf("Expected record array length %d, actual %d", recordArrayLength, len(c.RecordArray))
}
for i := 0; i < recordArrayLength; i++ {
rec := c.RecordArray[i]
assert(t, rec.LongRecordField, int64(i))
assert(t, rec.StringRecordField, fmt.Sprintf("TestRecord%d", i))
assert(t, rec.IntRecordField, int32(1000+i))
assert(t, rec.FloatRecordField, float32(i)+0.05)
}
intOrString := []interface{}{int32(32), "not an integer", int32(49)}
if len(c.IntOrStringArray) != len(intOrString) {
t.Errorf("Expected union array length %d, actual %d", len(intOrString), len(c.IntOrStringArray))
}
for i := 0; i < len(intOrString); i++ {
assert(t, c.IntOrStringArray[i], intOrString[i])
}
recordMapLength := 2
if len(c.RecordMap) != recordMapLength {
t.Errorf("Expected map length %d, actual %d", recordMapLength, len(c.RecordMap))
}
rec1 := c.RecordMap["a key"]
assert(t, rec1.DoubleRecordField, float64(32.5))
assert(t, rec1.FixedRecordField, []byte{0x00, 0x01, 0x02, 0x03})
rec2 := c.RecordMap["another key"]
assert(t, rec2.DoubleRecordField, float64(33.5))
assert(t, rec2.FixedRecordField, []byte{0x01, 0x02, 0x03, 0x04})
stringMapLength := 3
if len(c.IntOrStringMap) != stringMapLength {
t.Errorf("Expected string map length %d, actual %d", stringMapLength, len(c.IntOrStringMap))
}
assert(t, c.IntOrStringMap["a key"], "a value")
assert(t, c.IntOrStringMap["one more key"], int32(123))
assert(t, c.IntOrStringMap["another key"], "another value")
if len(c.NullOrRecordUnion.StringArray) != arrayLength {
t.Errorf("Expected record union string array length %d, actual %d", arrayLength, len(c.NullOrRecordUnion.StringArray))
}
for i := 0; i < arrayLength; i++ {
assert(t, c.NullOrRecordUnion.StringArray[i], fmt.Sprintf("%d", i))
}
enumValues := []string{"A", "B", "C", "D"}
for i := 0; i < len(enumValues); i++ {
if enumValues[i] != c.NullOrRecordUnion.EnumRecordField.schema.Symbols[i] {
t.Errorf("Invalid enum value in sequence: expected %v, actual %v", enumValues[i], c.NullOrRecordUnion.EnumRecordField.schema.Symbols[i])
}
}
if c.NullOrRecordUnion.EnumRecordField.String() != enumValues[3] {
t.Errorf("Invalid enum value: expected %v, actual %v", enumValues[3], c.NullOrRecordUnion.EnumRecordField.String())
}
}
}
}
func TestSpecificSelfRecursive_NoPrepare(t *testing.T) {
specificSelfRecursive(t, false)
}
func TestSpecificSelfRecursive_Prepare(t *testing.T) {
specificSelfRecursive(t, true)
}
func specificSelfRecursive(t *testing.T, prepare bool) {
type SelfRecursive struct {
Label string `avro:"a"`
B *SelfRecursive
C []*SelfRecursive
}
schema := maybePrepare(prepare, MustParseSchema(`{
"type": "record",
"name": "SelfRecursive",
"fields": [
{"name": "a", "type": "string"},
{"name": "b", "type": ["null", {"type": "SelfRecursive"}]},
{"name": "c", "type": {"type": "array", "items": {"type": "SelfRecursive"}}}
]
}`))
input := testEncodeBytes(schema, &SelfRecursive{
Label: "outer",
B: &SelfRecursive{Label: "inner"},
C: []*SelfRecursive{
&SelfRecursive{Label: "arrayInner1"},
&SelfRecursive{Label: "arrayInner2", B: &SelfRecursive{Label: "inner2Child"}},
},
})
r := NewSpecificDatumReader()
r.SetSchema(schema)
var dest SelfRecursive
err := r.Read(&dest, NewBinaryDecoder(input))
if err != nil {
t.Fatal(err)
}
assert(t, dest.Label, "outer")
assert(t, dest.B.Label, "inner")
assert(t, len(dest.C), 2)
assert(t, dest.C[0].Label, "arrayInner1")
assert(t, dest.C[1].Label, "arrayInner2")
assert(t, dest.C[1].B.Label, "inner2Child")
}
func TestSpecificCoRecursive_NoPrepare(t *testing.T) {
specificCoRecursive(t, false)
}
func TestSpecificCoRecursive_Prepare(t *testing.T) {
specificCoRecursive(t, true)
}
type coRecursive struct {
A string `avro:"a"`
B *crFriend
C *crItemC
}
type crFriend struct {
Label string `avro:"label"`
D *coRecursive `avro:"d"`
E []*coRecursive `avro:"e"`
}
type crItemC struct {
Label string
Ref *crFriend
}
func specificCoRecursive(t *testing.T, prepare bool) {
schema := maybePrepare(prepare, MustParseSchema(`{
"type": "record",
"name": "CoRecursive",
"fields": [
{"name": "a", "type": "string"},
{"name": "b", "type": [
"null",
{
"type": "record",
"name": "Friend",
"fields": [
{"name": "label", "type": "string"},
{"name": "d", "type": ["null", {"type": "CoRecursive"}]},
{"name": "e", "type": {"type": "array", "items": {"type": "CoRecursive"}}}
]
}
]},
{"name": "c", "type": [
"null",
{
"type": "record",
"name": "ItemC",
"fields": [
{"name": "label", "type": "string"},
{"name": "ref", "type": {"type": "Friend"}}
]
}
]}
]
}`))
input := testEncodeBytes(schema, &coRecursive{
A: "outer",
B: &crFriend{
Label: "inner",
D: &coRecursive{A: "co-inner-d"},
E: []*coRecursive{&coRecursive{A: "co-inner-e"}},
},
C: &crItemC{
Label: "itemC",
Ref: &crFriend{
Label: "requiredCRef",
},
},
})
assert(t, len(input), 64)
r := NewSpecificDatumReader()
r.SetSchema(schema)
var dest coRecursive
err := r.Read(&dest, NewBinaryDecoder(input))
if err != nil {
t.Fatal(err)
}
assert(t, dest.A, "outer")
assert(t, dest.B.Label, "inner")
assert(t, dest.B.D.A, "co-inner-d")
assert(t, dest.B.E[0].A, "co-inner-e")
assert(t, dest.C.Label, "itemC")
assert(t, dest.C.Ref.Label, "requiredCRef")
}
// TestSpecificArrayCrash tests against regression of a crash scenario
// The crash occurs when an array decodes an explicitly nil value (like in a
// type union). The type union works fine as a raw field but not in an array.
func TestSpecificArrayCrash(t *testing.T) {
schema := MustParseSchema(`{
"type": "record",
"name": "Rec",
"fields": [{
"name": "a",
"type": {
"type": "array",
"items": ["null", "string", "long", "float"]
}
}]
}`)
type Rec struct {
A []interface{} `avro:"a"`
}
// Write some bytes
var buf bytes.Buffer
writer := NewSpecificDatumWriter()
writer.SetSchema(schema)
prims := []interface{}{
"foo",
nil,
int64(7),
}
writer.Write(&Rec{prims}, NewBinaryEncoder(&buf))
// Now do the read. This will crash if there's any null setting issue.
var dest Rec
reader := NewSpecificDatumReader()
reader.SetSchema(schema)
err := reader.Read(&dest, NewBinaryDecoder(buf.Bytes()))
if err != nil {
t.Fatal(err)
}
if len(dest.A) != 3 {
t.Fatalf("A must be 3, got %d", len(dest.A))
}
assert(t, dest.A[0], "foo")
assert(t, dest.A[1], nil)
assert(t, dest.A[2], int64(7))
}
func TestSpecificReaderMapOfRecords(t *testing.T) {
schema := MustParseSchema(`{
"type": "record",
"name": "Rec",
"fields": [{
"name": "a",
"type": {
"type": "map",
"values": {
"type": "record",
"name": "Inner",
"fields": [
{"name": "innerA", "type": "int"}
]
}
}
}]
}`)
type Inner struct {
InnerA int32
}
type PtrRec struct {
A map[string]*Inner `avro:"a"`
}
type ValueRec struct {
A map[string]Inner `avro:"a"`
}
// Write some bytes
var buf bytes.Buffer
writer := NewSpecificDatumWriter()
writer.SetSchema(schema)
testVal := &PtrRec{
A: map[string]*Inner{
"abc": &Inner{InnerA: 7},
"def": &Inner{InnerA: 9},
},
}
writer.Write(testVal, NewBinaryEncoder(&buf))
b1 := buf.Bytes()
// Now do the read. This will crash
var dest PtrRec
reader := NewSpecificDatumReader()
reader.SetSchema(schema)
err := reader.Read(&dest, NewBinaryDecoder(b1))
if err != nil {
t.Fatal(err)
}
assert(t, dest.A["abc"].InnerA, int32(7))
assert(t, dest.A["def"].InnerA, int32(9))
var dest2 ValueRec
err = reader.Read(&dest2, NewBinaryDecoder(b1))
if err != nil {
t.Fatal(err)
}
assert(t, dest2.A["abc"].InnerA, int32(7))
assert(t, dest2.A["def"].InnerA, int32(9))
}
func TestGenericDatumReaderEmptyMap(t *testing.T) {
sch, err := ParseSchema(`{
"type": "record",
"name": "Rec",
"fields": [
{
"name": "map1",
"type": {
"type": "map",
"values": "string"
}
}
]
}`)
if err != nil {
t.Fatal(err)
}
reader := NewGenericDatumReader()
reader.SetSchema(sch)
decoder := NewBinaryDecoder([]byte{0x00})
rec := NewGenericRecord(sch)
err = reader.Read(rec, decoder)
if err != nil {
t.Fatal(err)
}
assert(t, rec.Get("map1"), make(map[string]interface{}))
}
func TestGenericDatumReaderEmptyArray(t *testing.T) {
sch, err := ParseSchema(`{
"type": "record",
"name": "Rec",
"fields": [
{
"name": "arr",
"type": {
"type": "array",
"items": "string"
}
}
]
}`)
if err != nil {
t.Fatal(err)
}
reader := NewGenericDatumReader()
reader.SetSchema(sch)
decoder := NewBinaryDecoder([]byte{0x00})
rec := NewGenericRecord(sch)
err = reader.Read(rec, decoder)
if err != nil {
t.Fatal(err)
}
assert(t, rec.Get("map1"), nil)
}
var schemaEnumA = MustParseSchema(`
{"type": "record", "name": "PlayingCard",
"fields": [
{"name": "type", "type": {"type": "enum", "name": "Type", "symbols":["HEART", "SPADE", "CLUB"]}}
]}`)
var schemaEnumB = MustParseSchema(`
{"type": "record", "name": "Car",
"fields": [
{"name": "drive", "type": {"type": "enum", "name": "DriveSystem", "symbols":["FWD", "RWD", "AWD"]}}
]}`)
func TestEnumCachingRace(t *testing.T) {
enumRaceTest(t, []Schema{schemaEnumA})
}
func TestEnumCachingRace2(t *testing.T) {
enumRaceTest(t, []Schema{schemaEnumA, schemaEnumB})
}
func enumRaceTest(t *testing.T, schemas []Schema) {
var buf bytes.Buffer
enc := NewBinaryEncoder(&buf)
enc.WriteInt(2)
parallelF(20, 100, func(routine, loop int) {
var dest GenericRecord
schema := schemas[routine%len(schemas)]
reader := NewGenericDatumReader()
reader.SetSchema(schema)
err := reader.Read(&dest, NewBinaryDecoder(buf.Bytes()))
assert(t, err, nil)
})
}
func TestEnumNegativeRegression(t *testing.T) {
var playingCard struct {
Type *EnumValue
}
var genericDest = NewGenericRecord(schemaEnumA)
reader := NewDatumReader(schemaEnumA)
///////////
// Scenario 1: negative index
var buf = []byte{0x7} // This is the encoding of the varint -4
// Before this fix, this panicked.
err := reader.Read(genericDest, NewBinaryDecoder(buf))
assert(t, err.Error(), "Enum index -4 < 0 in schema Type")
err = reader.Read(&playingCard, NewBinaryDecoder(buf))
//assert(t, err.Error(), "Enum index -4 < 0 in schema Type")
///////////
// Scenario 2: too-large positive index
buf = []byte{0x78} // This is the encoding of the varint 60
err = reader.Read(genericDest, NewBinaryDecoder(buf))
assert(t, err.Error(), "Enum index invalid! 60 from: [HEART SPADE CLUB]")
playingCard.Type = nil
err = reader.Read(&playingCard, NewBinaryDecoder(buf))
assert(t, err.Error(), "Enum index 60 too high for enum Type")
}
func parallelF(numRoutines, numLoops int, f func(routine, loop int)) {
var wg sync.WaitGroup
wg.Add(numRoutines)
for i := 0; i < numRoutines; i++ {
go func(routine int) {
defer wg.Done()
for loop := 0; loop < numLoops; loop++ {
f(routine, loop)
}
}(i)
}
}
func BenchmarkSpecificDatumReader_complex(b *testing.B) {
schema, buf := specificReaderComplexVal()
specificDecoderBench(b, schema, buf, func() interface{} {
var dest Complex
return &dest
})
}
func BenchmarkSpecificDatumReader_complex_prepared_bytes(b *testing.B) {
schema, buf := specificReaderComplexVal()
specificDecoderBench(b, Prepare(schema), buf, func() interface{} {
var dest Complex
return &dest
})
}
func BenchmarkSpecificDatumReader_complex_prepared_ioReader(b *testing.B) {
schema, buf := specificReaderComplexVal()
specificDecoderBenchReader(b, Prepare(schema), buf, func() interface{} {
var dest Complex
return &dest
})
}
type Primitive primitive
type hugeval struct {
Complex
primitive
testRecord
}
func BenchmarkSpecificDatumReader_hugeval(b *testing.B) {
schema, buf := specificReaderComplexVal()
specificDecoderBench(b, schema, buf, func() interface{} {
return &hugeval{}
})
}
func BenchmarkSpecificDatumReader_hugeval_prepared(b *testing.B) {
schema, buf := specificReaderComplexVal()
specificDecoderBench(b, Prepare(schema), buf, func() interface{} {
return &hugeval{}
})
}
func specificReaderComplexVal() (Schema, []byte) {
schema, err := ParseSchemaFile("test/schemas/test_record.avsc")
if err != nil {
panic(err)
}
e, _ := NewEnumValue("A", &EnumSchema{Symbols: []string{"A", "B", "C", "D"}})
e.Set("A")
c := newComplex()
c.EnumField.Set("A")
c.FixedField = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
buf := testEncodeBytes(schema, c)
return schema, buf
}
/////// BIG ARRAYS
var bigArraysSchema = MustParseSchema(`{
"type": "record",
"name": "bigArrays",
"fields": [
{"name": "ints", "type": {"type": "array", "items": "int"}},
{"name": "strings", "type": {"type": "array", "items": "string"}}
]
}`)
type bigArrays struct {
Ints []int32 `avro:"ints"`
Strings []string `avro:"strings"`
}
func BenchmarkSpecificDatumReader_bigArrays(b *testing.B) {
big := &bigArrays{}
for i := 0; i < 2000; i++ {
big.Ints = append(big.Ints, int32(i+1))
}
buf := testEncodeBytes(bigArraysSchema, big)
specificDecoderBench(b, bigArraysSchema, buf, func() interface{} {
return &bigArrays{}
})
}
func BenchmarkSpecificDatumReader_segmented_bigArrays(b *testing.B) {
// go-avro doesn't create segmented arrays by default. Make one ourselves.
var buf bytes.Buffer
encoder := NewBinaryEncoder(&buf)
for i := 0; i < 2000; i += 100 {
if i == 0 {
encoder.WriteArrayStart(100)
} else {
encoder.WriteArrayNext(100)
}
for j := i; j < i+100; j++ {
encoder.WriteInt(int32(j + 1))
}
}
encoder.WriteArrayNext(0)
encoder.WriteArrayStart(0)
specificDecoderBench(b, bigArraysSchema, buf.Bytes(), func() interface{} {
return &bigArrays{}
})
}
/////// UTILITIES
func specificDecoderBench(b *testing.B, schema Schema, buf []byte, destFunc func() interface{}) {
specificDecoderBenchGeneric(b, schema, buf, destFunc, false)
}
func specificDecoderBenchReader(b *testing.B, schema Schema, buf []byte, destFunc func() interface{}) {
specificDecoderBenchGeneric(b, schema, buf, destFunc, true)
}
func specificDecoderBenchGeneric(b *testing.B, schema Schema, buf []byte, destFunc func() interface{}, ioReader bool) {
b.ReportAllocs()
datumReader := NewSpecificDatumReader()
datumReader.SetSchema(schema)
b.ResetTimer()
if ioReader {
b.RunParallel(func(pb *testing.PB) {
dest := destFunc()
for pb.Next() {
br := bytes.NewReader(buf)
dec := NewBinaryDecoderReader(br)
err := datumReader.Read(dest, dec)
if err != nil {
b.Fatal(err)
}
}
})
} else {
b.RunParallel(func(pb *testing.PB) {
dest := destFunc()
for pb.Next() {
dec := NewBinaryDecoder(buf)
err := datumReader.Read(dest, dec)
if err != nil {
b.Fatal(err)
}
}
})
}
}
func testEncodeBytes(schema Schema, rec interface{}) []byte {
var buf bytes.Buffer
w := NewSpecificDatumWriter()
w.SetSchema(schema)
encoder := NewBinaryEncoder(&buf)
err := w.Write(rec, encoder)
if err != nil {
panic(err)
}
return buf.Bytes()
}
func maybePrepare(prepare bool, s Schema) Schema {
if prepare {
s = Prepare(s)
}
return s
}