Skip to content

testing: b.StopTimer breaks b.Loop #72922

Description

@dominikh

Go version

go version go1.24.1 linux/amd64

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/dominikh/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/dominikh/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1859758891=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/dominikh/prj/src/example.com/go.mod'
GOMODCACHE='/home/dominikh/prj/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/dominikh/prj'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/dominikh/prj/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/dominikh/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/dominikh/prj/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.1'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Consider the following benchmark function:

func BenchmarkFoo(b *testing.B) {
	for b.Loop() {
		println("iter")
		b.StopTimer()
		// expensive per-iteration setup work
		b.StartTimer()
		time.Sleep(100 * time.Millisecond)
	}
}

Run with go test -bench=. -benchtime=1s

b.Loop never returns false and the benchmark never terminates.

In B.stopOrScaleBLoop we use b.start to compute how long we took to iterate b.N times. However, calling B.StartTimer sets b.start to the current time.

None of the documentation says that using b.StopTimer/b.StartTimer is forbidden when using b.Loop.

Activity

  1. ianlancetaylor commented on Mar 18, 2025

    @ianlancetaylor
    Contributor
  2. added
    BugReportIssues describing a possible bug in the Go implementation.
    on Mar 18, 2025
  3. self-assigned this
    on Mar 18, 2025
  4. gopherbot commented on Mar 18, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/658975 mentions this issue: testing: allow manual timer control in testing.B.Loop

  5. aclements commented on Mar 18, 2025

    @aclements
    Member

    @gopherbot, please backport to Go 1.24.

  6. gopherbot commented on Mar 18, 2025

    @gopherbot
    Contributor

    Backport issue(s) opened: #72934 (for 1.24).

    Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://go.dev/wiki/MinorReleases.

  7. added this to the Go1.25 milestone on Mar 19, 2025
  8. added
    FixPendingIssues that have a fix which has not yet been reviewed or submitted.
    on Mar 19, 2025
  9. added a commit that references this issue on Mar 20, 2025
    b613d21
  10. locked and limited conversation to collaborators on Mar 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

BugReportIssues describing a possible bug in the Go implementation.FixPendingIssues that have a fix which has not yet been reviewed or submitted.FrozenDueToAge

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions