-
Notifications
You must be signed in to change notification settings - Fork 6
/
fastcsv_test.go
180 lines (154 loc) · 2.94 KB
/
fastcsv_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
package fastcsv
import (
"encoding/csv"
"io"
"math/rand"
"os"
"testing"
)
var names = []string{
"Reyes Palmer",
"Lauran Sander",
"Oswaldo Dyess",
"Jamila Tiffany",
"Shalonda Teti",
"Monty Alcott",
"Donald Brand",
"Reginald Morningstar",
"Elvie Aguiniga",
"Cris Mulford",
"Autumn Dahlquist",
"Palmer Redman",
"Merry Lesane",
"Jannie Laura",
"Reina Lofland",
"Norma Valiente",
"Millard Melville",
}
type Record struct {
First []byte `csv:"first"`
Second []byte `csv:"second"`
Third []byte `csv:"third"`
Fourth []byte `csv:"fourth"`
}
func createTestFile(filename string) {
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0655)
if os.IsExist(err) {
return
} else if err != nil {
panic(err)
}
defer f.Close()
w := csv.NewWriter(f)
w.Write([]string{"first", "second", "third", "fourth", "fifth"})
for i := 0; i < 1000000; i++ {
record := make([]string, 5)
for i := range record {
j := rand.Intn(len(names))
record[i] = names[j]
}
w.Write(record)
}
w.Flush()
if err := w.Error(); err != nil {
panic(err)
}
}
func BenchmarkRead(b *testing.B) {
var record Record
createTestFile("test.csv")
r, err := NewFileReader("test.csv", ',', &record)
if err != nil {
panic(err)
}
defer func() {
if err := r.Close(); err != nil {
panic(err)
}
}()
b.ResetTimer()
for i := 0; i < b.N; i++ {
r.Scan()
}
}
func BenchmarkStandard(b *testing.B) {
createTestFile("test.csv")
f, err := os.Open("test.csv")
if err != nil {
panic(err)
}
defer f.Close()
r := csv.NewReader(f)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := r.Read()
if err == io.EOF {
panic("Ran out of records")
} else if err != nil {
panic(err)
}
}
}
func TestComparison(t *testing.T) {
createTestFile("test.csv")
actual := make(chan [][]byte)
expected := make(chan []string)
actualNext := make(chan bool)
expectedNext := make(chan bool)
go func() {
defer close(actual)
var record Record
r, err := NewFileReader("test.csv", ',', &record)
if err != nil {
panic(err)
}
defer func() {
if err := r.Close(); err != nil {
panic(err)
}
}()
for r.Scan() {
actual <- [][]byte{record.First, record.Second, record.Third, record.Fourth}
<-actualNext
}
}()
go func() {
defer close(expected)
f, err := os.Open("test.csv")
if err != nil {
panic(err)
}
defer f.Close()
r := csv.NewReader(f)
r.Read()
for {
record, err := r.Read()
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
expected <- record[:4]
<-expectedNext
}
}()
nrecord := 0
for {
a := <-actual
e := <-expected
if a == nil && e == nil {
break
}
if len(a) != len(e) {
t.Fatalf("record %d: len(a) = %d, len(e) = %d", nrecord, len(a), len(e))
}
for i := 0; i < len(a); i++ {
if string(a[i]) != e[i] {
t.Fatalf("record %d: a[%d] = %q, e[%d] = %q", nrecord, i, a[i], i, e[i])
}
}
actualNext <- true
expectedNext <- true
nrecord++
}
}