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
Closed

testing: Benchmark shows zero allocations #20863

rogpeppe opened this issue Jun 30, 2017 · 10 comments
Labels
Documentation FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@rogpeppe
Copy link
Contributor

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
Copy link
Contributor Author

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
Copy link
Member

ALTree 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 ALTree added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Jun 30, 2017
@ALTree
Copy link
Member

ALTree commented Jun 30, 2017

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

@gopherbot
Copy link

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

@bradfitz
Copy link
Contributor

@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
Copy link
Contributor

/cc @josharian @aclements

@aclements
Copy link
Member

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

@bradfitz
Copy link
Contributor

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

@gopherbot
Copy link

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

@ALTree
Copy link
Member

ALTree 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.
Labels
Documentation FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

5 participants