[go] testing: with -benchtime=1x, run the benchmark loop exactly once

38 views
Skip to first unread message

Caleb Spare (Gerrit)

unread,
Jun 25, 2021, 4:51:33 PM6/25/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Caleb Spare has uploaded this change for review.

View Change

testing: with -benchtime=1x, run the benchmark loop exactly once

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
---
A src/cmd/go/testdata/script/test_benchmark_1x.txt
M src/testing/benchmark.go
2 files changed, 28 insertions(+), 1 deletion(-)

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
index 0000000..deeff54
--- /dev/null
+++ b/src/cmd/go/testdata/script/test_benchmark_1x.txt
@@ -0,0 +1,25 @@
+# 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 "testing"
+
+var called = false
+
+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
+}
diff --git a/src/testing/benchmark.go b/src/testing/benchmark.go
index 15b4426..01d91a4 100644
--- a/src/testing/benchmark.go
+++ b/src/testing/benchmark.go
@@ -298,7 +298,9 @@

// Run the benchmark for at least the specified amount of time.
if b.benchTime.n > 0 {
- b.runN(b.benchTime.n)
+ 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; {

To view, visit change 331089. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I76aaef2ba521ba8ad6bbde2b14977e191aada5e4
Gerrit-Change-Number: 331089
Gerrit-PatchSet: 1
Gerrit-Owner: Caleb Spare <ces...@gmail.com>
Gerrit-MessageType: newchange

Caleb Spare (Gerrit)

unread,
Jun 25, 2021, 4:52:28 PM6/25/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Patch set 1:Run-TryBot +1

View Change

1 comment:

To view, visit change 331089. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I76aaef2ba521ba8ad6bbde2b14977e191aada5e4
Gerrit-Change-Number: 331089
Gerrit-PatchSet: 1
Gerrit-Owner: Caleb Spare <ces...@gmail.com>
Gerrit-Reviewer: Caleb Spare <ces...@gmail.com>
Gerrit-Comment-Date: Fri, 25 Jun 2021 20:52:24 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

Caleb Spare (Gerrit)

unread,
Aug 28, 2021, 7:38:44 PM8/28/21
to goph...@pubsubhelper.golang.org, Ian Lance Taylor, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Ian Lance Taylor.

View Change

1 comment:

  • Patchset:

To view, visit change 331089. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I76aaef2ba521ba8ad6bbde2b14977e191aada5e4
Gerrit-Change-Number: 331089
Gerrit-PatchSet: 1
Gerrit-Owner: Caleb Spare <ces...@gmail.com>
Gerrit-Reviewer: Caleb Spare <ces...@gmail.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Comment-Date: Sat, 28 Aug 2021 23:38:39 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

Ian Lance Taylor (Gerrit)

unread,
Sep 11, 2021, 4:57:47 PM9/11/21
to Caleb Spare, goph...@pubsubhelper.golang.org, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Caleb Spare.

View Change

2 comments:

  • File src/cmd/go/testdata/script/test_benchmark_1x.txt:

    • Patch Set #1, Line 15: var called = false

      This test should ideally also test that the benchmark is in fact run once. I think it should work to have a TestMain that verifies that called is true after m.Run.

  • File src/testing/benchmark.go:

To view, visit change 331089. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I76aaef2ba521ba8ad6bbde2b14977e191aada5e4
Gerrit-Change-Number: 331089
Gerrit-PatchSet: 1
Gerrit-Owner: Caleb Spare <ces...@gmail.com>
Gerrit-Reviewer: Caleb Spare <ces...@gmail.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Caleb Spare <ces...@gmail.com>
Gerrit-Comment-Date: Sat, 11 Sep 2021 20:57:42 +0000

Caleb Spare (Gerrit)

unread,
Sep 11, 2021, 7:50:40 PM9/11/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Caleb Spare.

Caleb Spare uploaded patch set #2 to this change.

View Change

testing: with -benchtime=1x, run the benchmark loop exactly once

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
---
A src/cmd/go/testdata/script/test_benchmark_1x.txt
M src/testing/benchmark.go
2 files changed, 43 insertions(+), 1 deletion(-)

To view, visit change 331089. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I76aaef2ba521ba8ad6bbde2b14977e191aada5e4
Gerrit-Change-Number: 331089
Gerrit-PatchSet: 2
Gerrit-Owner: Caleb Spare <ces...@gmail.com>
Gerrit-Reviewer: Caleb Spare <ces...@gmail.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Caleb Spare <ces...@gmail.com>
Gerrit-MessageType: newpatchset

Caleb Spare (Gerrit)

unread,
Sep 11, 2021, 7:51:20 PM9/11/21
to goph...@pubsubhelper.golang.org, Ian Lance Taylor, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Ian Lance Taylor.

Patch set 2:Run-TryBot +1

View Change

2 comments:

  • File src/cmd/go/testdata/script/test_benchmark_1x.txt:

    • This test should ideally also test that the benchmark is in fact run once. […]

      Good point.

  • File src/testing/benchmark.go:

    • Done

To view, visit change 331089. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I76aaef2ba521ba8ad6bbde2b14977e191aada5e4
Gerrit-Change-Number: 331089
Gerrit-PatchSet: 2
Gerrit-Owner: Caleb Spare <ces...@gmail.com>
Gerrit-Reviewer: Caleb Spare <ces...@gmail.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Comment-Date: Sat, 11 Sep 2021 23:51:16 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Comment-In-Reply-To: Ian Lance Taylor <ia...@golang.org>
Gerrit-MessageType: comment

Emmanuel Odeke (Gerrit)

unread,
Sep 11, 2021, 8:33:19 PM9/11/21
to Caleb Spare, goph...@pubsubhelper.golang.org, Go Bot, Ian Lance Taylor, golang-co...@googlegroups.com

Attention is currently required from: Caleb Spare, Ian Lance Taylor.

View Change

2 comments:

  • Patchset:

    • Patch Set #2:

      Thank you for this change Caleb, thank you for the review Ian.

      Caleb, I’ve made a suggestion to just list out the cases individually to make for much easier reading, otherwise looks good; please take a look.

  • File src/testing/benchmark.go:

    • Patch Set #2, Line 300: if b.benchTime.n > 0 {

      We could list out these cases for much easier reading, so perhaps:
      if b.benchTime == 1 {
      // We already ran a single iteration
      ……
      } else if b.benchTime > 1 {
      b.run(b.benchTime.n)
      } else {
      ..

To view, visit change 331089. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I76aaef2ba521ba8ad6bbde2b14977e191aada5e4
Gerrit-Change-Number: 331089
Gerrit-PatchSet: 2
Gerrit-Owner: Caleb Spare <ces...@gmail.com>
Gerrit-Reviewer: Caleb Spare <ces...@gmail.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-CC: Emmanuel Odeke <emma...@orijtech.com>
Gerrit-Attention: Caleb Spare <ces...@gmail.com>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Comment-Date: Sun, 12 Sep 2021 00:33:15 +0000

Caleb Spare (Gerrit)

unread,
Sep 11, 2021, 8:47:53 PM9/11/21
to goph...@pubsubhelper.golang.org, Emmanuel Odeke, Go Bot, Ian Lance Taylor, golang-co...@googlegroups.com

Attention is currently required from: Ian Lance Taylor, Emmanuel Odeke.

View Change

1 comment:

  • File src/testing/benchmark.go:

    • We could list out these cases for much easier reading, so perhaps: […]

      I prefer the current structure of the code because the top-level 'if' separates the two modes of operation (iterations vs. duration).

      There is also a sort of parallelism to the code paths of each arm of that 'if': in both the 'iterations' and 'duration' mode, we only run more iterations if the run1 we already did wasn't enough.

To view, visit change 331089. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I76aaef2ba521ba8ad6bbde2b14977e191aada5e4
Gerrit-Change-Number: 331089
Gerrit-PatchSet: 2
Gerrit-Owner: Caleb Spare <ces...@gmail.com>
Gerrit-Reviewer: Caleb Spare <ces...@gmail.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-CC: Emmanuel Odeke <emma...@orijtech.com>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Emmanuel Odeke <emma...@orijtech.com>
Gerrit-Comment-Date: Sun, 12 Sep 2021 00:47:48 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Emmanuel Odeke <emma...@orijtech.com>
Gerrit-MessageType: comment

Caleb Spare (Gerrit)

unread,
Oct 6, 2021, 4:27:20 PM10/6/21
to goph...@pubsubhelper.golang.org, Emmanuel Odeke, Go Bot, Ian Lance Taylor, golang-co...@googlegroups.com

Attention is currently required from: Ian Lance Taylor, Emmanuel Odeke.

Patch set 2:Trust +1

View Change

1 comment:

  • File src/testing/benchmark.go:

    • I prefer the current structure of the code because the top-level 'if' separates the two modes of ope […]

      Emmanuel -- ping.

To view, visit change 331089. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I76aaef2ba521ba8ad6bbde2b14977e191aada5e4
Gerrit-Change-Number: 331089
Gerrit-PatchSet: 2
Gerrit-Owner: Caleb Spare <ces...@gmail.com>
Gerrit-Reviewer: Caleb Spare <ces...@gmail.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-CC: Emmanuel Odeke <emma...@orijtech.com>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Emmanuel Odeke <emma...@orijtech.com>
Gerrit-Comment-Date: Wed, 06 Oct 2021 20:27:15 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Comment-In-Reply-To: Caleb Spare <ces...@gmail.com>

Emmanuel Odeke (Gerrit)

unread,
Oct 6, 2021, 4:30:06 PM10/6/21
to Caleb Spare, goph...@pubsubhelper.golang.org, Go Bot, Ian Lance Taylor, golang-co...@googlegroups.com

Attention is currently required from: Caleb Spare, Ian Lance Taylor.

Patch set 2:Code-Review +2

View Change

2 comments:

  • Patchset:

    • Patch Set #2:

      LGTM, thank you Caleb. Please rebase from master and let’s rerun the trybots then submit.

  • File src/testing/benchmark.go:

    • Emmanuel -- ping.

      No biggie then, thanks.

To view, visit change 331089. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I76aaef2ba521ba8ad6bbde2b14977e191aada5e4
Gerrit-Change-Number: 331089
Gerrit-PatchSet: 2
Gerrit-Owner: Caleb Spare <ces...@gmail.com>
Gerrit-Reviewer: Caleb Spare <ces...@gmail.com>
Gerrit-Reviewer: Emmanuel Odeke <emma...@orijtech.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Caleb Spare <ces...@gmail.com>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Comment-Date: Wed, 06 Oct 2021 20:30:00 +0000

Caleb Spare (Gerrit)

unread,
Oct 7, 2021, 5:06:38 PM10/7/21
to goph...@pubsubhelper.golang.org, Emmanuel Odeke, Go Bot, Ian Lance Taylor, golang-co...@googlegroups.com

Attention is currently required from: Ian Lance Taylor.

Patch set 3:Run-TryBot +1

View Change

    To view, visit change 331089. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I76aaef2ba521ba8ad6bbde2b14977e191aada5e4
    Gerrit-Change-Number: 331089
    Gerrit-PatchSet: 3
    Gerrit-Owner: Caleb Spare <ces...@gmail.com>
    Gerrit-Reviewer: Caleb Spare <ces...@gmail.com>
    Gerrit-Reviewer: Emmanuel Odeke <emma...@orijtech.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Comment-Date: Thu, 07 Oct 2021 21:06:34 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Emmanuel Odeke (Gerrit)

    unread,
    Oct 7, 2021, 5:46:40 PM10/7/21
    to Caleb Spare, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Go Bot, Ian Lance Taylor, golang-co...@googlegroups.com

    Emmanuel Odeke submitted this change.

    View Change



    2 is the latest approved patch-set.
    No files were changed between the latest approved patch-set and the submitted one.

    Approvals: Emmanuel Odeke: Looks good to me, approved Caleb Spare: Trusted; Run TryBots Go Bot: TryBots succeeded
    testing: with -benchtime=1x, run the benchmark loop exactly once

    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 <ces...@gmail.com>
    Run-TryBot: Caleb Spare <ces...@gmail.com>
    TryBot-Result: Go Bot <go...@golang.org>
    Reviewed-by: Emmanuel Odeke <emma...@orijtech.com>
    ---
    M src/testing/benchmark.go
    A src/cmd/go/testdata/script/test_benchmark_1x.txt
    2 files changed, 63 insertions(+), 1 deletion(-)

    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
    index 0000000..b1d4c39
    --- /dev/null
    +++ b/src/cmd/go/testdata/script/test_benchmark_1x.txt
    @@ -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
    +}
    diff --git a/src/testing/benchmark.go b/src/testing/benchmark.go
    index 30fa106..1ce637e 100644
    --- a/src/testing/benchmark.go
    +++ b/src/testing/benchmark.go
    @@ -299,7 +299,12 @@


    // 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; {

    To view, visit change 331089. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I76aaef2ba521ba8ad6bbde2b14977e191aada5e4
    Gerrit-Change-Number: 331089
    Gerrit-PatchSet: 4
    Gerrit-Owner: Caleb Spare <ces...@gmail.com>
    Gerrit-Reviewer: Caleb Spare <ces...@gmail.com>
    Gerrit-Reviewer: Emmanuel Odeke <emma...@orijtech.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-MessageType: merged
    Reply all
    Reply to author
    Forward
    0 new messages