From ee43bc1951c835471e3bb2d5c5a9f19b3cbff8cb Mon Sep 17 00:00:00 2001 From: Adrian Lanzafame Date: Mon, 29 Oct 2018 20:14:51 +1000 Subject: [PATCH 1/3] prevent timeout by un-nesting tb.Runs License: MIT Signed-off-by: Adrian Lanzafame --- pstoreds/ds_test.go | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/pstoreds/ds_test.go b/pstoreds/ds_test.go index 29ac9eb..1557c65 100644 --- a/pstoreds/ds_test.go +++ b/pstoreds/ds_test.go @@ -33,26 +33,24 @@ func TestDsPeerstore(t *testing.T) { func TestDsAddrBook(t *testing.T) { for name, dsFactory := range dstores { - t.Run(name, func(t *testing.T) { - t.Run("Cacheful", func(t *testing.T) { - t.Parallel() + t.Run(name+" Cacheful", func(t *testing.T) { + t.Parallel() - opts := DefaultOpts() - opts.TTLInterval = 100 * time.Microsecond - opts.CacheSize = 1024 + opts := DefaultOpts() + opts.TTLInterval = 100 * time.Microsecond + opts.CacheSize = 1024 - pt.TestAddrBook(t, addressBookFactory(t, dsFactory, opts)) - }) + pt.TestAddrBook(t, addressBookFactory(t, dsFactory, opts)) + }) - t.Run("Cacheless", func(t *testing.T) { - t.Parallel() + t.Run(name+" Cacheless", func(t *testing.T) { + t.Parallel() - opts := DefaultOpts() - opts.TTLInterval = 100 * time.Microsecond - opts.CacheSize = 0 + opts := DefaultOpts() + opts.TTLInterval = 100 * time.Microsecond + opts.CacheSize = 0 - pt.TestAddrBook(t, addressBookFactory(t, dsFactory, opts)) - }) + pt.TestAddrBook(t, addressBookFactory(t, dsFactory, opts)) }) } } @@ -75,6 +73,8 @@ func BenchmarkDsPeerstore(b *testing.B) { for name, dsFactory := range dstores { b.Run(name, func(b *testing.B) { pt.BenchmarkPeerstore(b, peerstoreFactory(b, dsFactory, caching), "Caching") + }) + b.Run(name, func(b *testing.B) { pt.BenchmarkPeerstore(b, peerstoreFactory(b, dsFactory, cacheless), "Cacheless") }) } From 308d998d76ac482a7e0693538ef4edba8f0767d0 Mon Sep 17 00:00:00 2001 From: Adrian Lanzafame Date: Mon, 29 Oct 2018 20:16:51 +1000 Subject: [PATCH 2/3] sort suite so benchmarks always run in the same order License: MIT Signed-off-by: Adrian Lanzafame --- test/benchmarks_suite.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/benchmarks_suite.go b/test/benchmarks_suite.go index 69fde2d..f283b9b 100644 --- a/test/benchmarks_suite.go +++ b/test/benchmarks_suite.go @@ -3,6 +3,7 @@ package test import ( "context" "fmt" + "sort" "testing" pstore "github.com/libp2p/go-libp2p-peerstore" @@ -38,7 +39,15 @@ func BenchmarkPeerstore(b *testing.B, factory PeerstoreFactory, variant string) go addressProducer(ctx, b, p.ch, p.n) } - for name, bench := range peerstoreBenchmarks { + // So tests are always run in the same order. + ordernames := make([]string, 0, len(peerstoreBenchmarks)) + for name := range peerstoreBenchmarks { + ordernames = append(ordernames, name) + } + sort.Strings(ordernames) + + for _, name := range ordernames { + bench := peerstoreBenchmarks[name] for _, p := range params { // Create a new peerstore. ps, closeFunc := factory() From c04ff2d27b30c1df932e069db58d9aa505390a6d Mon Sep 17 00:00:00 2001 From: Adrian Lanzafame Date: Mon, 29 Oct 2018 20:17:10 +1000 Subject: [PATCH 3/3] add keybook benchmarks License: MIT Signed-off-by: Adrian Lanzafame --- pstoreds/ds_test.go | 8 +++ pstoremem/inmem_test.go | 6 ++ test/keybook_suite.go | 141 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+) diff --git a/pstoreds/ds_test.go b/pstoreds/ds_test.go index 1557c65..c98d4a6 100644 --- a/pstoreds/ds_test.go +++ b/pstoreds/ds_test.go @@ -63,6 +63,14 @@ func TestDsKeyBook(t *testing.T) { } } +func BenchmarkDsKeyBook(b *testing.B) { + for name, dsFactory := range dstores { + b.Run(name, func(b *testing.B) { + pt.BenchmarkKeyBook(b, keyBookFactory(b, dsFactory, DefaultOpts())) + }) + } +} + func BenchmarkDsPeerstore(b *testing.B) { caching := DefaultOpts() caching.CacheSize = 1024 diff --git a/pstoremem/inmem_test.go b/pstoremem/inmem_test.go index 510399d..97e24e4 100644 --- a/pstoremem/inmem_test.go +++ b/pstoremem/inmem_test.go @@ -30,3 +30,9 @@ func BenchmarkInMemoryPeerstore(b *testing.B) { return NewPeerstore(), nil }, "InMem") } + +func BenchmarkInMemoryKeyBook(b *testing.B) { + pt.BenchmarkKeyBook(b, func() (pstore.KeyBook, func()) { + return NewKeyBook(), nil + }) +} diff --git a/test/keybook_suite.go b/test/keybook_suite.go index e713f0e..f4f8496 100644 --- a/test/keybook_suite.go +++ b/test/keybook_suite.go @@ -161,3 +161,144 @@ func testInlinedPubKeyAddedOnRetrieve(kb pstore.KeyBook) func(t *testing.T) { } } } + +var keybookBenchmarkSuite = map[string]func(kb pstore.KeyBook) func(*testing.B){ + "PubKey": benchmarkPubKey, + "AddPubKey": benchmarkAddPubKey, + "PrivKey": benchmarkPrivKey, + "AddPrivKey": benchmarkAddPrivKey, + "PeersWithKeys": benchmarkPeersWithKeys, +} + +func BenchmarkKeyBook(b *testing.B, factory KeyBookFactory) { + ordernames := make([]string, 0, len(keybookBenchmarkSuite)) + for name := range keybookBenchmarkSuite { + ordernames = append(ordernames, name) + } + sort.Strings(ordernames) + for _, name := range ordernames { + bench := keybookBenchmarkSuite[name] + kb, closeFunc := factory() + + b.Run(name, bench(kb)) + + if closeFunc != nil { + closeFunc() + } + } +} + +func benchmarkPubKey(kb pstore.KeyBook) func(*testing.B) { + return func(b *testing.B) { + _, pub, err := pt.RandTestKeyPair(512) + if err != nil { + b.Error(err) + } + + id, err := peer.IDFromPublicKey(pub) + if err != nil { + b.Error(err) + } + + err = kb.AddPubKey(id, pub) + if err != nil { + b.Fatal(err) + } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + kb.PubKey(id) + } + } +} + +func benchmarkAddPubKey(kb pstore.KeyBook) func(*testing.B) { + return func(b *testing.B) { + _, pub, err := pt.RandTestKeyPair(512) + if err != nil { + b.Error(err) + } + + id, err := peer.IDFromPublicKey(pub) + if err != nil { + b.Error(err) + } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + kb.AddPubKey(id, pub) + } + } +} + +func benchmarkPrivKey(kb pstore.KeyBook) func(*testing.B) { + return func(b *testing.B) { + priv, _, err := pt.RandTestKeyPair(512) + if err != nil { + b.Error(err) + } + + id, err := peer.IDFromPrivateKey(priv) + if err != nil { + b.Error(err) + } + + err = kb.AddPrivKey(id, priv) + if err != nil { + b.Fatal(err) + } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + kb.PrivKey(id) + } + } +} + +func benchmarkAddPrivKey(kb pstore.KeyBook) func(*testing.B) { + return func(b *testing.B) { + priv, _, err := pt.RandTestKeyPair(512) + if err != nil { + b.Error(err) + } + + id, err := peer.IDFromPrivateKey(priv) + if err != nil { + b.Error(err) + } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + kb.AddPrivKey(id, priv) + } + } +} + +func benchmarkPeersWithKeys(kb pstore.KeyBook) func(*testing.B) { + return func(b *testing.B) { + for i := 0; i < 10; i++ { + priv, pub, err := pt.RandTestKeyPair(512) + if err != nil { + b.Error(err) + } + + id, err := peer.IDFromPublicKey(pub) + if err != nil { + b.Error(err) + } + + err = kb.AddPubKey(id, pub) + if err != nil { + b.Fatal(err) + } + err = kb.AddPrivKey(id, priv) + if err != nil { + b.Fatal(err) + } + } + b.ResetTimer() + for i := 0; i < b.N; i++ { + kb.PeersWithKeys() + } + } +}