Skip to content

testing: Benchmark shows zero allocations #20863

Description

@rogpeppe

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

Activity

  1. changed the title [-]testing: testing.Benchmark shows zero allocations[/-] [+]testing: Benchmark shows zero allocations[/+] on Jun 30, 2017
  2. rogpeppe commented on Jun 30, 2017

    @rogpeppe
    ContributorAuthor

    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.

  3. ALTree commented on Jun 30, 2017

    @ALTree
    Member

    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.

  4. added this to the Go1.9 milestone on Jun 30, 2017
  5. added
    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.
    on Jun 30, 2017
  6. ALTree commented on Jun 30, 2017

    @ALTree
    Member

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

  7. gopherbot commented on Jun 30, 2017

    @gopherbot
    Contributor

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

  8. bradfitz commented on Jun 30, 2017

    @bradfitz
    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.

  9. bradfitz commented on Jun 30, 2017

    @bradfitz
    Contributor
  10. aclements commented on Jun 30, 2017

    @aclements
    Member

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

  11. bradfitz commented on Jun 30, 2017

    @bradfitz
    Contributor

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

  12. gopherbot commented on Jun 30, 2017

    @gopherbot
    Contributor

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

  13. ALTree commented on Jun 30, 2017

    @ALTree
    Member

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

  14. locked and limited conversation to collaborators on Jun 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.FrozenDueToAgeNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions