]> Cypherpunks repositories - gostls13.git/commitdiff
testing: fix Benchmark() to start at 1 iteration, not 100
authorHeschi Kreinick <heschi@google.com>
Mon, 27 Feb 2017 23:26:33 +0000 (18:26 -0500)
committerMarcel van Lohuizen <mpvl@golang.org>
Wed, 1 Mar 2017 11:06:34 +0000 (11:06 +0000)
The run1 call removed in golang.org/cl/36990 was necessary to
initialize the duration of the benchmark. With it gone, the math in
launch() starts from 100. This doesn't work out well for second-long
benchmark methods. Put it back.

Updates #18815

Change-Id: I461f3466c805d0c61124a2974662f7ad45335794
Reviewed-on: https://go-review.googlesource.com/37530
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
src/testing/benchmark.go
src/testing/sub_test.go

index 86c0f10ecdc1d960cc69d9bd1a78606a59fe59cc..18a46d93bf0edc80e11b1dc668de1ec825eb7726 100644 (file)
@@ -657,7 +657,10 @@ func Benchmark(f func(b *B)) BenchmarkResult {
                benchFunc: f,
                benchTime: *benchTime,
        }
-       return b.run()
+       if b.run1() {
+               b.run()
+       }
+       return b.result
 }
 
 type discard struct{}
index fe3e8ff8581ea024261204ad3454e972623ee6e0..ab145b5bf4f5deaf30f275ab299da91479d604e5 100644 (file)
@@ -7,6 +7,7 @@ package testing
 import (
        "bytes"
        "fmt"
+       "os"
        "regexp"
        "runtime"
        "strings"
@@ -530,6 +531,16 @@ func TestBenchmarkOutput(t *T) {
        Benchmark(func(b *B) {})
 }
 
+func TestBenchmarkStartsFrom1(t *T) {
+       var first = true
+       Benchmark(func(b *B) {
+               if first && b.N != 1 {
+                       panic(fmt.Sprintf("Benchmark() first N=%v; want 1", b.N))
+               }
+               first = false
+       })
+}
+
 func TestParallelSub(t *T) {
        c := make(chan int)
        block := make(chan int)
@@ -591,6 +602,7 @@ func TestBenchmark(t *T) {
        res := Benchmark(func(b *B) {
                for i := 0; i < 5; i++ {
                        b.Run("", func(b *B) {
+                               fmt.Fprintf(os.Stderr, "b.N: %v\n", b.N)
                                for i := 0; i < b.N; i++ {
                                        time.Sleep(time.Millisecond)
                                }