p2p: using testing.B.Loop (#32664)

This commit is contained in:
cui 2025-09-20 06:38:36 +08:00 committed by GitHub
parent 1601f398d4
commit 64c6de7747
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 5 additions and 8 deletions

View file

@ -477,10 +477,9 @@ func BenchmarkV5_DecodeHandshakePingSecp256k1(b *testing.B) {
b.Fatal("can't encode handshake packet") b.Fatal("can't encode handshake packet")
} }
challenge.Node = nil // force ENR signature verification in decoder challenge.Node = nil // force ENR signature verification in decoder
b.ResetTimer()
input := make([]byte, len(enc)) input := make([]byte, len(enc))
for i := 0; i < b.N; i++ { for b.Loop() {
copy(input, enc) copy(input, enc)
net.nodeB.c.sc.storeSentHandshake(idA, "", challenge) net.nodeB.c.sc.storeSentHandshake(idA, "", challenge)
_, _, _, err := net.nodeB.c.Decode(input, "") _, _, _, err := net.nodeB.c.Decode(input, "")
@ -507,10 +506,9 @@ func BenchmarkV5_DecodePing(b *testing.B) {
if err != nil { if err != nil {
b.Fatalf("can't encode: %v", err) b.Fatalf("can't encode: %v", err)
} }
b.ResetTimer()
input := make([]byte, len(enc)) input := make([]byte, len(enc))
for i := 0; i < b.N; i++ { for b.Loop() {
copy(input, enc) copy(input, enc)
_, _, packet, _ := net.nodeB.c.Decode(input, addrB) _, _, packet, _ := net.nodeB.c.Decode(input, addrB)
if _, ok := packet.(*Ping); !ok { if _, ok := packet.(*Ping); !ok {

View file

@ -187,7 +187,7 @@ func TestCheckRelayIP(t *testing.T) {
func BenchmarkCheckRelayIP(b *testing.B) { func BenchmarkCheckRelayIP(b *testing.B) {
sender := parseIP("23.55.1.242") sender := parseIP("23.55.1.242")
addr := parseIP("23.55.1.2") addr := parseIP("23.55.1.2")
for i := 0; i < b.N; i++ { for b.Loop() {
CheckRelayIP(sender, addr) CheckRelayIP(sender, addr)
} }
} }

View file

@ -369,8 +369,7 @@ func TestHandshakeForwardCompatibility(t *testing.T) {
func BenchmarkHandshakeRead(b *testing.B) { func BenchmarkHandshakeRead(b *testing.B) {
var input = unhex(eip8HandshakeAuthTests[0].input) var input = unhex(eip8HandshakeAuthTests[0].input)
for b.Loop() {
for i := 0; i < b.N; i++ {
var ( var (
h handshakeState h handshakeState
r = bytes.NewReader(input) r = bytes.NewReader(input)
@ -427,7 +426,7 @@ func BenchmarkThroughput(b *testing.B) {
// Read N messages. // Read N messages.
b.SetBytes(int64(len(msgdata))) b.SetBytes(int64(len(msgdata)))
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for b.Loop() {
_, _, _, err := conn2.Read() _, _, _, err := conn2.Read()
if err != nil { if err != nil {
b.Fatal("read error:", err) b.Fatal("read error:", err)