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: Benchmark shows zero allocations #20863

Closed
rogpeppe opened this issue Jun 30, 2017 · 10 comments

Comments

Projects
None yet
5 participants
@rogpeppe
Copy link
Contributor

commented Jun 30, 2017

testing.Benchmark does not seem to produce valid memory stats any more:

The following program:

package main

import (
	"fmt"
	"testing"
)

var global *int

func main() {
	r := testing.Benchmark(func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			global = new(int)
		}
	})
	fmt.Printf("creating filter: %v\n", r.MemString())
}

always prints:

       0 B/op	       0 allocs/op

I'd expect it to print

       8 B/op	       1 allocs/op

like previous Go versions.

go version devel +eab99a8 Mon Jun 26 21:12:22 2017 +0000 linux/amd64

@rogpeppe rogpeppe changed the title testing: testing.Benchmark shows zero allocations testing: Benchmark shows zero allocations Jun 30, 2017

@rogpeppe

This comment has been minimized.

Copy link
Contributor Author

commented Jun 30, 2017

It turns out that it prints memory statistics when b.ReportAllocs is called.
As this is a behaviour change, perhaps this should be documented in
the Go 1.9 release notes.

@ALTree

This comment has been minimized.

Copy link
Member

commented Jun 30, 2017

This is already documented

MemAllocs and MemBytes may be zero if memory benchmarking is not requested using B.ReportAllocs or the -benchmem command line flag.

http://tip.golang.org/pkg/testing/#BenchmarkResult

but if it was changed in go1.9 then I agree it's worth mentioning in release notes, too.

@ALTree ALTree added this to the Go1.9 milestone Jun 30, 2017

@ALTree

This comment has been minimized.

Copy link
Member

commented Jun 30, 2017

The change was introduced in https://go-review.googlesource.com/c/36791/

@gopherbot

This comment has been minimized.

Copy link

commented Jun 30, 2017

CL https://golang.org/cl/47331 mentions this issue.

@bradfitz

This comment has been minimized.

Copy link
Member

commented Jun 30, 2017

@ALTree I would rather revert CL 36791 now that ReadMemStats is fast (see https://tip.golang.org/doc/go1.9#gc).

I'll send a CL.

@bradfitz

This comment has been minimized.

Copy link
Member

commented Jun 30, 2017

@aclements

This comment has been minimized.

Copy link
Member

commented Jun 30, 2017

I thought this was fixed a few days ago by https://golang.org/cl/46612?

@bradfitz

This comment has been minimized.

Copy link
Member

commented Jun 30, 2017

Yeah, I was just about to point that out. Looks like that only reverted 1 of the 3 spots.

@gopherbot

This comment has been minimized.

Copy link

commented Jun 30, 2017

CL https://golang.org/cl/47350 mentions this issue.

@gopherbot gopherbot closed this in 0ff876a Jun 30, 2017

@ALTree

This comment has been minimized.

Copy link
Member

commented Jun 30, 2017

Ah, thanks for noticing. I didn't notice that that change was partly reverted.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.