use testing.B.Loop in all benchmark tests (#5285)

go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -category=bloop -fix -test ./...
was used as a starting point.
This commit is contained in:
Marten Seemann
2025-08-16 12:44:56 +02:00
committed by GitHub
parent 66e5c5ebaa
commit 8c062ae604
13 changed files with 108 additions and 86 deletions

View File

@@ -239,6 +239,8 @@ func TestLogsRetryPacketsWithToken(t *testing.T) {
}
func BenchmarkParseExtendedHeader(b *testing.B) {
b.ReportAllocs()
data, err := (&ExtendedHeader{
Header: Header{
Type: protocol.PacketTypeHandshake,
@@ -255,9 +257,7 @@ func BenchmarkParseExtendedHeader(b *testing.B) {
}
data = append(data, make([]byte, 1231)...)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
hdr, _, _, err := ParsePacket(data)
if err != nil {
b.Fatal(err)

View File

@@ -818,15 +818,13 @@ func BenchmarkParseDatagramFrame(b *testing.B) {
}
func benchmarkFrames(b *testing.B, frames ...Frame) {
buf := writeFrames(b, frames...)
b.ReportAllocs()
buf := writeFrames(b, frames...)
parser := NewFrameParser(true, true, true)
parser.SetAckDelayExponent(3)
b.ResetTimer()
b.ReportAllocs()
for range b.N {
for b.Loop() {
parseFrames(b, parser, buf, frames...)
}
}

View File

@@ -452,14 +452,15 @@ func BenchmarkIs0RTTPacket(b *testing.B) {
src := mrand.NewChaCha8([32]byte{'f', 'o', 'o', 'b', 'a', 'r'})
random := mrand.New(src)
packets := make([][]byte, 1024)
for i := 0; i < len(packets); i++ {
for i := range len(packets) {
packets[i] = make([]byte, random.IntN(256))
src.Read(packets[i])
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
var i int
for b.Loop() {
Is0RTTPacket(packets[i%len(packets)])
i++
}
}
@@ -475,6 +476,8 @@ func BenchmarkParseInitial(b *testing.B) {
}
func benchmarkInitialPacketParsing(b *testing.B, token []byte) {
b.ReportAllocs()
hdr := Header{
Type: protocol.PacketTypeInitial,
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
@@ -493,9 +496,7 @@ func benchmarkInitialPacketParsing(b *testing.B, token []byte) {
}
data = append(data, make([]byte, 1000)...)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
h, _, _, err := ParsePacket(data)
if err != nil {
b.Fatal(err)
@@ -508,6 +509,8 @@ func benchmarkInitialPacketParsing(b *testing.B, token []byte) {
}
func BenchmarkParseRetry(b *testing.B) {
b.ReportAllocs()
token := make([]byte, 64)
rand.Read(token)
hdr := &ExtendedHeader{
@@ -524,9 +527,7 @@ func BenchmarkParseRetry(b *testing.B) {
b.Fatal(err)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
h, _, _, err := ParsePacket(data)
if err != nil {
b.Fatal(err)
@@ -555,9 +556,8 @@ func benchmarkArbitraryHeaderParsing(b *testing.B, destLen, srcLen int) {
buf = append(buf, uint8(srcLen))
buf = append(buf, srcConnID...)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
parsed, d, s, err := ParseArbitraryLenConnectionIDs(buf)
if err != nil {
b.Fatal(err)

View File

@@ -94,7 +94,8 @@ func BenchmarkWriteShortHeader(b *testing.B) {
b.ReportAllocs()
buf := make([]byte, 100)
connID := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6})
for i := 0; i < b.N; i++ {
for b.Loop() {
var err error
buf, err = AppendShortHeader(buf, connID, 1337, protocol.PacketNumberLen4, protocol.KeyPhaseOne)
if err != nil {

View File

@@ -878,6 +878,8 @@ func BenchmarkTransportParameters(b *testing.B) {
}
func benchmarkTransportParameters(b *testing.B, withPreferredAddress bool) {
b.ReportAllocs()
var token protocol.StatelessResetToken
rand.Read(token[:])
rcid := protocol.ParseConnectionID([]byte{0xde, 0xad, 0xc0, 0xde})
@@ -915,10 +917,8 @@ func benchmarkTransportParameters(b *testing.B, withPreferredAddress bool) {
}
data := params.Marshal(protocol.PerspectiveServer)
b.ResetTimer()
b.ReportAllocs()
var p TransportParameters
for i := 0; i < b.N; i++ {
for b.Loop() {
if err := p.Unmarshal(data, protocol.PerspectiveServer); err != nil {
b.Fatal(err)
}

View File

@@ -92,10 +92,12 @@ versionLoop:
func BenchmarkComposeVersionNegotiationPacket(b *testing.B) {
b.ReportAllocs()
supportedVersions := []protocol.Version{protocol.Version2, protocol.Version1, 0x1337}
destConnID := protocol.ArbitraryLenConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0xa, 0xb, 0xc, 0xd}
srcConnID := protocol.ArbitraryLenConnectionID{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
for i := 0; i < b.N; i++ {
for b.Loop() {
ComposeVersionNegotiation(destConnID, srcConnID, supportedVersions)
}
}