forked from actforgood/bigcsvreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader_test.go
560 lines (487 loc) · 14.8 KB
/
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
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/bigcsvreader/blob/main/LICENSE.
package bigcsvreader_test
import (
"context"
"encoding/csv"
"errors"
"io"
"os"
"reflect"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/actforgood/bigcsvreader"
)
func TestCsvReader(t *testing.T) {
t.Parallel()
t.Run("csv file with header", testCsvReaderByHeader(true))
t.Run("csv file without header", testCsvReaderByHeader(false))
t.Run("empty file", testCsvReaderWithEmptyFile)
t.Run("not found file", testCsvReaderWithNotFoundFile)
t.Run("with 10k rows file", testCsvReaderWithDifferentFileSizesAndMaxGoroutines(1e4))
t.Run("with 100k rows file", testCsvReaderWithDifferentFileSizesAndMaxGoroutines(1e5))
t.Run("with 500k rows file", testCsvReaderWithDifferentFileSizesAndMaxGoroutines(5e5))
t.Run("context is canceled", testCsvReaderWithContextCanceled)
t.Run("invalid row", testCsvReaderWithInvalidRow)
t.Run("small buffer size", testCsvReaderWithSmallBufferSize)
}
func testCsvReaderByHeader(withHeader bool) func(t *testing.T) {
return func(t *testing.T) {
t.Parallel()
// arrange
subject := bigcsvreader.New()
subject.ColumnsCount = 3
if withHeader {
subject.SetFilePath("testdata/file_with_header.csv")
subject.FileHasHeader = true
subject.ColumnsDelimiter = ';'
} else {
subject.SetFilePath("testdata/file_without_header.csv")
}
ctx, cancelCtx := context.WithTimeout(context.Background(), 15*time.Second)
defer cancelCtx()
expectedRecords := [][]string{
{"1", "John", "33"},
{"2", "Jane", "30"},
{"3", "Mike", "18"},
{"4", "Ronaldinho", "23"},
{"5", "Elisabeth", "45"},
}
// act
rowsChans, errsChan := subject.Read(ctx)
records, err := gatherRecords(rowsChans, errsChan)
// assert
assertNil(t, err)
assertEqual(t, len(expectedRecords), len(records))
for _, expectedRecord := range expectedRecords {
found := false
for _, record := range records {
if reflect.DeepEqual(expectedRecord, record) {
found = true
break
}
}
if !found {
t.Errorf("record '%v' was expected to be found, but was not", expectedRecord)
}
}
}
}
func testCsvReaderWithEmptyFile(t *testing.T) {
t.Parallel()
// arrange
subject := bigcsvreader.New()
subject.SetFilePath("testdata/empty.csv")
ctx, cancelCtx := context.WithTimeout(context.Background(), 15*time.Second)
defer cancelCtx()
expectedErr := bigcsvreader.ErrEmptyFile
// act
rowsChans, errsChan := subject.Read(ctx)
records, err := gatherRecords(rowsChans, errsChan)
// assert
assertTrue(t, errors.Is(err, expectedErr))
assertNil(t, records)
}
func testCsvReaderWithNotFoundFile(t *testing.T) {
t.Parallel()
// arrange
subject := bigcsvreader.New()
subject.SetFilePath("testdata/this_file_does_not_exist.csv")
ctx, cancelCtx := context.WithTimeout(context.Background(), 15*time.Second)
defer cancelCtx()
expectedErr := os.ErrNotExist
// act
rowsChans, errsChan := subject.Read(ctx)
records, err := gatherRecords(rowsChans, errsChan)
// assert
assertTrue(t, errors.Is(err, expectedErr))
assertNil(t, records)
}
func testCsvReaderWithDifferentFileSizesAndMaxGoroutines(rowsCount int64) func(t *testing.T) {
return func(t *testing.T) {
t.Parallel()
// arrange
fName, err := setUpTmpCsvFile(rowsCount)
if err != nil {
t.Fatalf("prerequisite failed: could not generate CSV file: %v", err)
}
defer tearDownTmpCsvFile(fName)
subject := bigcsvreader.New()
subject.SetFilePath(fName)
subject.ColumnsCount = 5
ctx, cancelCtx := context.WithCancel(context.Background())
defer cancelCtx()
var sumIds int64
var wg sync.WaitGroup
for maxGoroutines := 1; maxGoroutines <= 16; maxGoroutines++ {
subject.MaxGoroutinesNo = maxGoroutines
sumIds = 0
expectedSumIds := rowsCount * (rowsCount + 1) / 2
// act
rowsChans, errsChan := subject.Read(ctx)
// assert
for i := 0; i < len(rowsChans); i++ {
wg.Add(1)
go func(rowsChan bigcsvreader.RowsChan, waitGr *sync.WaitGroup) {
var localSumIds int64
for record := range rowsChan {
if !assertEqual(t, 5, len(record)) {
continue
}
id, _ := strconv.ParseInt(record[colID], 10, 64)
localSumIds += id
expectedColName := colValueNamePrefix + record[colID]
assertEqual(t, expectedColName, record[colName])
assertEqual(t, colValueDescription, record[colDescription])
assertEqual(t, colValuePrice, record[colPrice])
assertEqual(t, colValueStock, record[colStock])
}
atomic.AddInt64(&sumIds, localSumIds)
waitGr.Done()
}(rowsChans[i], &wg)
}
for err := range errsChan {
assertNil(t, err)
}
wg.Wait()
assertEqual(t, expectedSumIds, sumIds)
}
}
}
func testCsvReaderWithInvalidRow(t *testing.T) {
t.Parallel()
// arrange
subject := bigcsvreader.New()
subject.SetFilePath("testdata/invalid_row.csv")
subject.ColumnsCount = 3
subject.FileHasHeader = true
var expectedErr *csv.ParseError
ctx, cancelCtx := context.WithTimeout(context.Background(), 15*time.Second)
defer cancelCtx()
// act
rowsChans, errsChan := subject.Read(ctx)
records, err := gatherRecords(rowsChans, errsChan)
// assert
assertTrue(t, errors.As(err, &expectedErr))
assertNil(t, records)
}
func testCsvReaderWithContextCanceled(t *testing.T) {
t.Parallel()
// arrange
subject := bigcsvreader.New()
subject.SetFilePath("testdata/file_without_header.csv")
subject.ColumnsCount = 3
expectedErr := context.Canceled
ctx, cancelCtx := context.WithCancel(context.Background())
// act
cancelCtx()
rowsChans, errsChan := subject.Read(ctx)
records, err := gatherRecords(rowsChans, errsChan)
// assert
assertTrue(t, errors.Is(err, expectedErr))
assertNil(t, records)
}
func testCsvReaderWithSmallBufferSize(t *testing.T) {
t.Parallel()
// arrange
subject := bigcsvreader.New()
subject.SetFilePath("testdata/file_without_header.csv")
subject.ColumnsCount = 3
subject.BufferSize = 16 // min buffer size set by bufio - Ronaldinho line has len 17 and err should arise
ctx, cancelCtx := context.WithCancel(context.Background())
defer cancelCtx()
// act
rowsChans, errsChan := subject.Read(ctx)
records, err := gatherRecords(rowsChans, errsChan)
// assert
if assertNotNil(t, err) {
assertTrue(t, strings.Contains(err.Error(), "buffer full"))
}
assertNil(t, records)
}
// gatherRecords returns the rows from big csv reader, or an error if something bad happened.
func gatherRecords(rowsChans []bigcsvreader.RowsChan, errsChan bigcsvreader.ErrsChan) ([][]string, error) {
var (
mu sync.Mutex
wg sync.WaitGroup
records = make([][]string, 0)
)
for i := 0; i < len(rowsChans); i++ {
wg.Add(1)
go func(rowsChan bigcsvreader.RowsChan, mutex *sync.Mutex, waitGr *sync.WaitGroup) {
for record := range rowsChan {
mutex.Lock()
records = append(records, record)
mu.Unlock()
}
waitGr.Done()
}(rowsChans[i], &mu, &wg)
}
for err := range errsChan {
return nil, err
}
wg.Wait()
return records, nil
}
const (
colID = iota
colName
colDescription
colPrice
colStock
)
const (
colValueNamePrefix = "Product_"
colValueDescription = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc eleifend felis quis magna auctor, ut lacinia eros efficitur. Maecenas mattis dolor a pharetra gravida. Aenean at eros sed metus posuere feugiat in vitae libero. Morbi a diam volutpat, tempor lacus sed, sagittis velit. Donec eget dignissim mauris, sed aliquam ex. Duis eros dolor, vestibulum ac aliquam eget, viverra in enim. Aenean ut turpis quis purus porta lobortis. Etiam sollicitudin lectus vitae velit tincidunt, ut volutpat justo aliquam. Aenean vitae vehicula arcu. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nunc viverra enim nec risus mollis elementum nec dictum ex. Nunc lorem eros, vulputate a rutrum nec, scelerisque non augue. Sed in egestas eros. Quisque felis lorem, vehicula ac venenatis vel, tristique id sapien. Morbi vitae odio eget orci facilisis suscipit. Cras sodales, augue vitae tincidunt tempus, diam turpis volutpat est, vitae fringilla augue leo semper augue. Integer scelerisque tempor mauris, ac posuere sem aenean"
colValuePrice = "150.99"
colValueStock = "35"
)
// setUpTmpCsvFile creates a CSV file in the OS's temp directory, like `/tmp/bigcsvreader_<noOfRows>-<randString>.csv` .
// The file will have the provided number of rows.
// Rows look like:
//
// 1,Product_1,"Lorem ipsum...",150.99,35\n
// 2,Product_2,"Lorem ipsum...",150.99,35\n
// <idIncremented>, Product_<id>, static text: Lorem ipsum..., static price: 150.99, static stock qty: 35 EOL
func setUpTmpCsvFile(rowsCount int64) (string, error) {
filePattern := "bigcsvreader_" + strconv.FormatInt(rowsCount, 10) + "-*.csv"
f, err := os.CreateTemp("", filePattern)
if err != nil {
return "", err
}
fName := f.Name()
var id int64
var buf = make([]byte, 0, 1280)
bufLenConst := 4 + 2 + 1 + len(colValueNamePrefix) + len(colValueDescription) + len(colValuePrice) + len(colValueStock) // 4 x comma, 2 x quote, 1 x \n,
for id = 1; id <= rowsCount; id++ {
buf = buf[0:0:1280]
idStr := strconv.FormatInt(id, 10)
buf = append(buf, idStr...)
buf = append(buf, ',')
buf = append(buf, colValueNamePrefix...)
buf = append(buf, idStr...)
buf = append(buf, `,"`...)
buf = append(buf, colValueDescription...)
buf = append(buf, `",`...)
buf = append(buf, colValuePrice...)
buf = append(buf, ',')
buf = append(buf, colValueStock...)
buf = append(buf, "\n"...)
bufLen := bufLenConst + 2*len(idStr)
_, err := f.Write(buf[0:bufLen])
if err != nil {
_ = f.Close()
tearDownTmpCsvFile(fName)
return "", err
}
}
_ = f.Close()
return fName, nil
}
// tearDownTmpCsvFile deletes the file provided.
func tearDownTmpCsvFile(filePath string) {
_ = os.Remove(filePath)
}
// fakeProcessRow simulates the processing a row from the CSV file.
// normally record should be validated / converted to a struct / saved into a db / sent over an API...
// here simulates an operation with the cost of 1ms.
func fakeProcessRow(_ []string) {
time.Sleep(time.Millisecond)
}
func benchmarkBigCsvReader(rowsCount int64) func(b *testing.B) {
return func(b *testing.B) {
fName, err := setUpTmpCsvFile(rowsCount)
if err != nil {
b.Fatalf("prerequisite failed: could not generate CSV file: %v", err)
}
defer tearDownTmpCsvFile(fName)
subject := bigcsvreader.New()
subject.SetFilePath(fName)
subject.ColumnsCount = 5
subject.MaxGoroutinesNo = 8
ctx, cancelCtx := context.WithCancel(context.Background())
defer cancelCtx()
var count int64
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
rowsChans, errsChan := subject.Read(ctx)
count = consumeBenchResults(rowsChans, errsChan)
if count != rowsCount {
b.Errorf("expected %d, but got %d", rowsCount, count)
}
}
}
}
// consumeBenchResults just counts the records received from big csv reader and applies a delay of 1ms.
func consumeBenchResults(rowsChans []bigcsvreader.RowsChan, _ bigcsvreader.ErrsChan) int64 {
var (
count int64
wg sync.WaitGroup
)
for i := 0; i < len(rowsChans); i++ {
wg.Add(1)
go func(rowsChan bigcsvreader.RowsChan, waitGr *sync.WaitGroup) {
var localCount int64
for record := range rowsChan {
localCount++
fakeProcessRow(record)
}
atomic.AddInt64(&count, localCount)
waitGr.Done()
}(rowsChans[i], &wg)
}
wg.Wait()
return count
}
func benchmarkGoCsvReaderReadAll(rowsCount int64) func(b *testing.B) {
return func(b *testing.B) {
fName, err := setUpTmpCsvFile(rowsCount)
if err != nil {
b.Fatalf("prerequisite failed: could not generate CSV file: %v", err)
}
defer tearDownTmpCsvFile(fName)
var count int64
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
f, err := os.Open(fName)
if err != nil {
b.Fatal(err)
}
subject := csv.NewReader(f)
subject.FieldsPerRecord = 5
subject.Comma = ','
count = 0
rows, err := subject.ReadAll()
if err != nil {
b.Error(err)
} else {
for _, record := range rows { // "consume" rows
count++
fakeProcessRow(record)
}
}
_ = f.Close()
if count != rowsCount {
b.Errorf("expected %d, but got %d", rowsCount, count)
}
}
}
}
func benchmarkGoCsvReaderReadOneByOneWithReuseRecord(rowsCount int64) func(b *testing.B) {
return func(b *testing.B) {
fName, err := setUpTmpCsvFile(rowsCount)
if err != nil {
b.Fatalf("prerequisite failed: could not generate CSV file: %v", err)
}
defer tearDownTmpCsvFile(fName)
var count int64
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
f, err := os.Open(fName)
if err != nil {
b.Fatal(err)
}
subject := csv.NewReader(f)
subject.FieldsPerRecord = 5
subject.Comma = ','
subject.ReuseRecord = true
count = 0
for {
record, err := subject.Read()
if err != nil {
if err == io.EOF {
break
}
b.Error(err)
} else { // "consume" row
count++
fakeProcessRow(record)
}
}
_ = f.Close()
if count != rowsCount {
b.Errorf("expected %d, but got %d", rowsCount, count)
}
}
}
}
func benchmarkGoCsvReaderReadOneByOneProcessParalell(rowsCount int64) func(b *testing.B) {
return func(b *testing.B) {
fName, err := setUpTmpCsvFile(rowsCount)
if err != nil {
b.Fatalf("prerequisite failed: could not generate CSV file: %v", err)
}
defer tearDownTmpCsvFile(fName)
numWorkers := runtime.GOMAXPROCS(0)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
// setup workers for parallel processing
rowsChan := make(chan []string, numWorkers)
var (
count int64
wg sync.WaitGroup
)
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func() {
var localCount int64
for record := range rowsChan {
localCount++
fakeProcessRow(record)
}
atomic.AddInt64(&count, localCount)
wg.Done()
}()
}
// sequential reading
f, err := os.Open(fName)
if err != nil {
b.Fatal(err)
}
subject := csv.NewReader(f)
subject.FieldsPerRecord = 5
subject.Comma = ','
count = 0
for {
record, err := subject.Read()
if err != nil {
if err == io.EOF {
break
}
b.Error(err)
} else { // "consume" row
rowsChan <- record
}
}
close(rowsChan)
wg.Wait()
_ = f.Close()
if count != rowsCount {
b.Errorf("expected %d, but got %d", rowsCount, count)
}
}
}
}
func Benchmark50000Rows_50Mb_withBigCsvReader(b *testing.B) {
benchmarkBigCsvReader(5e4)(b)
}
func Benchmark50000Rows_50Mb_withGoCsvReaderReadAll(b *testing.B) {
benchmarkGoCsvReaderReadAll(5e4)(b)
}
func Benchmark50000Rows_50Mb_withGoCsvReaderReadOneByOneAndReuseRecord(b *testing.B) {
benchmarkGoCsvReaderReadOneByOneWithReuseRecord(5e4)(b)
}
func Benchmark50000Rows_50Mb_withGoCsvReaderReadOneByOneProcessParalell(b *testing.B) {
benchmarkGoCsvReaderReadOneByOneProcessParalell(5e4)(b)
}