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: Wrong number of allocations #54041

Closed
illiafox opened this issue Jul 25, 2022 · 2 comments
Closed

testing: Wrong number of allocations #54041

illiafox opened this issue Jul 25, 2022 · 2 comments
Labels
WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@illiafox
Copy link

illiafox commented Jul 25, 2022

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

$ go version
go version go1.18.3 linux/amd64

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/illia/.cache/go-build"
GOENV="/home/illia/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/illia/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/illia/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.3"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2942373244=/tmp/go-build -gno-record-gcc-switches"

What did you do?

func ToBytes(s string) []byte {
	ptr := unsafe.Pointer(&s)
	slice := *(*[]byte)(ptr)
	return slice[:(*reflect.SliceHeader)(ptr).Len]
}

func BenchmarkToBytes(b *testing.B) {
	for i := 0; i < b.N; i++ {
		ToBytes("Hello, world!")
	}
}

func main() {
	alloc := testing.AllocsPerRun(1, func() {
		ToBytes("Hello, world!")
	})
	fmt.Println("Total allocations:", alloc)
}
go run .
go test -bench=BenchmarkToBytes -count=1 

What did you expect to see?

Same numbers of the allocations

What did you see instead?

$ go run .
Total allocations: 0
$ go test -bench=BenchmarkToBytes -count=1
goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i5-2540M CPU @ 2.60GHz
BenchmarkToBytes-4      1000000000               0.6898 ns/op
@mengzhuo
Copy link
Contributor

mengzhuo commented Jul 25, 2022

The 0.6898 ns/op means times of operation instead of allocation.
Are you missing the benchmark with --benchmem?

@mengzhuo mengzhuo added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jul 25, 2022
@illiafox
Copy link
Author

illiafox commented Jul 25, 2022

$ go test -bench=BenchmarkToBytes -count=1 -benchmem                                                                                                           
goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i5-2540M CPU @ 2.60GHz
BenchmarkToBytes-4      1000000000               0.6601 ns/op          0 B/op          0 allocs/op

oops, i thought that 1000000 is a number of allocations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

2 participants