mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2025-03-13 11:30:09 +08:00
Implement proper benchmark with b.N
This commit is contained in:
parent
f3e62000ee
commit
34b7dc806d
@ -1,12 +1,10 @@
|
||||
package peerstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
cr "crypto/rand"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"math/rand"
|
||||
|
||||
"github.com/mr-tron/base58/base58"
|
||||
ma "github.com/multiformats/go-multiaddr"
|
||||
@ -45,31 +43,14 @@ func randomPeer(b *testing.B) *peerpair {
|
||||
return &peerpair{b58ID, addr}
|
||||
}
|
||||
|
||||
func addressProducer(b *testing.B, addrs chan *peerpair) {
|
||||
func addressProducer(ctx context.Context, b *testing.B, addrs chan *peerpair) {
|
||||
defer close(addrs)
|
||||
for i := 0; i < b.N; i++ {
|
||||
p := randomPeer(b)
|
||||
addrs <- p
|
||||
}
|
||||
}
|
||||
|
||||
func rateLimitedAddressProducer(b *testing.B, addrs chan *peerpair, producer chan *peerpair, avgTime time.Duration, errBound time.Duration) {
|
||||
defer close(addrs)
|
||||
|
||||
go addressProducer(b, producer)
|
||||
|
||||
eb := int64(errBound)
|
||||
for {
|
||||
addr, ok := <-producer
|
||||
if !ok {
|
||||
break
|
||||
p := randomPeer(b)
|
||||
select {
|
||||
case addrs <- p:
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
addrs <- addr
|
||||
wiggle := time.Duration(0)
|
||||
if eb > 0 {
|
||||
wiggle = time.Duration(rand.Int63n(eb*2) - eb)
|
||||
}
|
||||
sleepDur := avgTime + wiggle
|
||||
time.Sleep(sleepDur)
|
||||
}
|
||||
}
|
||||
|
@ -313,17 +313,16 @@ func benchmarkPeerstore(ps Peerstore) func(*testing.B) {
|
||||
return func(b *testing.B) {
|
||||
addrs := make(chan *peerpair, 100)
|
||||
|
||||
go addressProducer(b, addrs)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
go addressProducer(ctx, b, addrs)
|
||||
|
||||
b.ResetTimer()
|
||||
for {
|
||||
pp, ok := <-addrs
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
pp := <-addrs
|
||||
pid := peer.ID(pp.ID)
|
||||
ps.AddAddr(pid, pp.Addr, PermanentAddrTTL)
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
|
||||
@ -335,31 +334,3 @@ func BenchmarkPeerstore(b *testing.B) {
|
||||
defer closer()
|
||||
b.Run("PeerstoreDatastore", benchmarkPeerstore(dsps))
|
||||
}
|
||||
|
||||
func benchmarkPeerstoreRateLimited(ps Peerstore) func(*testing.B) {
|
||||
return func(b *testing.B) {
|
||||
producer := make(chan *peerpair, 100)
|
||||
addrs := make(chan *peerpair, 100)
|
||||
|
||||
go rateLimitedAddressProducer(b, addrs, producer, 100*time.Microsecond, 100*time.Microsecond)
|
||||
|
||||
b.ResetTimer()
|
||||
for {
|
||||
pp, ok := <-addrs
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
pid := peer.ID(pp.ID)
|
||||
ps.AddAddr(pid, pp.Addr, PermanentAddrTTL)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPeerstoreRateLimited(b *testing.B) {
|
||||
ps := NewPeerstore()
|
||||
b.Run("PeerstoreBasic", benchmarkPeerstoreRateLimited(ps))
|
||||
|
||||
dsps, closer := setupDatastorePeerstore(b)
|
||||
defer closer()
|
||||
b.Run("PeerstoreDatastore", benchmarkPeerstoreRateLimited(dsps))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user