From cd78345010dd145d466cba3a3ad224a373e22b86 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Sat, 5 Oct 2019 21:50:29 +0200 Subject: [PATCH 1/2] Add benchmarks License: MIT Signed-off-by: Jakub Sztandera --- go.mod | 2 ++ rabin_test.go | 29 +++++++++++++++++++++++++++++ splitting_test.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/go.mod b/go.mod index 0610d88..a1f4483 100644 --- a/go.mod +++ b/go.mod @@ -7,3 +7,5 @@ require ( github.com/libp2p/go-buffer-pool v0.0.1 github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f ) + +go 1.12 diff --git a/rabin_test.go b/rabin_test.go index f267db4..140c0c4 100644 --- a/rabin_test.go +++ b/rabin_test.go @@ -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 +} diff --git a/splitting_test.go b/splitting_test.go index 3153427..a05504b 100644 --- a/splitting_test.go +++ b/splitting_test.go @@ -6,6 +6,7 @@ import ( "testing" u "github.com/ipfs/go-ipfs-util" + util "github.com/ipfs/go-ipfs-util" ) func randBuf(t *testing.T, size int) []byte { @@ -118,3 +119,30 @@ 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)) + } + } + Res = Res + res +} From 52ca04ea25ced5b8caba1755f1555c25e5082ff1 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Sat, 5 Oct 2019 21:53:36 +0200 Subject: [PATCH 2/2] Fix pool usage in benchmark License: MIT Signed-off-by: Jakub Sztandera --- splitting_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/splitting_test.go b/splitting_test.go index a05504b..27afe59 100644 --- a/splitting_test.go +++ b/splitting_test.go @@ -7,6 +7,7 @@ import ( 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 { @@ -142,6 +143,7 @@ func BenchmarkDefault(b *testing.B) { b.Fatal(err) } res = res + uint64(len(chunk)) + pool.Put(chunk) } } Res = Res + res