From b7a2d413a3f710f14accedf185c93bfb63d24fd0 Mon Sep 17 00:00:00 2001 From: Martin Möhrmann Date: Tue, 20 Oct 2020 09:56:14 +0200 Subject: testing: print cpu type as label for benchmarks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Supports 386 and amd64 architectures on all operating systems. Example output: $ go test -bench=.* goos: darwin goarch: amd64 pkg: strconv cpu: Intel(R) Core(TM) i7-3520M CPU @ 2.90GHz BenchmarkAtof64Decimal-4 24431032 46.8 ns/op ... As the displayed CPU information is only used for information purposes it is lazily initialized when needed using the new internal/sysinfo package. This allows internal/cpu to stay without dependencies and avoid initialization costs when the CPU information is not needed as the new code to query the CPU name in internal/cpu can be dead code eliminated if not used. Fixes #39214 Change-Id: I77ae5c5d2fed6b28fa78dd45075f9f0a6a7f1bfd Reviewed-on: https://go-review.googlesource.com/c/go/+/263804 Trust: Martin Möhrmann Reviewed-by: Keith Randall --- src/testing/benchmark.go | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/testing/benchmark.go') diff --git a/src/testing/benchmark.go b/src/testing/benchmark.go index e9687bf26d..1b81ec3a2d 100644 --- a/src/testing/benchmark.go +++ b/src/testing/benchmark.go @@ -8,6 +8,7 @@ import ( "flag" "fmt" "internal/race" + "internal/sysinfo" "io" "math" "os" @@ -262,6 +263,9 @@ func (b *B) run() { if b.importPath != "" { fmt.Fprintf(b.w, "pkg: %s\n", b.importPath) } + if cpu := sysinfo.CPU.Name(); cpu != "" { + fmt.Fprintf(b.w, "cpu: %s\n", cpu) + } }) if b.context != nil { // Running go test --test.bench @@ -648,6 +652,9 @@ func (b *B) Run(name string, f func(b *B)) bool { if b.importPath != "" { fmt.Printf("pkg: %s\n", b.importPath) } + if cpu := sysinfo.CPU.Name(); cpu != "" { + fmt.Printf("cpu: %s\n", cpu) + } }) fmt.Println(benchName) -- cgit v1.3 From afe7c8d0b25f26f0abd749ca52c7e1e7dfdee8cb Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Mon, 2 Nov 2020 08:48:23 -0500 Subject: testing: increase benchmark output to four significant figures Currently, the benchmark output from the testing package prints small values with three significant figures. This means it can only distinguish 1 part in 100, or a 1% error, which can be enough to throw off further analysis of the output. This CL increases it to four significant figures. For time values, at least, anything beyond four significant figures is almost certainly noise. Fixes #34626. Change-Id: I3bcf305427130026276e6a4c78167989319f280c Reviewed-on: https://go-review.googlesource.com/c/go/+/267102 Trust: Austin Clements Run-TryBot: Austin Clements TryBot-Result: Go Bot Reviewed-by: Michael Pratt --- src/testing/benchmark.go | 20 ++++++++++++-------- src/testing/benchmark_test.go | 21 +++++++++++---------- 2 files changed, 23 insertions(+), 18 deletions(-) (limited to 'src/testing/benchmark.go') diff --git a/src/testing/benchmark.go b/src/testing/benchmark.go index 1b81ec3a2d..a8f75e9712 100644 --- a/src/testing/benchmark.go +++ b/src/testing/benchmark.go @@ -451,23 +451,27 @@ func (r BenchmarkResult) String() string { func prettyPrint(w io.Writer, x float64, unit string) { // Print all numbers with 10 places before the decimal point - // and small numbers with three sig figs. + // and small numbers with four sig figs. Field widths are + // chosen to fit the whole part in 10 places while aligning + // the decimal point of all fractional formats. var format string switch y := math.Abs(x); { - case y == 0 || y >= 99.95: + case y == 0 || y >= 999.95: format = "%10.0f %s" - case y >= 9.995: + case y >= 99.995: format = "%12.1f %s" - case y >= 0.9995: + case y >= 9.9995: format = "%13.2f %s" - case y >= 0.09995: + case y >= 0.99995: format = "%14.3f %s" - case y >= 0.009995: + case y >= 0.099995: format = "%15.4f %s" - case y >= 0.0009995: + case y >= 0.0099995: format = "%16.5f %s" - default: + case y >= 0.00099995: format = "%17.6f %s" + default: + format = "%18.7f %s" } fmt.Fprintf(w, format, x, unit) } diff --git a/src/testing/benchmark_test.go b/src/testing/benchmark_test.go index 1434c2613f..4c1cbd1933 100644 --- a/src/testing/benchmark_test.go +++ b/src/testing/benchmark_test.go @@ -22,13 +22,14 @@ var prettyPrintTests = []struct { {0, " 0 x"}, {1234.1, " 1234 x"}, {-1234.1, " -1234 x"}, - {99.950001, " 100 x"}, - {99.949999, " 99.9 x"}, - {9.9950001, " 10.0 x"}, - {9.9949999, " 9.99 x"}, - {-9.9949999, " -9.99 x"}, - {0.0099950001, " 0.0100 x"}, - {0.0099949999, " 0.00999 x"}, + {999.950001, " 1000 x"}, + {999.949999, " 999.9 x"}, + {99.9950001, " 100.0 x"}, + {99.9949999, " 99.99 x"}, + {-99.9949999, " -99.99 x"}, + {0.000999950001, " 0.001000 x"}, + {0.000999949999, " 0.0009999 x"}, // smallest case + {0.0000999949999, " 0.0001000 x"}, } func TestPrettyPrint(t *testing.T) { @@ -50,13 +51,13 @@ func TestResultString(t *testing.T) { if r.NsPerOp() != 2 { t.Errorf("NsPerOp: expected 2, actual %v", r.NsPerOp()) } - if want, got := " 100\t 2.40 ns/op", r.String(); want != got { + if want, got := " 100\t 2.400 ns/op", r.String(); want != got { t.Errorf("String: expected %q, actual %q", want, got) } // Test sub-1 ns/op (issue #31005) r.T = 40 * time.Nanosecond - if want, got := " 100\t 0.400 ns/op", r.String(); want != got { + if want, got := " 100\t 0.4000 ns/op", r.String(); want != got { t.Errorf("String: expected %q, actual %q", want, got) } @@ -130,7 +131,7 @@ func TestReportMetric(t *testing.T) { } // Test stringing. res.N = 1 // Make the output stable - want := " 1\t 12345 ns/op\t 0.200 frobs/op" + want := " 1\t 12345 ns/op\t 0.2000 frobs/op" if want != res.String() { t.Errorf("expected %q, actual %q", want, res.String()) } -- cgit v1.3