From dadb6bdeae7630909d3f16a1029ca695861e5875 Mon Sep 17 00:00:00 2001
From: Johanan Idicula
Date: Thu, 2 Dec 2021 10:04:59 -0500
Subject: [PATCH] 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
---
examples/testing/main_test.go | 6 +++++
examples/testing/main_test.sh | 11 +++++++++
examples/testing/testing.hash | 4 +--
public/testing | 46 ++++++++++++++++++++++++++++++++---
4 files changed, 61 insertions(+), 6 deletions(-)
diff --git a/examples/testing/main_test.go b/examples/testing/main_test.go
index 25531da..86111d4 100644
--- a/examples/testing/main_test.go
+++ b/examples/testing/main_test.go
@@ -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)
}
diff --git a/examples/testing/main_test.sh b/examples/testing/main_test.sh
index 58e0615..b04fdb2 100644
--- a/examples/testing/main_test.sh
+++ b/examples/testing/main_test.sh
@@ -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
diff --git a/examples/testing/testing.hash b/examples/testing/testing.hash
index 4572b51..7ecf04f 100644
--- a/examples/testing/testing.hash
+++ b/examples/testing/testing.hash
@@ -1,2 +1,2 @@
-25e8941d63b555a590e6d44a95ae0e41ecadadca
-ALL2BVLkYEr
+3671aaf0eee9f6d2b68e51b09997be767edfe97c
+PlzU16wwEWE
diff --git a/public/testing b/public/testing
index afb9fa4..530d0b2 100644
--- a/public/testing
+++ b/public/testing
@@ -47,7 +47,7 @@ typically lives in the same package as the code it tests.
- 
+ 
package main
@@ -184,11 +184,29 @@ when executing go test -v .
|
+ 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.
+
|
- func BenchmarkIntMin(b *testing.B) {
+
for i := 0; i < b.N; i++ {
IntMin(1, 2)
}
@@ -206,7 +224,7 @@ when executing go test -v .
Run all tests in the current project in verbose mode.
|
-
+ |
$ go test -v
@@ -229,6 +247,26 @@ when executing go test -v .
|
+
+
+ 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
+ |
+
+
@@ -244,7 +282,7 @@ when executing go test -v
.