-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbench_test.go
55 lines (46 loc) · 917 Bytes
/
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
package stream
import (
"bytes"
"io"
"io/ioutil"
"math/rand"
"sync"
"testing"
)
const testDataSize = 10 * 1024 * 1024
func BenchmarkInMemoryStream(b *testing.B) {
benchmarkStream(NewMemFS(), b)
}
func BenchmarkOnDiskStream(b *testing.B) {
benchmarkStream(StdFileSystem, b)
}
func benchmarkStream(fs FileSystem, b *testing.B) {
b.ReportAllocs()
// load random bytes
rnd := io.LimitReader(rand.New(rand.NewSource(0)), testDataSize)
buf := bytes.NewBuffer(nil)
io.Copy(buf, rnd)
// allocate stream
w, err := NewStream("hello", fs)
if err != nil {
b.Fatal(err)
}
defer w.Remove()
// test parallel writer/reader speed
var once sync.Once
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
// fill writer
once.Do(func() {
go func() {
io.Copy(w, buf)
w.Close()
}()
})
// read all
r, _ := w.NextReader()
io.Copy(ioutil.Discard, r)
r.Close()
}
})
}