aboutsummaryrefslogtreecommitdiff
path: root/src/testing/benchmark.go
diff options
context:
space:
mode:
authorCaleb Spare <cespare@gmail.com>2019-04-24 12:06:43 -0700
committerBrad Fitzpatrick <bradfitz@golang.org>2019-04-29 19:54:30 +0000
commitfbc6a972226f889d2ab1150468755615098ee80f (patch)
treeb8b743fc496592f20ae81a8cb024868926cc38be /src/testing/benchmark.go
parent2b8cbc384d092dc63e9dc18ec318d0682185611c (diff)
downloadgo-fbc6a972226f889d2ab1150468755615098ee80f.tar.xz
testing: delay flag registration; move to an Init function
Any code that imports the testing package forces the testing flags to be defined, even in non-test binaries. People work around this today by defining a copy of the testing.TB interface just to avoid importing testing. Fix this by moving flag registration into a new function, testing.Init. Delay calling Init until the testing binary begins to run, in testing.MainStart. Init is exported for cases where users need the testing flags to be defined outside of a "go test" context. In particular, this may be needed where testing.Benchmark is called outside of a test. Fixes #21051 Change-Id: Ib7e02459e693c26ae1ba71bbae7d455a91118ee3 Reviewed-on: https://go-review.googlesource.com/c/go/+/173722 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/testing/benchmark.go')
-rw-r--r--src/testing/benchmark.go18
1 files changed, 13 insertions, 5 deletions
diff --git a/src/testing/benchmark.go b/src/testing/benchmark.go
index cc22bdd2b5..0e348be358 100644
--- a/src/testing/benchmark.go
+++ b/src/testing/benchmark.go
@@ -21,14 +21,19 @@ import (
"unicode"
)
-var matchBenchmarks = flag.String("test.bench", "", "run only benchmarks matching `regexp`")
-var benchTime = benchTimeFlag{d: 1 * time.Second}
-var benchmarkMemory = flag.Bool("test.benchmem", false, "print memory allocations for benchmarks")
-
-func init() {
+func initBenchmarkFlags() {
+ matchBenchmarks = flag.String("test.bench", "", "run only benchmarks matching `regexp`")
+ benchmarkMemory = flag.Bool("test.benchmem", false, "print memory allocations for benchmarks")
flag.Var(&benchTime, "test.benchtime", "run each benchmark for duration `d`")
}
+var (
+ matchBenchmarks *string
+ benchmarkMemory *bool
+
+ benchTime = benchTimeFlag{d: 1 * time.Second} // changed during test of testing package
+)
+
type benchTimeFlag struct {
d time.Duration
n int
@@ -755,6 +760,9 @@ func (b *B) SetParallelism(p int) {
// Benchmark benchmarks a single function. It is useful for creating
// custom benchmarks that do not use the "go test" command.
//
+// If f depends on testing flags, then Init must be used to register
+// those flags before calling Benchmark and before calling flag.Parse.
+//
// If f calls Run, the result will be an estimate of running all its
// subbenchmarks that don't call Run in sequence in a single benchmark.
func Benchmark(f func(b *B)) BenchmarkResult {