1
0
mirror of https://github.com/golang/go synced 2024-11-18 15:14:44 -07:00

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
This commit is contained in:
Russ Cox 2014-05-30 21:45:27 -04:00
parent 43c97eab79
commit 222283a9c8
3 changed files with 50 additions and 1 deletions

View File

@ -16,6 +16,7 @@ import (
var ( var (
changedOnly = flag.Bool("changed", false, "show only benchmarks that have changed") changedOnly = flag.Bool("changed", false, "show only benchmarks that have changed")
magSort = flag.Bool("mag", false, "sort benchmarks by magnitude of change") 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 = ` const usageFooter = `

View File

@ -114,8 +114,16 @@ func ParseBenchSet(r io.Reader) (BenchSet, error) {
for scan.Scan() { for scan.Scan() {
if b, err := ParseLine(scan.Text()); err == nil { if b, err := ParseLine(scan.Text()); err == nil {
b.ord = ord b.ord = ord
bb[b.Name] = append(bb[b.Name], b)
ord++ 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)
} }
} }

View File

@ -152,3 +152,43 @@ func TestParseBenchSet(t *testing.T) {
t.Errorf("parsed bench set incorrectly, want %v have %v", want, have) 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)
}
}