forked from TRON-US/go-btfs-chunker
-
Notifications
You must be signed in to change notification settings - Fork 5
/
benchmark_test.go
59 lines (49 loc) · 910 Bytes
/
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
package chunk
import (
"bytes"
"io"
"math/rand"
"testing"
)
type newSplitter func(io.Reader) Splitter
type bencSpec struct {
size int
name string
}
var bSizes = []bencSpec{
{1 << 10, "1K"},
{1 << 20, "1M"},
{16 << 20, "16M"},
{100 << 20, "100M"},
}
func benchmarkChunker(b *testing.B, ns newSplitter) {
for _, s := range bSizes {
s := s
b.Run(s.name, func(b *testing.B) {
benchmarkChunkerSize(b, ns, s.size)
})
}
}
func benchmarkChunkerSize(b *testing.B, ns newSplitter, size int) {
rng := rand.New(rand.NewSource(1))
data := make([]byte, size)
rng.Read(data)
b.SetBytes(int64(size))
b.ReportAllocs()
b.ResetTimer()
var res uint64
for i := 0; i < b.N; i++ {
r := ns(bytes.NewReader(data))
for {
chunk, err := r.NextBytes()
if err != nil {
if err == io.EOF {
break
}
b.Fatal(err)
}
res = res + uint64(len(chunk))
}
}
Res = Res + res
}