Skip to content

refactor(benchmark): use b.Loop() to simplify the code#4432

Merged
appleboy merged 1 commit into
gin-gonic:masterfrom
efcking:master
Nov 15, 2025
Merged

refactor(benchmark): use b.Loop() to simplify the code#4432
appleboy merged 1 commit into
gin-gonic:masterfrom
efcking:master

Conversation

@efcking
Copy link
Copy Markdown
Contributor

@efcking efcking commented Nov 11, 2025

Pull Request Checklist

Please ensure your pull request meets the following requirements:

  • Open your pull request against the master branch.
  • All tests pass in available continuous integration systems (e.g., GitHub Actions).
  • Tests are added or modified as needed to cover code changes.
  • If the pull request introduces a new feature, the feature is documented in the docs/doc.md.

replace for i := range b.N or for range b.N in a benchmark with for b.Loop(), and remove any preceding calls to b.StopTimer, b.StartTimer, and b.ResetTimer.

Supported by Go Team, more info: https://go.dev/blog/testing-b-loop

Before:

goos: darwin
goarch: arm64
pkg: github.com/gin-gonic/gin/binding
cpu: Apple M4
BenchmarkSliceValidationError-10    	  627850	      1907 ns/op	    1912 B/op	       8 allocs/op
BenchmarkMapFormFull-10             	 1385667	       873.1 ns/op
BenchmarkMapFormName-10             	10485808	       108.4 ns/op
PASS
ok  	github.com/gin-gonic/gin/binding	5.891s

After:

go test -run=^$ -bench=. ./binding           
goos: darwin
goarch: arm64
pkg: github.com/gin-gonic/gin/binding
cpu: Apple M4
BenchmarkSliceValidationError-10    	  607466	      1958 ns/op	    1912 B/op	       8 allocs/op
BenchmarkMapFormFull-10             	 1359453	       881.2 ns/op
BenchmarkMapFormName-10             	10900568	       110.4 ns/op
PASS
ok  	github.com/gin-gonic/gin/binding	4.242s

@appleboy appleboy added this to the v1.12 milestone Nov 15, 2025
@appleboy appleboy changed the title refactor: use b.Loop() to simplify the code and improve performance refactor(benchmark): use b.Loop() to simplify the code Nov 15, 2025
@codecov
Copy link
Copy Markdown

codecov Bot commented Nov 15, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.96%. Comparing base (3dc1cd6) to head (c147e8d).
⚠️ Report is 207 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4432      +/-   ##
==========================================
- Coverage   99.21%   98.96%   -0.25%     
==========================================
  Files          42       44       +2     
  Lines        3182     2903     -279     
==========================================
- Hits         3157     2873     -284     
- Misses         17       19       +2     
- Partials        8       11       +3     
Flag Coverage Δ
?
--ldflags="-checklinkname=0" -tags sonic 98.95% <ø> (?)
-tags go_json 98.88% <ø> (?)
-tags nomsgpack 98.95% <ø> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.24 98.96% <ø> (?)
go-1.25 98.96% <ø> (?)
macos-latest 98.96% <ø> (-0.25%) ⬇️
ubuntu-latest 98.96% <ø> (-0.25%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@appleboy appleboy merged commit a85ef5c into gin-gonic:master Nov 15, 2025
26 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants