Skip to content
This repository was archived by the owner on Jun 20, 2023. It is now read-only.

Commit 25cb45d

Browse files
committed
Improve benchmarks
License: MIT Signed-off-by: Jakub Sztandera <[email protected]>
1 parent b1c398a commit 25cb45d

File tree

5 files changed

+77
-86
lines changed

5 files changed

+77
-86
lines changed

benchmark_test.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package chunk
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"math/rand"
7+
"testing"
8+
)
9+
10+
type newSplitter func(io.Reader) Splitter
11+
12+
type bencSpec struct {
13+
size int
14+
name string
15+
}
16+
17+
var bSizes = []bencSpec{
18+
{1 << 10, "1K"},
19+
{1 << 20, "1M"},
20+
{16 << 20, "16M"},
21+
{100 << 20, "100M"},
22+
}
23+
24+
func benchmarkChunker(b *testing.B, ns newSplitter) {
25+
for _, s := range bSizes {
26+
s := s
27+
b.Run(s.name, func(b *testing.B) {
28+
benchmarkChunkerSize(b, ns, s.size)
29+
})
30+
}
31+
}
32+
33+
func benchmarkChunkerSize(b *testing.B, ns newSplitter, size int) {
34+
rng := rand.New(rand.NewSource(1))
35+
data := make([]byte, size)
36+
rng.Read(data)
37+
38+
b.SetBytes(int64(size))
39+
b.ReportAllocs()
40+
b.ResetTimer()
41+
42+
var res uint64
43+
44+
for i := 0; i < b.N; i++ {
45+
r := ns(bytes.NewReader(data))
46+
47+
for {
48+
chunk, err := r.NextBytes()
49+
if err != nil {
50+
if err == io.EOF {
51+
break
52+
}
53+
b.Fatal(err)
54+
}
55+
res = res + uint64(len(chunk))
56+
}
57+
}
58+
Res = Res + res
59+
}

buzhash.go

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ func NewBuzhash(r io.Reader) *Buzhash {
2828
}
2929
}
3030

31+
func (b *Buzhash) Reader() io.Reader {
32+
return b.r
33+
}
34+
3135
func (b *Buzhash) NextBytes() ([]byte, error) {
3236
if b.err != nil {
3337
return nil, b.err

buzhash_test.go

+5-26
Original file line numberDiff line numberDiff line change
@@ -41,37 +41,16 @@ func TestBuzhashChunking(t *testing.T) {
4141
}
4242

4343
func TestBuzhashChunkReuse(t *testing.T) {
44-
newBuzhash := func(r io.Reader) cher {
44+
newBuzhash := func(r io.Reader) Splitter {
4545
return NewBuzhash(r)
4646
}
4747
testReuse(t, newBuzhash)
4848
}
4949

50-
func BenchmarkBuzhash(b *testing.B) {
51-
data := make([]byte, 1<<10)
52-
util.NewTimeSeededRand().Read(data)
53-
54-
b.SetBytes(int64(len(data)))
55-
b.ReportAllocs()
56-
b.ResetTimer()
57-
58-
var res uint64
59-
60-
for i := 0; i < b.N; i++ {
61-
r := NewBuzhash(bytes.NewReader(data))
62-
63-
for {
64-
chunk, err := r.NextBytes()
65-
if err != nil {
66-
if err == io.EOF {
67-
break
68-
}
69-
b.Fatal(err)
70-
}
71-
res = res + uint64(len(chunk))
72-
}
73-
}
74-
Res = Res + res
50+
func BenchmarkBuzhash2(b *testing.B) {
51+
benchmarkChunker(b, func(r io.Reader) Splitter {
52+
return NewBuzhash(r)
53+
})
7554
}
7655

7756
func TestBuzhashBitsHash(t *testing.T) {

rabin_test.go

+6-34
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,7 @@ func TestRabinChunking(t *testing.T) {
3939
}
4040
}
4141

42-
type cher interface {
43-
NextBytes() ([]byte, error)
44-
}
45-
46-
type newChunker func(io.Reader) cher
47-
48-
func chunkData(t *testing.T, newC newChunker, data []byte) map[string]blocks.Block {
42+
func chunkData(t *testing.T, newC newSplitter, data []byte) map[string]blocks.Block {
4943
r := newC(bytes.NewReader(data))
5044

5145
blkmap := make(map[string]blocks.Block)
@@ -66,7 +60,7 @@ func chunkData(t *testing.T, newC newChunker, data []byte) map[string]blocks.Blo
6660
return blkmap
6761
}
6862

69-
func testReuse(t *testing.T, cr newChunker) {
63+
func testReuse(t *testing.T, cr newSplitter) {
7064
data := make([]byte, 1024*1024*16)
7165
util.NewTimeSeededRand().Read(data)
7266

@@ -87,7 +81,7 @@ func testReuse(t *testing.T, cr newChunker) {
8781
}
8882

8983
func TestRabinChunkReuse(t *testing.T) {
90-
newRabin := func(r io.Reader) cher {
84+
newRabin := func(r io.Reader) Splitter {
9185
return NewRabin(r, 256*1024)
9286
}
9387
testReuse(t, newRabin)
@@ -96,29 +90,7 @@ func TestRabinChunkReuse(t *testing.T) {
9690
var Res uint64
9791

9892
func BenchmarkRabin(b *testing.B) {
99-
const size = 1 << 10
100-
data := make([]byte, size)
101-
util.NewTimeSeededRand().Read(data)
102-
103-
b.SetBytes(size)
104-
b.ReportAllocs()
105-
b.ResetTimer()
106-
107-
var res uint64
108-
109-
for i := 0; i < b.N; i++ {
110-
r := NewRabin(bytes.NewReader(data), 1024*256)
111-
112-
for {
113-
chunk, err := r.NextBytes()
114-
if err != nil {
115-
if err == io.EOF {
116-
break
117-
}
118-
b.Fatal(err)
119-
}
120-
res = res + uint64(len(chunk))
121-
}
122-
}
123-
Res = Res + res
93+
benchmarkChunker(b, func(r io.Reader) Splitter {
94+
return NewRabin(r, 256<<10)
95+
})
12496
}

splitting_test.go

+3-26
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"testing"
77

88
u "github.com/ipfs/go-ipfs-util"
9-
util "github.com/ipfs/go-ipfs-util"
109
)
1110

1211
func randBuf(t *testing.T, size int) []byte {
@@ -121,29 +120,7 @@ func (s *clipReader) Read(buf []byte) (int, error) {
121120
}
122121

123122
func BenchmarkDefault(b *testing.B) {
124-
const size = 1 << 10
125-
data := make([]byte, size)
126-
util.NewTimeSeededRand().Read(data)
127-
128-
b.SetBytes(size)
129-
b.ReportAllocs()
130-
b.ResetTimer()
131-
132-
var res uint64
133-
134-
for i := 0; i < b.N; i++ {
135-
r := DefaultSplitter(bytes.NewReader(data))
136-
137-
for {
138-
chunk, err := r.NextBytes()
139-
if err != nil {
140-
if err == io.EOF {
141-
break
142-
}
143-
b.Fatal(err)
144-
}
145-
res = res + uint64(len(chunk))
146-
}
147-
}
148-
Res = Res + res
123+
benchmarkChunker(b, func(r io.Reader) Splitter {
124+
return DefaultSplitter(r)
125+
})
149126
}

0 commit comments

Comments
 (0)