From 222283a9c8849e939f0b05b6cfaf296763e9f169 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Fri, 30 May 2014 21:45:27 -0400 Subject: [PATCH] go.tools/cmd/benchcmp: add -best flag If you have multiple runs in old.txt and new.txt the default behavior is to match them up pairwise and compare successive pairs (and if you have a different number of runs in each file, benchcmp refuses to do anything). The new -best flag changes the behavior to instead compare the fastest run of each benchmark from the two files. This makes sense if you believe that the fastest speed is the 'actual' speed and the slower results are due to the computer spending time doing non-benchmark work while the benchmark was running. LGTM=josharian R=golang-codereviews, josharian CC=golang-codereviews https://golang.org/cl/102890047 --- cmd/benchcmp/benchcmp.go | 1 + cmd/benchcmp/parse.go | 10 +++++++++- cmd/benchcmp/parse_test.go | 40 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/cmd/benchcmp/benchcmp.go b/cmd/benchcmp/benchcmp.go index c2f0013e..56ef55ad 100644 --- a/cmd/benchcmp/benchcmp.go +++ b/cmd/benchcmp/benchcmp.go @@ -16,6 +16,7 @@ import ( var ( changedOnly = flag.Bool("changed", false, "show only benchmarks that have changed") magSort = flag.Bool("mag", false, "sort benchmarks by magnitude of change") + best = flag.Bool("best", false, "compare best times from old and new") ) const usageFooter = ` diff --git a/cmd/benchcmp/parse.go b/cmd/benchcmp/parse.go index 30102f88..f1df681c 100644 --- a/cmd/benchcmp/parse.go +++ b/cmd/benchcmp/parse.go @@ -114,8 +114,16 @@ func ParseBenchSet(r io.Reader) (BenchSet, error) { for scan.Scan() { if b, err := ParseLine(scan.Text()); err == nil { b.ord = ord - bb[b.Name] = append(bb[b.Name], b) ord++ + old := bb[b.Name] + if *best && old != nil { + if old[0].NsOp < b.NsOp { + continue + } + b.ord = old[0].ord + bb[b.Name] = old[:0] + } + bb[b.Name] = append(bb[b.Name], b) } } diff --git a/cmd/benchcmp/parse_test.go b/cmd/benchcmp/parse_test.go index 3f3c0036..a59b20c5 100644 --- a/cmd/benchcmp/parse_test.go +++ b/cmd/benchcmp/parse_test.go @@ -152,3 +152,43 @@ func TestParseBenchSet(t *testing.T) { t.Errorf("parsed bench set incorrectly, want %v have %v", want, have) } } + +func TestParseBenchSetBest(t *testing.T) { + // Test that -best mode takes best ns/op. + *best = true + defer func() { + *best = false + }() + + in := ` + Benchmark1 10 100 ns/op + Benchmark2 10 60 ns/op + Benchmark2 10 500 ns/op + Benchmark1 10 50 ns/op + ` + + want := BenchSet{ + "Benchmark1": []*Bench{ + { + Name: "Benchmark1", + N: 10, NsOp: 50, Measured: NsOp, + ord: 0, + }, + }, + "Benchmark2": []*Bench{ + { + Name: "Benchmark2", + N: 10, NsOp: 60, Measured: NsOp, + ord: 1, + }, + }, + } + + have, err := ParseBenchSet(strings.NewReader(in)) + if err != nil { + t.Fatalf("unexpected err during ParseBenchSet: %v", err) + } + if !reflect.DeepEqual(want, have) { + t.Errorf("parsed bench set incorrectly, want %v have %v", want, have) + } +}