-
Notifications
You must be signed in to change notification settings - Fork 34.2k
Description
I discussed this with @connor4312 on #122208 (comment), but now I have a concrete need.
Go has benchmarks. Benchmarks (in Go) are defined and run in much the same way that tests are. Tests are defined with func TestXxx(*testing.T)
and benchmarks are defined with func BenchmarkXxx(*testing.B)
; tests are run with go test -run='^TestXxx$'
and benchmarks are run with go test -bench='^BenchmarkXxx$'
. So it is natural for benchmarks to be exposed along side tests in the testing API.
Given a benchmark:
func BenchmarkFoo(b *testing.B) {
for i := 0; i < b.N; i++ {
Foo("bar")
}
}
Running this benchmark will produce something like the following:
goos: linux
goarch: amd64
pkg: example.com/foo
cpu: Intel(R) Core(TM) i9-10850K CPU @ 3.60GHz
BenchmarkFoo
BenchmarkFoo-20 123456 123.4 ns/op 123 B/op 12 allocs/op
PASS
ok example.com/foo 6.313s
For running benchmarks with the testing API to be meaningful, the user needs to see the results of the benchmark (the second BenchmarkFoo line). Previously, I could attach an info message to a test that passed. That capability was removed in the finalized testing API.