-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbenchmark_test.go
79 lines (71 loc) · 1.94 KB
/
benchmark_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
package gobls
import (
"bufio"
"bytes"
"fmt"
"testing"
)
func benchmarkScanner(b *testing.B, lineLength int, makeScanner func(buf []byte) Scanner) {
b.Helper()
const lineCount = 100
wanted := makeBuffer(1, lineLength)
wanted = wanted[:len(wanted)-2] // trim final CRLF
buf := makeBuffer(lineCount, lineLength)
b.ResetTimer()
for i := 0; i < b.N; i++ {
var count int
s := makeScanner(buf)
for s.Scan() {
if got := s.Bytes(); !bytes.Equal(got, wanted) {
b.Errorf("GOT: %#v; WANT: %#v", got, wanted)
}
count++
}
if got, want := s.Err(), error(nil); got != want {
b.Fatalf("GOT: %#v; WANT: %#v", got, want)
}
if got, want := count, lineCount; got != want {
b.Fatalf("GOT: %#v; WANT: %#v", got, want)
}
}
}
func BenchmarkScanner(b *testing.B) {
for _, i := range []uint{6, 7, 8, 9, 10, 11, 12} {
lineLength := 1 << i
b.Run(fmt.Sprintf("%04d", lineLength), func(b *testing.B) {
b.Run("bufio", func(b *testing.B) {
benchmarkScanner(b, lineLength, func(buf []byte) Scanner {
return bufio.NewScanner(bytes.NewReader(buf))
})
})
b.Run("scanner", func(b *testing.B) {
benchmarkScanner(b, lineLength, func(buf []byte) Scanner {
return NewScanner(bytes.NewReader(buf))
})
})
b.Run("buffer", func(b *testing.B) {
benchmarkScanner(b, lineLength, func(buf []byte) Scanner {
return NewBufferScanner(buf)
})
})
})
}
b.Run("excessively long", func(b *testing.B) {
const lineLength = excessivelyLongLineLength
b.Run("bufio", func(b *testing.B) {
benchmarkScanner(b, lineLength, func(buf []byte) Scanner {
return bufio.NewScanner(bytes.NewReader(buf))
})
})
b.Run("scanner", func(b *testing.B) {
benchmarkScanner(b, lineLength, func(buf []byte) Scanner {
return NewScanner(bytes.NewReader(buf))
})
})
b.Run("buffer", func(b *testing.B) {
benchmarkScanner(b, lineLength, func(buf []byte) Scanner {
return NewBufferScanner(buf)
})
})
})
}