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

@@ -65,8 +65,8 @@ func benchmarkHKDFExpandLabel(b *testing.B, cipherSuite uint16, useStdLib bool)
cs := cipherSuiteTLS13ByID(cipherSuite)
secret := make([]byte, 32)
rand.Read(secret)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
if useStdLib {
nextTrafficSecret(cs, secret)
} else {

View File

@@ -246,7 +246,8 @@ func TestInitialAEADEncryptsAndDecryptsHeader(t *testing.T) {
func BenchmarkInitialAEADCreate(b *testing.B) {
b.ReportAllocs()
connID := protocol.ParseConnectionID([]byte{0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef})
for i := 0; i < b.N; i++ {
for b.Loop() {
NewInitialAEAD(connID, protocol.PerspectiveServer, protocol.Version1)
}
}
@@ -282,7 +283,8 @@ func BenchmarkInitialAEAD(b *testing.B) {
func benchmarkOpen(b *testing.B, aead LongHeaderOpener, msg, hdr []byte) {
b.ReportAllocs()
dst := make([]byte, 0, 1500)
for i := 0; i < b.N; i++ {
for b.Loop() {
dst = dst[:0]
if _, err := aead.Open(dst, msg, 42, hdr); err != nil {
b.Fatalf("opening failed: %s", err)
@@ -293,8 +295,11 @@ func benchmarkOpen(b *testing.B, aead LongHeaderOpener, msg, hdr []byte) {
func benchmarkSeal(b *testing.B, aead LongHeaderSealer, msg, hdr []byte) {
b.ReportAllocs()
dst := make([]byte, 0, 1500)
for i := 0; i < b.N; i++ {
var pn protocol.PacketNumber
for b.Loop() {
dst = dst[:0]
aead.Seal(dst, msg, protocol.PacketNumber(i), hdr)
aead.Seal(dst, msg, pn, hdr)
pn++
}
}

View File

@@ -603,8 +603,10 @@ func BenchmarkPacketEncryption(b *testing.B) {
ad := make([]byte, 32)
rand.Read(ad)
for i := 0; i < b.N; i++ {
src = client.Seal(src[:0], src[:l], protocol.PacketNumber(i), ad)
var pn protocol.PacketNumber
for b.Loop() {
src = client.Seal(src[:0], src[:l], pn, ad)
pn++
}
}
@@ -618,7 +620,7 @@ func BenchmarkPacketDecryption(b *testing.B) {
rand.Read(ad)
src = client.Seal(src[:0], src[:l], 1337, ad)
for i := 0; i < b.N; i++ {
for b.Loop() {
if _, err := server.Open(dst[:0], src, time.Time{}, 1337, protocol.KeyPhaseZero, ad); err != nil {
b.Fatalf("opening failed: %v", err)
}
@@ -627,7 +629,8 @@ func BenchmarkPacketDecryption(b *testing.B) {
func BenchmarkRollKeys(b *testing.B) {
client, _ := getClientAndServer()
for i := 0; i < b.N; i++ {
for b.Loop() {
client.rollKeys()
}
if int(client.keyPhase) != b.N {