fix: for loop can be modernized using range over int

This commit is contained in:
613 2025-10-09 14:06:46 +08:00
commit a7d6241989
7 changed files with 12 additions and 12 deletions

View File

@ -154,7 +154,7 @@ func runRequest(B *testing.B, r *Engine, method, path string) {
w := newMockWriter()
B.ReportAllocs()
B.ResetTimer()
for range B.N {
for B.Loop() {
r.ServeHTTP(w, req)
}
}

View File

@ -20,7 +20,7 @@ func BenchmarkSliceValidationError(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for range b.N {
for b.Loop() {
if len(e.Error()) == 0 {
b.Errorf("error")
}

View File

@ -31,7 +31,7 @@ type structFull struct {
func BenchmarkMapFormFull(b *testing.B) {
var s structFull
for range b.N {
for b.Loop() {
err := mapForm(&s, form)
if err != nil {
b.Fatalf("Error on a form mapping")
@ -54,7 +54,7 @@ type structName struct {
func BenchmarkMapFormName(b *testing.B) {
var s structName
for range b.N {
for b.Loop() {
err := mapForm(&s, form)
if err != nil {
b.Fatalf("Error on a form mapping")

View File

@ -3632,7 +3632,7 @@ func BenchmarkGetMapFromFormData(b *testing.B) {
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
b.ReportAllocs()
for range b.N {
for b.Loop() {
_, _ = getMapFromFormData(bm.data, bm.key)
}
})

View File

@ -81,25 +81,25 @@ func TestStringToBytes(t *testing.T) {
// go test -v -run=none -bench=^BenchmarkBytesConv -benchmem=true
func BenchmarkBytesConvBytesToStrRaw(b *testing.B) {
for range b.N {
for b.Loop() {
rawBytesToStr(testBytes)
}
}
func BenchmarkBytesConvBytesToStr(b *testing.B) {
for range b.N {
for b.Loop() {
BytesToString(testBytes)
}
}
func BenchmarkBytesConvStrToBytesRaw(b *testing.B) {
for range b.N {
for b.Loop() {
rawStrToBytes(testString)
}
}
func BenchmarkBytesConvStrToBytes(b *testing.B) {
for range b.N {
for b.Loop() {
StringToBytes(testString)
}
}

View File

@ -94,7 +94,7 @@ func TestPathCleanMallocs(t *testing.T) {
func BenchmarkPathClean(b *testing.B) {
b.ReportAllocs()
for range b.N {
for {
for _, test := range cleanTests {
cleanPath(test.path)
}
@ -137,7 +137,7 @@ func BenchmarkPathCleanLong(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for range b.N {
for b.Loop() {
for _, test := range cleanTests {
cleanPath(test.path)
}

View File

@ -19,7 +19,7 @@ func init() {
}
func BenchmarkParseAccept(b *testing.B) {
for range b.N {
for b.Loop() {
parseAccept("text/html , application/xhtml+xml,application/xml;q=0.9, */* ;q=0.8")
}
}