-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: add benchmark for peerQueue #204
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
) | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initially I thought it's a bad func to bench but it's getting interesting. We create a new if we will change
We can get the following results:
(no idea why pushing 1M peers resulted in +15% time, yet). |
||
} | ||
}) | ||
|
||
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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think keeping
queue
inside every subtest will be better to misuse it in the future (assume moving 1 subtest before another or so).