From e00a2395d9e30b92c2e5050831398647a4c95e5e Mon Sep 17 00:00:00 2001 From: Damian Gryski Date: Sun, 9 Apr 2023 09:41:15 -0700 Subject: [PATCH] testing: fix benchmark logging output --- src/testing/benchmark.go | 15 ++++++++++++--- src/testing/testing.go | 4 ++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/testing/benchmark.go b/src/testing/benchmark.go index 5a353260..b7bd106b 100644 --- a/src/testing/benchmark.go +++ b/src/testing/benchmark.go @@ -11,6 +11,7 @@ import ( "fmt" "io" "math" + "os" "runtime" "strconv" "strings" @@ -378,7 +379,8 @@ func runBenchmarks(matchString func(pat, str string) (bool, error), benchmarks [ } main := &B{ common: common{ - name: "Main", + output: &logger{}, + name: "Main", }, benchTime: benchTime, benchFunc: func(b *B) { @@ -416,6 +418,12 @@ func (b *B) processBench(ctx *benchContext) { results += "\t" + r.MemString() } fmt.Println(results) + + // Print any benchmark output + if b.output.Len() > 0 { + fmt.Printf("--- BENCH: %s\n", benchName) + b.output.WriteTo(os.Stdout) + } } } } @@ -436,8 +444,9 @@ func (b *B) Run(name string, f func(b *B)) bool { b.hasSub = true sub := &B{ common: common{ - name: benchName, - level: b.level + 1, + output: &logger{}, + name: benchName, + level: b.level + 1, }, benchFunc: f, benchTime: b.benchTime, diff --git a/src/testing/testing.go b/src/testing/testing.go index 5c81a312..7f01f131 100644 --- a/src/testing/testing.go +++ b/src/testing/testing.go @@ -95,6 +95,10 @@ func (l *logger) WriteTo(w io.Writer) (int64, error) { } +func (l *logger) Len() int { + return l.b.Len() +} + // Short reports whether the -test.short flag is set. func Short() bool { return flagShort