Skip to content

proposal: testing: panic if benchmark function tampers with b.N? #14479

@minux

Description

@minux

If the benchmark function changes b.N, the testing package will get very confused.

As an example, this (contrived) function:

func Benchmark(b *testing.B) {
    b.N = 1000000000
    time.Sleep(time.Second)
}

will lead to this:

Benchmark-4    1000000000               1.00 ns/op

I've seen this in the wild, for example:
https://groups.google.com/forum/#!topic/golang-nuts/DjKNjpSHi3M

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions