Skip to content

Commit

Permalink
remove StartTimer/StopTimer from the bench loop
Browse files Browse the repository at this point in the history
It can completely mess up the benchmark numbers for init functions that
were too small. Moreover, it could make the -benchtime estimates be way
off. For example, 'benchinit cmd/go' was taking over a minute to run the
benchmark, instead of the expected ~1s.

The benchtime estimate being off is likely the upstream issue
golang/go#27217.

The fact that StartTimer and StopTimer are expensive is being tracked in
golang/go#20875.
  • Loading branch information
mvdan committed Jan 13, 2019
1 parent 7d17615 commit 3aca7ab
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,11 @@ type toZero struct {
Offset, ZeroSize int64
}

// Don't use StartTimer and StopTimer, as they call ReadMemStats, which is way
// too expensive compared to most init functions.
// For example, on 'benchinit cmd/go', ReadMemStats was taking up over 90% of
// the cpu time, and throwing off all the numbers.

var benchTmpl = template.Must(template.New("").Parse(`
// Code generated by benchinit. DO NOT EDIT.
Expand All @@ -349,11 +354,8 @@ func BenchmarkInit(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
b.StartTimer()
_init()
b.StopTimer()
deinit() // get ready to run init again
_init()
}
}
Expand Down

0 comments on commit 3aca7ab

Please sign in to comment.