From 79fab70a63e03897db384fbf1b5fdeeb6f9f4b92 Mon Sep 17 00:00:00 2001 From: Marcel van Lohuizen Date: Tue, 14 Feb 2017 13:01:18 +0100 Subject: [PATCH] testing: fix stats bug for sub benchmarks Fixes golang/go#18815. Change-Id: Ic9d5cb640a555c58baedd597ed4ca5dd9f275c97 Reviewed-on: https://go-review.googlesource.com/36990 Run-TryBot: Marcel van Lohuizen TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/testing/benchmark.go | 3 --- src/testing/sub_test.go | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/testing/benchmark.go b/src/testing/benchmark.go index 3233347dcbd..86c0f10ecdc 100644 --- a/src/testing/benchmark.go +++ b/src/testing/benchmark.go @@ -657,9 +657,6 @@ func Benchmark(f func(b *B)) BenchmarkResult { benchFunc: f, benchTime: *benchTime, } - if !b.run1() { - return BenchmarkResult{} - } return b.run() } diff --git a/src/testing/sub_test.go b/src/testing/sub_test.go index c12a2c807ab..fe3e8ff8581 100644 --- a/src/testing/sub_test.go +++ b/src/testing/sub_test.go @@ -15,6 +15,11 @@ import ( "time" ) +func init() { + // Make benchmark tests run 10* faster. + *benchTime = 100 * time.Millisecond +} + func TestTestContext(t *T) { const ( add1 = 0 @@ -581,3 +586,18 @@ func TestRacyOutput(t *T) { t.Errorf("detected %d racy Writes", races) } } + +func TestBenchmark(t *T) { + res := Benchmark(func(b *B) { + for i := 0; i < 5; i++ { + b.Run("", func(b *B) { + for i := 0; i < b.N; i++ { + time.Sleep(time.Millisecond) + } + }) + } + }) + if res.NsPerOp() < 4000000 { + t.Errorf("want >5ms; got %v", time.Duration(res.NsPerOp())) + } +}