Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing: strange benchmark results with RunParallel and -benchtime #72079

Open
RaduBerinde opened this issue Mar 3, 2025 · 4 comments
Open
Labels
BugReport Issues describing a possible bug in the Go implementation. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@RaduBerinde
Copy link
Contributor

Go version

go version go1.23.6 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/radu/Library/Caches/go-build'
GOENV='/Users/radu/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/radu/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/radu/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/radu/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.23.6.darwin-arm64'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/radu/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.23.6.darwin-arm64/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.6'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/radu/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/radu/go/src/github.com/cockroachdb/pebble/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/x0/5yrxw_4s271g705vjscld23c0000gq/T/go-build4216578675=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

func BenchmarkFoo(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		n := 0
		for pb.Next() {
			n++
		}
		fmt.Fprintf(io.Discard, " %d\n", n)
	})
}

What did you see happen?

go test -bench BenchmarkFoo -run - ./internal/cache --count 1
goos: darwin
goarch: arm64
pkg: github.com/cockroachdb/pebble/internal/cache
cpu: Apple M1 Pro
BenchmarkFoo-10         1000000000               0.08839 ns/op
PASS
ok      github.com/cockroachdb/pebble/internal/cache    0.351s


go test -bench BenchmarkFoo -run - ./internal/cache --count 1 --benchtime=100000000x
goos: darwin
goarch: arm64
pkg: github.com/cockroachdb/pebble/internal/cache
cpu: Apple M1 Pro
BenchmarkFoo-10         100000000               73.14 ns/op
PASS
ok      github.com/cockroachdb/pebble/internal/cache    7.552s

What did you expect to see?

I expected the second variant to yield a similar result. 73ns per operation cannot be right.

@RaduBerinde
Copy link
Contributor Author

@nvanbenschoten points out that the grain calculation is suspicious for this case:

go/src/testing/benchmark.go

Lines 867 to 881 in 0312e31

// Calculate grain size as number of iterations that take ~100µs.
// 100µs is enough to amortize the overhead and provide sufficient
// dynamic load balancing.
grain := uint64(0)
if b.previousN > 0 && b.previousDuration > 0 {
grain = 1e5 * uint64(b.previousN) / uint64(b.previousDuration)
}
if grain < 1 {
grain = 1
}
// We expect the inner loop and function call to take at least 10ns,
// so do not do more than 100µs/10ns=1e4 iterations.
if grain > 1e4 {
grain = 1e4
}

@RaduBerinde
Copy link
Contributor Author

Perhaps setting a lower bound like b.N / numProcs / 100 for the grain size would be sufficient.

@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label Mar 3, 2025
@JunyangShao
Copy link
Contributor

JunyangShao commented Mar 4, 2025

@alandonovan @neild

@JunyangShao JunyangShao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants