-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsvreader_bench_test.go
163 lines (148 loc) · 3.4 KB
/
dsvreader_bench_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
package dsvreader
import (
"bytes"
"fmt"
"strings"
"testing"
)
func BenchmarkReaderBytes(b *testing.B) {
for _, rows := range []int{100, 1e3, 1e4} {
for _, cols := range []int{1, 10, 100} {
name := fmt.Sprintf("%d_%d", rows, cols)
b.Run(name, func(b *testing.B) {
benchmarkReaderBytes(b, rows, cols)
})
}
}
}
func benchmarkReaderBytes(b *testing.B, rows, cols int) {
b.Helper()
b.StopTimer()
bb := createBytesTSV(rows, cols)
br := bytes.NewReader(bb)
r := NewTSV(br)
b.StartTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
benchmarkReaderBytesSingleIter(b, r, rows, cols)
br.Reset(bb)
r.Reset(br)
}
}
func benchmarkReaderBytesSingleIter(b *testing.B, r *Reader, rows, cols int) {
b.Helper()
for i := 0; i < rows; i++ {
if !r.Next() {
b.Fatalf("Reader.Next must return true on row #%d", i+1)
}
for j := 0; j < cols; j++ {
bb := r.Bytes()
if len(bb) == 0 {
b.Fatalf("expecting non-empty bytes on row #%d, col #%d", i+1, j+1)
}
}
}
}
func BenchmarkReaderInt(b *testing.B) {
for _, rows := range []int{100, 1e3, 1e4} {
for _, cols := range []int{1, 10, 100} {
name := fmt.Sprintf("%d_%d", rows, cols)
b.Run(name, func(b *testing.B) {
benchmarkReaderInt(b, rows, cols)
})
}
}
}
func benchmarkReaderInt(b *testing.B, rows, cols int) {
b.Helper()
b.StopTimer()
bb := createIntTSV(rows, cols)
br := bytes.NewReader(bb)
r := NewTSV(br)
b.StartTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
benchmarkReaderIntSingleIter(b, r, rows, cols)
br.Reset(bb)
r.Reset(br)
}
}
func benchmarkReaderIntSingleIter(b *testing.B, r *Reader, rows, cols int) {
b.Helper()
for i := 0; i < rows; i++ {
if !r.Next() {
b.Fatalf("Reader.Next must return true on row #%d", i+1)
}
for j := 0; j < cols; j++ {
n := r.Int()
if n == 0 {
b.Fatalf("expecting non-zero int on row #%d, col #%d", i+1, j+1)
}
}
}
}
func BenchmarkReaderUint(b *testing.B) {
for _, rows := range []int{100, 1e3, 1e4} {
for _, cols := range []int{1, 10, 100} {
name := fmt.Sprintf("%d_%d", rows, cols)
b.Run(name, func(b *testing.B) {
benchmarkReaderUint(b, rows, cols)
})
}
}
}
func benchmarkReaderUint(b *testing.B, rows, cols int) {
b.Helper()
b.StopTimer()
bb := createUintTSV(rows, cols)
br := bytes.NewReader(bb)
r := NewTSV(br)
b.StartTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
benchmarkReaderUintSingleIter(b, r, rows, cols)
br.Reset(bb)
r.Reset(br)
}
}
func benchmarkReaderUintSingleIter(b *testing.B, r *Reader, rows, cols int) {
b.Helper()
for i := 0; i < rows; i++ {
if !r.Next() {
b.Fatalf("Reader.Next must return true on row #%d", i+1)
}
for j := 0; j < cols; j++ {
n := r.Uint()
if n == 0 {
b.Fatalf("expecting non-zero uint on row #%d, col #%d", i+1, j+1)
}
}
}
}
func createBytesTSV(rows, cols int) []byte {
var bb bytes.Buffer
for i := 0; i < rows; i++ {
var ss []string
for j := 0; j < cols; j++ {
s := fmt.Sprintf("cell %d %d", i, j)
ss = append(ss, s)
}
fmt.Fprintf(&bb, "%s\n", strings.Join(ss, "\t"))
}
return bb.Bytes()
}
func createIntTSV(rows, cols int) []byte {
return createUintTSV(rows, cols)
}
func createUintTSV(rows, cols int) []byte {
var bb bytes.Buffer
for i := 0; i < rows; i++ {
var ss []string
for j := 0; j < cols; j++ {
s := fmt.Sprintf("%d", i*cols+j+1)
ss = append(ss, s)
}
fmt.Fprintf(&bb, "%s\n", strings.Join(ss, "\t"))
}
return bb.Bytes()
}