]> Cypherpunks repositories - gostls13.git/commitdiff
testing: with -benchtime=1x, run the benchmark loop exactly once
authorCaleb Spare <cespare@gmail.com>
Fri, 25 Jun 2021 20:40:30 +0000 (13:40 -0700)
committerEmmanuel Odeke <emmanuel@orijtech.com>
Thu, 7 Oct 2021 21:46:33 +0000 (21:46 +0000)
Like with -benchtime=1ns, if we find that the "discovery" round (run1)
has already crossed the -benchtime threshold, we skip running more
iterations.

Fixes #32051

Change-Id: I76aaef2ba521ba8ad6bbde2b14977e191aada5e4
Reviewed-on: https://go-review.googlesource.com/c/go/+/331089
Trust: Caleb Spare <cespare@gmail.com>
Run-TryBot: Caleb Spare <cespare@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
src/cmd/go/testdata/script/test_benchmark_1x.txt [new file with mode: 0644]
src/testing/benchmark.go

diff --git a/src/cmd/go/testdata/script/test_benchmark_1x.txt b/src/cmd/go/testdata/script/test_benchmark_1x.txt
new file mode 100644 (file)
index 0000000..b1d4c39
--- /dev/null
@@ -0,0 +1,37 @@
+# Test that -benchtime 1x only runs a total of 1 loop iteration.
+# See golang.org/issue/32051.
+
+go test -run ^$ -bench . -benchtime 1x
+
+-- go.mod --
+module bench
+
+go 1.16
+-- x_test.go --
+package bench
+
+import (
+       "fmt"
+       "os"
+       "testing"
+)
+
+var called = false
+
+func TestMain(m *testing.M) {
+       m.Run()
+       if !called {
+               fmt.Println("benchmark never called")
+               os.Exit(1)
+       }
+}
+
+func Benchmark(b *testing.B) {
+       if b.N > 1 {
+               b.Fatalf("called with b.N=%d; want b.N=1 only", b.N)
+       }
+       if called {
+               b.Fatal("called twice")
+       }
+       called = true
+}
index 30fa106dd47dd0a87afee89054b6b25e2db4a89d..1ce637e137860da09c7a6f44defa7b4f45111705 100644 (file)
@@ -299,7 +299,12 @@ func (b *B) launch() {
 
        // Run the benchmark for at least the specified amount of time.
        if b.benchTime.n > 0 {
-               b.runN(b.benchTime.n)
+               // We already ran a single iteration in run1.
+               // If -benchtime=1x was requested, use that result.
+               // See https://golang.org/issue/32051.
+               if b.benchTime.n > 1 {
+                       b.runN(b.benchTime.n)
+               }
        } else {
                d := b.benchTime.d
                for n := int64(1); !b.failed && b.duration < d && n < 1e9; {