Skip to content

Commit 0e9f3c9

Browse files
authored
Merge pull request ipfs/go-ipfs-chunker#15 from ipfs/feat/benchmarks
Add benchmarks This commit was moved from ipfs/go-ipfs-chunker@9a794d0
2 parents 5ec56f4 + 8b25c43 commit 0e9f3c9

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

chunker/rabin_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,32 @@ func TestRabinChunkReuse(t *testing.T) {
7979
t.Log("too many spare chunks made")
8080
}
8181
}
82+
83+
var Res uint64
84+
85+
func BenchmarkRabin(b *testing.B) {
86+
data := make([]byte, 16<<20)
87+
util.NewTimeSeededRand().Read(data)
88+
89+
b.SetBytes(16 << 20)
90+
b.ReportAllocs()
91+
b.ResetTimer()
92+
93+
var res uint64
94+
95+
for i := 0; i < b.N; i++ {
96+
r := NewRabin(bytes.NewReader(data), 1024*256)
97+
98+
for {
99+
chunk, err := r.NextBytes()
100+
if err != nil {
101+
if err == io.EOF {
102+
break
103+
}
104+
b.Fatal(err)
105+
}
106+
res = res + uint64(len(chunk))
107+
}
108+
}
109+
Res = Res + res
110+
}

chunker/splitting_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"testing"
77

88
u "github.com/ipfs/go-ipfs-util"
9+
util "github.com/ipfs/go-ipfs-util"
10+
pool "github.com/libp2p/go-buffer-pool"
911
)
1012

1113
func randBuf(t *testing.T, size int) []byte {
@@ -118,3 +120,31 @@ func (s *clipReader) Read(buf []byte) (int, error) {
118120

119121
return s.r.Read(buf)
120122
}
123+
124+
func BenchmarkDefault(b *testing.B) {
125+
data := make([]byte, 16<<20)
126+
util.NewTimeSeededRand().Read(data)
127+
128+
b.SetBytes(16 << 20)
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+
pool.Put(chunk)
147+
}
148+
}
149+
Res = Res + res
150+
}

0 commit comments

Comments
 (0)