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

Go bench test never ends #46493

Closed
rodoufu opened this issue Jun 1, 2021 · 1 comment
Closed

Go bench test never ends #46493

rodoufu opened this issue Jun 1, 2021 · 1 comment

Comments

@rodoufu
Copy link

rodoufu commented Jun 1, 2021

What version of Go are you using (go version)?

$ go version
go version go1.16.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="~/Library/Caches/go-build"
GOENV="~/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="~/go/pkg/mod"
GOOS="darwin"
GOPATH="~/go"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.2"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="~/git/positions/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/xg/xyn22y996bx5h7mbjhq1w0jm0000gn/T/go-build2191936031=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Create this bench test

import (
	"fmt"
	"github.com/pkg/errors"
)

func BenchmarkWrap(b *testing.B) {
	b.ReportAllocs()
	err := fmt.Errorf("simple error")
	for n := 0; n < b.N; n++ {
		err = errors.Wrap(err, "open failed")
	}
}

func BenchmarkNoWrap(b *testing.B) {
	b.ReportAllocs()
	err := fmt.Errorf("simple error")
	for n := 0; n < b.N; n++ {
		err = fmt.Errorf("open failed %w", err)
	}
}

func BenchmarkWrapf(b *testing.B) {
	b.ReportAllocs()
	err := fmt.Errorf("simple error")
	for n := 0; n < b.N; n++ {
		err = errors.Wrapf(err, "open failed: %v", err)
	}
}

Then try to run it like:

$ go test -bench=. -run=Bench ./...

What did you expect to see?

I would expect to see the execution time for all tests.

What did you see instead?

It is getting stuck with the BenchmarkWrapf.

$ go test -bench=. -run=Bench ./...
goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
BenchmarkWrap-8          1408294               756.3 ns/op           336 B/op          4 allocs/op
BenchmarkNoWrap-8          38275            341847 ns/op          462277 B/op          4 allocs/op
BenchmarkWrapf-8        ^Csignal: interrupt
FAIL    pkg/domain        443.597s
@ianlancetaylor
Copy link
Member

Each time through the loop the error message gets longer. The long error message can't be garbage collected, so your program will eventually start swapping. It's not hanging, it's just taking a very very long time to complete.

Try this instead:

func BenchmarkWrap(b *testing.B) {
	b.ReportAllocs()
	err := fmt.Errorf("simple error")
	for n := 0; n < b.N; n++ {
		_ = errors.Wrap(err, "open failed")
	}
}

func BenchmarkNoWrap(b *testing.B) {
	b.ReportAllocs()
	err := fmt.Errorf("simple error")
	for n := 0; n < b.N; n++ {
		_ = fmt.Errorf("open failed %w", err)
	}
}

func BenchmarkWrapf(b *testing.B) {
	b.ReportAllocs()
	err := fmt.Errorf("simple error")
	for n := 0; n < b.N; n++ {
		_ = errors.Wrapf(err, "open failed: %v", err)
	}
}

@golang golang locked and limited conversation to collaborators Jun 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants