Skip to content

Commit

Permalink
Merge pull request #43 from lanzafame/feat/more-benchs
Browse files Browse the repository at this point in the history
Deterministic benchmark order; Keybook interface benchmarks
  • Loading branch information
raulk authored Nov 14, 2018
2 parents 0596636 + 56ce954 commit 835ae99
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 16 deletions.
38 changes: 23 additions & 15 deletions p2p/host/peerstore/pstoreds/ds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}
}
Expand All @@ -65,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
Expand All @@ -75,6 +81,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")
})
}
Expand Down
6 changes: 6 additions & 0 deletions p2p/host/peerstore/pstoremem/inmem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
11 changes: 10 additions & 1 deletion p2p/host/peerstore/test/benchmarks_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test
import (
"context"
"fmt"
"sort"
"testing"

pstore "github.com/libp2p/go-libp2p-peerstore"
Expand Down Expand Up @@ -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()
Expand Down
141 changes: 141 additions & 0 deletions p2p/host/peerstore/test/keybook_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}

0 comments on commit 835ae99

Please sign in to comment.