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}
|
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)
|
defer close(addrs)
|
||||||
for i := 0; i < n; i++ {
|
for {
|
||||||
p := randomPeer(b)
|
select {
|
||||||
addrs <- p
|
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)
|
defer close(addrs)
|
||||||
|
|
||||||
go addressProducer(b, producer, n)
|
go addressProducer(b, producer, done)
|
||||||
|
|
||||||
eb := int64(errBound)
|
eb := int64(errBound)
|
||||||
for {
|
for {
|
||||||
|
@ -309,39 +309,61 @@ func TestBasicPeerstore(t *testing.T) {
|
|||||||
runTestWithPeerstores(t, testBasicPeerstore)
|
runTestWithPeerstores(t, testBasicPeerstore)
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkBasicPeerstore(b *testing.B) {
|
func benchmarkPeerstore(ps Peerstore) func(*testing.B) {
|
||||||
ps := NewPeerstore()
|
return func(b *testing.B) {
|
||||||
|
addrs := make(chan *peerpair, 100)
|
||||||
|
|
||||||
addrs := make(chan *peerpair, 100)
|
go addressProducer(b, addrs, time.After(10*time.Second))
|
||||||
|
|
||||||
go addressProducer(b, addrs, 50000)
|
start := time.Now()
|
||||||
|
count := 0
|
||||||
for {
|
b.ResetTimer()
|
||||||
pp, ok := <-addrs
|
for {
|
||||||
if !ok {
|
pp, ok := <-addrs
|
||||||
break
|
if !ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
count++
|
||||||
|
pid := peer.ID(pp.ID)
|
||||||
|
ps.AddAddr(pid, pp.Addr, PermanentAddrTTL)
|
||||||
}
|
}
|
||||||
pid := peer.ID(pp.ID)
|
elapsed := time.Since(start)
|
||||||
ps.AddAddr(pid, pp.Addr, PermanentAddrTTL)
|
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) {
|
func benchmarkPeerstoreRateLimited(ps Peerstore) func(*testing.B) {
|
||||||
return func(b *testing.B) {
|
return func(b *testing.B) {
|
||||||
producer := make(chan *peerpair, 100)
|
producer := make(chan *peerpair, 100)
|
||||||
addrs := 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()
|
b.ResetTimer()
|
||||||
for {
|
for {
|
||||||
pp, ok := <-addrs
|
pp, ok := <-addrs
|
||||||
if !ok {
|
if !ok {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
count++
|
||||||
pid := peer.ID(pp.ID)
|
pid := peer.ID(pp.ID)
|
||||||
ps.AddAddr(pid, pp.Addr, PermanentAddrTTL)
|
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