From 80a69a89743c10e66746cc4e863769b556d24fe5 Mon Sep 17 00:00:00 2001 From: jidicula Date: Mon, 29 Nov 2021 15:02:30 -0500 Subject: [PATCH] feat: Add benchmark example Adds a simple benchmark example and its invocation. --- examples/testing/main_test.go | 3 +++ examples/testing/main_test.sh | 26 +++++++++++++++++++ examples/testing/testing.hash | 4 +-- public/testing | 49 +++++++++++++++++++++++++++++++---- 4 files changed, 75 insertions(+), 7 deletions(-) diff --git a/examples/testing/main_test.go b/examples/testing/main_test.go index 25531da..85fc243 100644 --- a/examples/testing/main_test.go +++ b/examples/testing/main_test.go @@ -67,6 +67,9 @@ func TestIntMinTableDriven(t *testing.T) { } } +// Benchmark tests typically go in `_test.go` files and begin with `Benchmark`. +// Each benchmark function is run several times, with `b.N` increasing on each +// run until the `testing` runner deems the benchmark to be accurate. func BenchmarkIntMin(b *testing.B) { 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..b6ae704 100644 --- a/examples/testing/main_test.sh +++ b/examples/testing/main_test.sh @@ -16,3 +16,29 @@ $ 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 -v -bench=. +=== RUN TestIntMinBasic +--- PASS: TestIntMinBasic (0.00s) +=== RUN TestIntMinTableDriven +=== RUN TestIntMinTableDriven/0,1 +=== RUN TestIntMinTableDriven/1,0 +=== RUN TestIntMinTableDriven/2,-2 +=== RUN TestIntMinTableDriven/0,-1 +=== RUN TestIntMinTableDriven/-1,0 +--- PASS: TestIntMinTableDriven (0.00s) + --- PASS: TestIntMinTableDriven/0,1 (0.00s) + --- PASS: TestIntMinTableDriven/1,0 (0.00s) + --- PASS: TestIntMinTableDriven/2,-2 (0.00s) + --- PASS: TestIntMinTableDriven/0,-1 (0.00s) + --- PASS: TestIntMinTableDriven/-1,0 (0.00s) +goos: darwin +goarch: arm64 +pkg: examples/testing +BenchmarkIntMin +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..c3d896f 100644 --- a/examples/testing/testing.hash +++ b/examples/testing/testing.hash @@ -1,2 +1,2 @@ -25e8941d63b555a590e6d44a95ae0e41ecadadca -ALL2BVLkYEr +c3489e12554d4b865ae225bc8bad2d9e634fe429 +Ct1lKa6FttL diff --git a/public/testing b/public/testing index afb9fa4..7ce6b1b 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,15 @@ when executing go test -v.

- +

Benchmark tests typically go in _test.go files and begin with Benchmark. +Each benchmark function is run several times, with b.N increasing on each +run until the testing runner deems the benchmark to be accurate.

+ -
func BenchmarkIntMin(b *testing.B) {
+          
+func BenchmarkIntMin(b *testing.B) {
     for i := 0; i < b.N; i++ {
         IntMin(1, 2)
     }
@@ -206,7 +210,7 @@ when executing go test -v.

Run all tests in the current project in verbose mode.

- +
 $ go test -v
@@ -229,6 +233,41 @@ 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 -v -bench=.
+=== RUN   TestIntMinBasic
+--- PASS: TestIntMinBasic (0.00s)
+=== RUN   TestIntMinTableDriven
+=== RUN   TestIntMinTableDriven/0,1
+=== RUN   TestIntMinTableDriven/1,0
+=== RUN   TestIntMinTableDriven/2,-2
+=== RUN   TestIntMinTableDriven/0,-1
+=== RUN   TestIntMinTableDriven/-1,0
+--- PASS: TestIntMinTableDriven (0.00s)
+    --- PASS: TestIntMinTableDriven/0,1 (0.00s)
+    --- PASS: TestIntMinTableDriven/1,0 (0.00s)
+    --- PASS: TestIntMinTableDriven/2,-2 (0.00s)
+    --- PASS: TestIntMinTableDriven/0,-1 (0.00s)
+    --- PASS: TestIntMinTableDriven/-1,0 (0.00s)
+goos: darwin
+goarch: arm64
+pkg: examples/testing
+BenchmarkIntMin
+BenchmarkIntMin-8 1000000000 0.3136 ns/op
+PASS
+ok      examples/testing    0.351s
+ + + @@ -244,7 +283,7 @@ when executing go test -v.