1
0
mirror of https://github.com/golang/go synced 2024-11-05 16:16:11 -07:00

testing: fix Benchmark() to start at 1 iteration, not 100

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>
This commit is contained in:
Heschi Kreinick 2017-02-27 18:26:33 -05:00 committed by Marcel van Lohuizen
parent 29f061960d
commit 5e90bbcc6d
2 changed files with 16 additions and 1 deletions

View File

@ -657,7 +657,10 @@ func Benchmark(f func(b *B)) BenchmarkResult {
benchFunc: f, benchFunc: f,
benchTime: *benchTime, benchTime: *benchTime,
} }
return b.run() if b.run1() {
b.run()
}
return b.result
} }
type discard struct{} type discard struct{}

View File

@ -7,6 +7,7 @@ package testing
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"os"
"regexp" "regexp"
"runtime" "runtime"
"strings" "strings"
@ -530,6 +531,16 @@ func TestBenchmarkOutput(t *T) {
Benchmark(func(b *B) {}) 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) { func TestParallelSub(t *T) {
c := make(chan int) c := make(chan int)
block := make(chan int) block := make(chan int)
@ -591,6 +602,7 @@ func TestBenchmark(t *T) {
res := Benchmark(func(b *B) { res := Benchmark(func(b *B) {
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
b.Run("", func(b *B) { b.Run("", func(b *B) {
fmt.Fprintf(os.Stderr, "b.N: %v\n", b.N)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
time.Sleep(time.Millisecond) time.Sleep(time.Millisecond)
} }