feat: Add benchmark example (#403)

* feat: Add benchmark example

Adds a simple benchmark example and its invocation.

* fix: Address PR comments

- Clarify meaning of `b.N`.
- Nix verbose flag in run example.

* fix: Reword b.N comment

* fix: Missing period
This commit is contained in:
Johanan Idicula
2021-12-02 10:04:59 -05:00
committed by GitHub
parent 80fb5ebddf
commit dadb6bdeae
4 changed files with 61 additions and 6 deletions

View File

@@ -67,7 +67,13 @@ func TestIntMinTableDriven(t *testing.T) {
}
}
// Benchmark tests typically go in `_test.go` files and are
// named beginning with `Benchmark`. The `testing` runner
// executes each benchmark function several times, increasing
// `b.N` on each run until it collects a precise measurement.
func BenchmarkIntMin(b *testing.B) {
// Typically the benchmark runs a function we're
// benchmarking in a loop `b.N` times.
for i := 0; i < b.N; i++ {
IntMin(1, 2)
}

View File

@@ -16,3 +16,14 @@ $ go test -v
--- PASS: TestIntMinTableDriven/-1,0 (0.00s)
PASS
ok examples/testing 0.023s
# Run all benchmarks in the current project in verbose
# mode. All tests are run prior to benchmarks. The `bench`
# flag receives a regex for benchmark function names.
$ go test -bench=.
goos: darwin
goarch: arm64
pkg: examples/testing
BenchmarkIntMin-8 1000000000 0.3136 ns/op
PASS
ok examples/testing 0.351s

View File

@@ -1,2 +1,2 @@
25e8941d63b555a590e6d44a95ae0e41ecadadca
ALL2BVLkYEr
3671aaf0eee9f6d2b68e51b09997be767edfe97c
PlzU16wwEWE