-
Notifications
You must be signed in to change notification settings - Fork 0
/
float_test.go
114 lines (93 loc) · 2.36 KB
/
float_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
package main
import (
"bytes"
"context"
"io"
"math/rand"
"os"
"testing"
"parquet/parquet"
)
type floatHolder struct {
F float32 `parquet:"f"`
}
func TestFloatWriter(t *testing.T) {
values := []floatHolder{{1}, {3}, {1}, {6}, {7}, {9}, {3}}
testCases := map[string][]writeOption{
"Default": {},
"Plain Dictionary": {WithEncodingHint("f", parquet.Encoding_PLAIN_DICTIONARY)},
}
for name, opts := range testCases {
t.Run(name, func(t *testing.T) {
buf := bytes.NewBuffer(nil)
err := write(context.Background(), buf, values, opts...)
if err != nil {
t.Fatal(err)
}
data := buf.Bytes()
os.WriteFile("/tmp/out.parquet", data, 0666)
f := newFile(data)
readValues := make([]floatHolder, f.NumRows())
parse(f, readValues)
if len(values) != len(readValues) {
t.Fatal("bad length")
}
for i := range values {
if values[i] != readValues[i] {
t.Fatalf("Bad at index %v, wanted %v, got %v", i, values[i], readValues[i])
}
}
})
}
}
func floatValues(n int) []floatHolder {
r := rand.New(rand.NewSource(0))
values := make([]floatHolder, n)
for i := range values {
values[i].F = r.Float32()
}
return values
}
var data []byte
// BenchmarkFloatWriterPlain-16 5133 232429 ns/op 403187 B/op 88 allocs/op
func BenchmarkFloatWriterPlain(b *testing.B) {
values := floatValues(100000)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
err := write(context.Background(), io.Discard, values)
if err != nil {
b.Fatal(err)
}
}
}
// BenchmarkFloatWriterDictionary-16 147 7941223 ns/op 4116423 B/op 1772 allocs/op
func BenchmarkFloatWriterDictionary(b *testing.B) {
values := floatValues(100000)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
err := write(context.Background(), io.Discard, values,
WithEncodingHint("f", parquet.Encoding_PLAIN_DICTIONARY))
if err != nil {
b.Fatal(err)
}
}
}
// BenchmarkFloatReaderPlain-16 3846 318899 ns/op 804387 B/op 36 allocs/op
func BenchmarkFloatReaderPlain(b *testing.B) {
values := floatValues(100000)
buf := bytes.NewBuffer(nil)
err := write(context.Background(), buf, values)
if err != nil {
b.Fatal(err)
}
data = buf.Bytes()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
f := newFile(data)
out := make([]floatHolder, f.NumRows())
parse(f, out)
}
}