mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2025-03-13 11:30:09 +08:00
Run benchmarks 10s, report rates
This commit is contained in:
parent
1d5ab0b868
commit
996387510d
@ -45,18 +45,23 @@ func randomPeer(b *testing.B) *peerpair {
|
||||
return &peerpair{b58ID, addr}
|
||||
}
|
||||
|
||||
func addressProducer(b *testing.B, addrs chan *peerpair, n int) {
|
||||
func addressProducer(b *testing.B, addrs chan *peerpair, done <-chan time.Time) {
|
||||
defer close(addrs)
|
||||
for i := 0; i < n; i++ {
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
return
|
||||
default:
|
||||
p := randomPeer(b)
|
||||
addrs <- p
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func rateLimitedAddressProducer(b *testing.B, addrs chan *peerpair, producer chan *peerpair, n int, avgTime time.Duration, errBound time.Duration) {
|
||||
func rateLimitedAddressProducer(b *testing.B, addrs chan *peerpair, producer chan *peerpair, done <-chan time.Time, avgTime time.Duration, errBound time.Duration) {
|
||||
defer close(addrs)
|
||||
|
||||
go addressProducer(b, producer, n)
|
||||
go addressProducer(b, producer, done)
|
||||
|
||||
eb := int64(errBound)
|
||||
for {
|
||||
|
@ -309,21 +309,37 @@ func TestBasicPeerstore(t *testing.T) {
|
||||
runTestWithPeerstores(t, testBasicPeerstore)
|
||||
}
|
||||
|
||||
func BenchmarkBasicPeerstore(b *testing.B) {
|
||||
ps := NewPeerstore()
|
||||
|
||||
func benchmarkPeerstore(ps Peerstore) func(*testing.B) {
|
||||
return func(b *testing.B) {
|
||||
addrs := make(chan *peerpair, 100)
|
||||
|
||||
go addressProducer(b, addrs, 50000)
|
||||
go addressProducer(b, addrs, time.After(10*time.Second))
|
||||
|
||||
start := time.Now()
|
||||
count := 0
|
||||
b.ResetTimer()
|
||||
for {
|
||||
pp, ok := <-addrs
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
count++
|
||||
pid := peer.ID(pp.ID)
|
||||
ps.AddAddr(pid, pp.Addr, PermanentAddrTTL)
|
||||
}
|
||||
elapsed := time.Since(start)
|
||||
rate := float64(count) / elapsed.Seconds()
|
||||
b.Logf("Added %d addresses in %s, rate: %f addrs/s\n", count, elapsed, rate)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPeerstore(b *testing.B) {
|
||||
ps := NewPeerstore()
|
||||
b.Run("PeerstoreBasic", benchmarkPeerstore(ps))
|
||||
|
||||
dsps, closer := setupDatastorePeerstore(b)
|
||||
defer closer()
|
||||
b.Run("PeerstoreDatastore", benchmarkPeerstore(dsps))
|
||||
}
|
||||
|
||||
func benchmarkPeerstoreRateLimited(ps Peerstore) func(*testing.B) {
|
||||
@ -331,17 +347,23 @@ func benchmarkPeerstoreRateLimited(ps Peerstore) func(*testing.B) {
|
||||
producer := make(chan *peerpair, 100)
|
||||
addrs := make(chan *peerpair, 100)
|
||||
|
||||
go rateLimitedAddressProducer(b, addrs, producer, 60000, 100*time.Microsecond, 100*time.Microsecond)
|
||||
go rateLimitedAddressProducer(b, addrs, producer, time.After(10*time.Second), 100*time.Microsecond, 100*time.Microsecond)
|
||||
|
||||
start := time.Now()
|
||||
count := 0
|
||||
b.ResetTimer()
|
||||
for {
|
||||
pp, ok := <-addrs
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
count++
|
||||
pid := peer.ID(pp.ID)
|
||||
ps.AddAddr(pid, pp.Addr, PermanentAddrTTL)
|
||||
}
|
||||
elapsed := time.Since(start)
|
||||
rate := float64(count) / elapsed.Seconds()
|
||||
b.Logf("Added %d addresses in %s, rate: %f addrs/s\n", count, elapsed, rate)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user