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

Commit 9a794d0

Browse files
authored
Merge pull request #15 from ipfs/feat/benchmarks
Add benchmarks
2 parents f9e6481 + 52ca04e commit 9a794d0

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ require (
77
github.com/libp2p/go-buffer-pool v0.0.1
88
github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f
99
)
10+
11+
go 1.12

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+
}

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)