-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringbuffer_test.go
108 lines (96 loc) · 1.84 KB
/
ringbuffer_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
package ringbuffer
import (
"bufio"
"bytes"
"crypto/sha256"
"io"
"math/rand"
"testing"
)
const (
datalen = 128 << 20
bufsize = 64 << 10
)
var rb, _ = io.ReadAll(io.LimitReader(rand.New(rand.NewSource(0)), datalen))
func Test(t *testing.T) {
r := bytes.NewReader(rb)
hasher := sha256.New()
hasher.Write(rb)
sum1 := hasher.Sum(nil)
hasher.Reset()
rbuf := NewReaderSize(r, bufsize)
buf := make([]byte, bufsize)
for {
n, err := rbuf.Read(buf)
if err != nil && err != io.EOF {
t.Fatalf(`chunker error: %s`, err)
}
hasher.Write(buf[:n])
if err == io.EOF {
break
}
}
sum2 := hasher.Sum(nil)
if !bytes.Equal(sum1, sum2) {
t.Fatalf(`ringbuffer produces incorrect output`)
}
}
func Benchmark_IOReader(b *testing.B) {
r := bytes.NewReader(rb)
b.SetBytes(int64(r.Len()))
buf := make([]byte, bufsize)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for {
n, err := r.Read(buf)
if err != nil && err != io.EOF {
b.Fatalf(`io error: %s`, err)
}
_ = buf[:n]
if err == io.EOF {
break
}
}
r.Reset(rb)
}
}
func Benchmark_BufIOReader(b *testing.B) {
r := bytes.NewReader(rb)
b.SetBytes(int64(r.Len()))
buf := make([]byte, bufsize)
b.ResetTimer()
for i := 0; i < b.N; i++ {
rd := bufio.NewReaderSize(r, bufsize)
for {
n, err := rd.Read(buf)
if err != nil && err != io.EOF {
b.Fatalf(`bufio error: %s`, err)
}
_ = buf[:n]
if err == io.EOF {
break
}
}
r.Reset(rb)
}
}
func Benchmark_PlakarLabs_RingbufferReader(b *testing.B) {
r := bytes.NewReader(rb)
b.SetBytes(int64(r.Len()))
buf := make([]byte, bufsize)
b.ResetTimer()
for i := 0; i < b.N; i++ {
rd := NewReaderSize(r, bufsize)
for {
n, err := rd.Read(buf)
if err != nil && err != io.EOF {
b.Fatalf(`ringbuffer error: %s`, err)
}
_ = buf[:n]
if err == io.EOF {
break
}
}
r.Reset(rb)
}
}