refactor: use b.Loop() to simplify the code and improve performance (#4432)

Signed-off-by: efcking <efcking@outlook.com>
This commit is contained in:
efcking 2025-11-15 19:22:18 +08:00 committed by GitHub
parent fb27ef26c2
commit a85ef5ce4d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 7 additions and 8 deletions

View File

@ -18,9 +18,8 @@ func BenchmarkSliceValidationError(b *testing.B) {
} }
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ { for b.Loop() {
if len(e.Error()) == 0 { if len(e.Error()) == 0 {
b.Errorf("error") b.Errorf("error")
} }

View File

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

View File

@ -94,7 +94,7 @@ func TestPathCleanMallocs(t *testing.T) {
func BenchmarkPathClean(b *testing.B) { func BenchmarkPathClean(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for b.Loop() {
for _, test := range cleanTests { for _, test := range cleanTests {
cleanPath(test.path) cleanPath(test.path)
} }
@ -134,10 +134,10 @@ func TestPathCleanLong(t *testing.T) {
func BenchmarkPathCleanLong(b *testing.B) { func BenchmarkPathCleanLong(b *testing.B) {
cleanTests := genLongPaths() cleanTests := genLongPaths()
b.ResetTimer()
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for b.Loop() {
for _, test := range cleanTests { for _, test := range cleanTests {
cleanPath(test.path) cleanPath(test.path)
} }

View File

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