Skip to content

Commit

Permalink
test: add benchmark for peerQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Jun 25, 2024
1 parent 5872766 commit a5ee5ec
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions p2p/peer_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package p2p
import (
"container/heap"
"context"
"fmt"
"math/rand"
"testing"
"time"

"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -110,3 +113,58 @@ func Test_StatDecreaseScore(t *testing.T) {
pStats.decreaseScore()
require.Equal(t, pStats.score(), float32(80.0))
}

// BenchmarkPeerQueue/push_for_10-14 2373187 489.3 ns/op 448 B/op 8 allocs/op
// BenchmarkPeerQueue/pop_for_10-14 1561532 761.8 ns/op 800 B/op 10 allocs/op
// BenchmarkPeerQueue/push_for_100-14 253057 4590 ns/op 2368 B/op 11 allocs/op
// BenchmarkPeerQueue/pop_for_100-14 151729 7503 ns/op 8000 B/op 100 allocs/op
// BenchmarkPeerQueue/push_for_1000-14 25915 46220 ns/op 17728 B/op 14 allocs/op
// BenchmarkPeerQueue/pop_for_1000-14 15207 75764 ns/op 80000 B/op 1000 allocs/op
// BenchmarkPeerQueue/push_for_1000000-14 15 69555594 ns/op 44948820 B/op 41 allocs/op
// BenchmarkPeerQueue/pop_for_1000000-14 15 75170956 ns/op 80000032 B/op 1000000 allocs/op
func BenchmarkPeerQueue(b *testing.B) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

peers := [][]*peerStat{
generatePeerStats(b, 10),
generatePeerStats(b, 100),
generatePeerStats(b, 1000),
generatePeerStats(b, 1000000),
}

for _, peerStats := range peers {
var queue *peerQueue
b.Run(fmt.Sprintf("push for %d", len(peerStats)), func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
queue = newPeerQueue(ctx, peerStats)
}
})

b.Run(fmt.Sprintf("pop for %d", len(peerStats)), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for range peerStats {
_ = queue.waitPop(ctx)
}
}
})
}
}

func generatePeerStats(b *testing.B, number int) []*peerStat {
stats := make([]*peerStat, number)
for i := range stats {
priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, 256)
require.NoError(b, err)
id, err := peer.IDFromPrivateKey(priv)
require.NoError(b, err)
stats[i] = &peerStat{
peerID: id,
peerScore: rand.Float32(),
}
}
return stats
}

0 comments on commit a5ee5ec

Please sign in to comment.