mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2025-03-24 13:10:06 +08:00
Use b.N in benchmarks
This commit is contained in:
parent
996387510d
commit
f3e62000ee
@ -45,23 +45,18 @@ func randomPeer(b *testing.B) *peerpair {
|
||||
return &peerpair{b58ID, addr}
|
||||
}
|
||||
|
||||
func addressProducer(b *testing.B, addrs chan *peerpair, done <-chan time.Time) {
|
||||
func addressProducer(b *testing.B, addrs chan *peerpair) {
|
||||
defer close(addrs)
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
return
|
||||
default:
|
||||
p := randomPeer(b)
|
||||
addrs <- p
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
p := randomPeer(b)
|
||||
addrs <- p
|
||||
}
|
||||
}
|
||||
|
||||
func rateLimitedAddressProducer(b *testing.B, addrs chan *peerpair, producer chan *peerpair, done <-chan time.Time, avgTime time.Duration, errBound time.Duration) {
|
||||
func rateLimitedAddressProducer(b *testing.B, addrs chan *peerpair, producer chan *peerpair, avgTime time.Duration, errBound time.Duration) {
|
||||
defer close(addrs)
|
||||
|
||||
go addressProducer(b, producer, done)
|
||||
go addressProducer(b, producer)
|
||||
|
||||
eb := int64(errBound)
|
||||
for {
|
||||
|
@ -313,23 +313,17 @@ func benchmarkPeerstore(ps Peerstore) func(*testing.B) {
|
||||
return func(b *testing.B) {
|
||||
addrs := make(chan *peerpair, 100)
|
||||
|
||||
go addressProducer(b, addrs, time.After(10*time.Second))
|
||||
go addressProducer(b, addrs)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -347,27 +341,21 @@ func benchmarkPeerstoreRateLimited(ps Peerstore) func(*testing.B) {
|
||||
producer := make(chan *peerpair, 100)
|
||||
addrs := make(chan *peerpair, 100)
|
||||
|
||||
go rateLimitedAddressProducer(b, addrs, producer, time.After(10*time.Second), 100*time.Microsecond, 100*time.Microsecond)
|
||||
go rateLimitedAddressProducer(b, addrs, producer, 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)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkBasicPeerstoreRateLimited(b *testing.B) {
|
||||
func BenchmarkPeerstoreRateLimited(b *testing.B) {
|
||||
ps := NewPeerstore()
|
||||
b.Run("PeerstoreBasic", benchmarkPeerstoreRateLimited(ps))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user