| name | optimize-go-test-duration |
| description | Use when Go test wall time is too high, CI test duration regresses, a package needs trace-driven test performance analysis, or tests should be made safely parallel with t.Parallel, package splitting, subprocess isolation, semaphores, or Go built-in tracing. |
Optimize Go Test Duration
Core Idea
Optimize wall time from evidence, not guesses. Separate actual slow work from time spent waiting behind the Go test scheduler, package boundaries, subprocess startup, locks, I/O, sleeps, or scarce external resources.
Keep coverage and realism unless the user explicitly asks to trade them away.
Baseline
Use comparable commands before and after each change.
For package timing:
go test ./path/to/pkg/... -shuffle=on
For per-test timing:
go test ./path/to/pkg -shuffle=on -json > /tmp/pkg-tests.json
jq -r 'select(.Action=="pass" and .Test != null and .Elapsed != null) | [.Elapsed, .Test] | @tsv' /tmp/pkg-tests.json | sort -nr | head -60
Follow repository test rules first. In kenn-forge, always pass -shuffle=on for direct go test, do not add -count=1, and do not use -v unless needed for a specific failure.
Trace Workflow
Capture a Go trace for the slow package or focused group:
go test ./path/to/pkg -shuffle=on -trace /tmp/pkg.trace
Extract profiles that explain where wall time went:
go tool trace -pprof=sched /tmp/pkg.trace > /tmp/pkg-sched.pprof
go tool trace -pprof= /tmp/pkg.trace > /tmp/pkg-sync.pprof
go tool trace -pprof=syscall /tmp/pkg.trace > /tmp/pkg-syscall.pprof
go tool trace -pprof=net /tmp/pkg.trace > /tmp/pkg-net.pprof
go tool pprof -top /tmp/pkg-sched.pprof
go tool pprof -top /tmp/pkg-sync.pprof
go tool pprof -top /tmp/pkg-syscall.pprof
go tool pprof -top /tmp/pkg-net.pprof