Skip to content

Commit

Permalink
Merge pull request ipfs/go-ipfs-chunker#15 from ipfs/feat/benchmarks
Browse files Browse the repository at this point in the history
Add benchmarks

This commit was moved from ipfs/go-ipfs-chunker@9a794d0
  • Loading branch information
Kubuxu authored Oct 6, 2019
2 parents 5ec56f4 + 8b25c43 commit 0e9f3c9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
29 changes: 29 additions & 0 deletions chunker/rabin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,32 @@ func TestRabinChunkReuse(t *testing.T) {
t.Log("too many spare chunks made")
}
}

var Res uint64

func BenchmarkRabin(b *testing.B) {
data := make([]byte, 16<<20)
util.NewTimeSeededRand().Read(data)

b.SetBytes(16 << 20)
b.ReportAllocs()
b.ResetTimer()

var res uint64

for i := 0; i < b.N; i++ {
r := NewRabin(bytes.NewReader(data), 1024*256)

for {
chunk, err := r.NextBytes()
if err != nil {
if err == io.EOF {
break
}
b.Fatal(err)
}
res = res + uint64(len(chunk))
}
}
Res = Res + res
}
30 changes: 30 additions & 0 deletions chunker/splitting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"

u "github.com/ipfs/go-ipfs-util"
util "github.com/ipfs/go-ipfs-util"
pool "github.com/libp2p/go-buffer-pool"
)

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

return s.r.Read(buf)
}

func BenchmarkDefault(b *testing.B) {
data := make([]byte, 16<<20)
util.NewTimeSeededRand().Read(data)

b.SetBytes(16 << 20)
b.ReportAllocs()
b.ResetTimer()

var res uint64

for i := 0; i < b.N; i++ {
r := DefaultSplitter(bytes.NewReader(data))

for {
chunk, err := r.NextBytes()
if err != nil {
if err == io.EOF {
break
}
b.Fatal(err)
}
res = res + uint64(len(chunk))
pool.Put(chunk)
}
}
Res = Res + res
}

0 comments on commit 0e9f3c9

Please sign in to comment.