diff options
| author | Quentin Smith <quentin@golang.org> | 2017-02-06 11:59:01 -0500 |
|---|---|---|
| committer | Quentin Smith <quentin@golang.org> | 2017-02-07 00:08:39 +0000 |
| commit | 6b742b2f84fc4ddea27076aa1e581197d17bd863 (patch) | |
| tree | 0a57912cf2d8303e1b3faec809c3ee583e39b2a7 /src/testing | |
| parent | c0bd4f33ccc9a9454d50245a1dba1fa46e62a1ad (diff) | |
| download | go-6b742b2f84fc4ddea27076aa1e581197d17bd863.tar.xz | |
testing: print extra labels on benchmarks
When running benchmarks, print "goos", "goarch", and "pkg"
labels. This makes it easier to refer to benchmark logs and understand
how they were generated. "pkg" is printed only for benchmarks located
in GOPATH.
Change-Id: I397cbdd57b9fe8cbabbb354ec7bfba59f5625c42
Reviewed-on: https://go-review.googlesource.com/36356
Run-TryBot: Quentin Smith <quentin@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/testing')
| -rw-r--r-- | src/testing/benchmark.go | 22 | ||||
| -rw-r--r-- | src/testing/internal/testdeps/deps.go | 7 | ||||
| -rw-r--r-- | src/testing/testing.go | 4 |
3 files changed, 27 insertions, 6 deletions
diff --git a/src/testing/benchmark.go b/src/testing/benchmark.go index bcebb418c4..8d3f63d232 100644 --- a/src/testing/benchmark.go +++ b/src/testing/benchmark.go @@ -47,6 +47,7 @@ type InternalBenchmark struct { // affecting benchmark results. type B struct { common + importPath string // import path of the package containing the benchmark context *benchContext N int previousN int // number of iterations in the previous run @@ -233,9 +234,18 @@ func (b *B) run1() bool { return true } +var labelsOnce sync.Once + // run executes the benchmark in a separate goroutine, including all of its // subbenchmarks. b must not have subbenchmarks. func (b *B) run() BenchmarkResult { + labelsOnce.Do(func() { + fmt.Fprintf(b.w, "goos: %s\n", runtime.GOOS) + fmt.Fprintf(b.w, "goarch: %s\n", runtime.GOARCH) + if b.importPath != "" { + fmt.Fprintf(b.w, "pkg: %s\n", b.importPath) + } + }) if b.context != nil { // Running go test --test.bench b.context.processBench(b) // Must call doBench. @@ -363,10 +373,10 @@ type benchContext struct { // An internal function but exported because it is cross-package; part of the implementation // of the "go test" command. func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks []InternalBenchmark) { - runBenchmarks(matchString, benchmarks) + runBenchmarks("", matchString, benchmarks) } -func runBenchmarks(matchString func(pat, str string) (bool, error), benchmarks []InternalBenchmark) bool { +func runBenchmarks(importPath string, matchString func(pat, str string) (bool, error), benchmarks []InternalBenchmark) bool { // If no flag was specified, don't run benchmarks. if len(*matchBenchmarks) == 0 { return true @@ -398,6 +408,7 @@ func runBenchmarks(matchString func(pat, str string) (bool, error), benchmarks [ w: os.Stdout, chatty: *chatty, }, + importPath: importPath, benchFunc: func(b *B) { for _, Benchmark := range bs { b.Run(Benchmark.Name, Benchmark.F) @@ -486,9 +497,10 @@ func (b *B) Run(name string, f func(b *B)) bool { w: b.w, chatty: b.chatty, }, - benchFunc: f, - benchTime: b.benchTime, - context: b.context, + importPath: b.importPath, + benchFunc: f, + benchTime: b.benchTime, + context: b.context, } if sub.run1() { sub.run() diff --git a/src/testing/internal/testdeps/deps.go b/src/testing/internal/testdeps/deps.go index b08300b5d6..042f69614e 100644 --- a/src/testing/internal/testdeps/deps.go +++ b/src/testing/internal/testdeps/deps.go @@ -49,3 +49,10 @@ func (TestDeps) WriteHeapProfile(w io.Writer) error { func (TestDeps) WriteProfileTo(name string, w io.Writer, debug int) error { return pprof.Lookup(name).WriteTo(w, debug) } + +// ImportPath is the import path of the testing binary, set by the generated main function. +var ImportPath string + +func (TestDeps) ImportPath() string { + return ImportPath +} diff --git a/src/testing/testing.go b/src/testing/testing.go index 5efbc244fe..97c703d8ba 100644 --- a/src/testing/testing.go +++ b/src/testing/testing.go @@ -766,6 +766,7 @@ func (f matchStringOnly) StartCPUProfile(w io.Writer) error { return e func (f matchStringOnly) StopCPUProfile() {} func (f matchStringOnly) WriteHeapProfile(w io.Writer) error { return errMain } func (f matchStringOnly) WriteProfileTo(string, io.Writer, int) error { return errMain } +func (f matchStringOnly) ImportPath() string { return "" } // Main is an internal function, part of the implementation of the "go test" command. // It was exported because it is cross-package and predates "internal" packages. @@ -795,6 +796,7 @@ type testDeps interface { StopCPUProfile() WriteHeapProfile(io.Writer) error WriteProfileTo(string, io.Writer, int) error + ImportPath() string } // MainStart is meant for use by tests generated by 'go test'. @@ -827,7 +829,7 @@ func (m *M) Run() int { if !testRan && !exampleRan && *matchBenchmarks == "" { fmt.Fprintln(os.Stderr, "testing: warning: no tests to run") } - if !testOk || !exampleOk || !runBenchmarks(m.deps.MatchString, m.benchmarks) || race.Errors() > 0 { + if !testOk || !exampleOk || !runBenchmarks(m.deps.ImportPath(), m.deps.MatchString, m.benchmarks) || race.Errors() > 0 { fmt.Println("FAIL") m.after() return 1 |
