]> Cypherpunks.ru repositories - gostls13.git/commitdiff
testing: always ReadMemStats before first benchmark run
authorMeir Fischer <meirfischer@gmail.com>
Mon, 26 Jun 2017 01:17:52 +0000 (21:17 -0400)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 28 Jun 2017 20:47:26 +0000 (20:47 +0000)
If the only way the user indicates they want alloc stats shown
is via ReportAllocs, we don't know that until benchFunc is run.
Therefore, StopTimer's ReadMemStats will return incorrect data
for single cycle runs since there's no counterpart ReadMemStats from
StartTimer that initializes alloc stats.

It appears that this bug was introduced by CL 46612,
"testing: only call ReadMemStats if necessary when benchmarking"

Fixes #20590

Change-Id: I3b5ef91677823f4b98011880a3be15423baf7e33
Reviewed-on: https://go-review.googlesource.com/46612
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/testing/benchmark.go
src/testing/sub_test.go

index 8b7f5cebaf08cfdeeec79f9a2fd3503e633b0c22..be9e96d50c948c5c068c0e4ee637949f956b717d 100644 (file)
@@ -73,11 +73,9 @@ type B struct {
 // a call to StopTimer.
 func (b *B) StartTimer() {
        if !b.timerOn {
-               if *benchmarkMemory || b.showAllocResult {
-                       runtime.ReadMemStats(&memStats)
-                       b.startAllocs = memStats.Mallocs
-                       b.startBytes = memStats.TotalAlloc
-               }
+               runtime.ReadMemStats(&memStats)
+               b.startAllocs = memStats.Mallocs
+               b.startBytes = memStats.TotalAlloc
                b.start = time.Now()
                b.timerOn = true
        }
index af2d39c5be13d5bed840bef1cda0aaaadf7015da..acf5dea8785588c8ed15cb381e626da9fa80f6a0 100644 (file)
@@ -540,6 +540,16 @@ func TestBenchmarkStartsFrom1(t *T) {
        })
 }
 
+func TestBenchmarkReadMemStatsBeforeFirstRun(t *T) {
+       var first = true
+       Benchmark(func(b *B) {
+               if first && (b.startAllocs == 0 || b.startBytes == 0) {
+                       panic(fmt.Sprintf("ReadMemStats not called before first run"))
+               }
+               first = false
+       })
+}
+
 func TestParallelSub(t *T) {
        c := make(chan int)
        block := make(chan int)