/
io_test.go
104 lines (89 loc) · 1.74 KB
/
io_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
package main
import (
"io"
"os"
"testing"
"github.com/dustin/randbo"
)
type fakeLineReader struct {
i int
lim int
}
func (f *fakeLineReader) Read(p []byte) (n int, err error) {
if f.i >= f.lim {
return 0, io.EOF
}
max := len(p)
if f.i+max > f.lim {
max = f.lim - f.i
}
x := 33 % len(p)
for i := 0; i < max; i++ {
if i == x {
p[i] = '\n'
} else {
p[i] = 'g'
}
}
f.i += max
return max, nil
}
func benchStream(b *testing.B, length int) {
b.SetBytes(int64(length))
for i := 0; i < b.N; i++ {
r := fakeLineReader{lim: length}
_, err := NewBuffer(&r)
if err != nil {
b.Fatalf("got error when reading: %v\n", err)
}
}
}
func BenchmarkStream_16(b *testing.B) {
benchStream(b, 16)
}
func BenchmarkStream_1024(b *testing.B) {
benchStream(b, 1024)
}
func BenchmarkStream_8192(b *testing.B) {
benchStream(b, 8192)
}
func BenchmarkStream_102400(b *testing.B) {
benchStream(b, 102400)
}
func createRandomDataFile(b *testing.B, length int) string {
f, err := os.Create("/tmp/test")
if err != nil {
b.Fatalf("couldn't create file: %v\n", err)
}
rand := randbo.New()
io.CopyN(f, rand, int64(length))
f.Close()
return "/tmp/test"
}
func benchFile(b *testing.B, length int) {
path := createRandomDataFile(b, length)
f, err := os.Open(path)
if err != nil {
b.Fatalf("couldn't open file: %v\n", err)
}
b.SetBytes(int64(length))
for i := 0; i < b.N; i++ {
f.Seek(0, 0)
_, err := NewBuffer(f)
if err != nil {
b.Fatalf("got error when reading: %v\n", err)
}
}
}
func BenchmarkFile_16(b *testing.B) {
benchFile(b, 16)
}
func BenchmarkFile_1024(b *testing.B) {
benchFile(b, 1024)
}
func BenchmarkFile_8192(b *testing.B) {
benchFile(b, 8192)
}
func BenchmarkFile_102400(b *testing.B) {
benchFile(b, 102400)
}