1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
11 "golang.org/x/tools/benchmark/parse"
14 // BenchCmp is a pair of benchmarks.
15 type BenchCmp struct {
16 Before *parse.Benchmark
17 After *parse.Benchmark
20 // Correlate correlates benchmarks from two BenchSets.
21 func Correlate(before, after parse.Set) (cmps []BenchCmp, warnings []string) {
22 cmps = make([]BenchCmp, 0, len(after))
23 for name, beforebb := range before {
24 afterbb := after[name]
25 if len(beforebb) != len(afterbb) {
26 warnings = append(warnings, fmt.Sprintf("ignoring %s: before has %d instances, after has %d", name, len(beforebb), len(afterbb)))
29 for i, beforeb := range beforebb {
31 cmps = append(cmps, BenchCmp{beforeb, afterb})
37 func (c BenchCmp) Name() string { return c.Before.Name }
38 func (c BenchCmp) String() string { return fmt.Sprintf("<%s, %s>", c.Before, c.After) }
39 func (c BenchCmp) Measured(flag int) bool { return (c.Before.Measured & c.After.Measured & flag) != 0 }
40 func (c BenchCmp) DeltaNsPerOp() Delta { return Delta{c.Before.NsPerOp, c.After.NsPerOp} }
41 func (c BenchCmp) DeltaMBPerS() Delta { return Delta{c.Before.MBPerS, c.After.MBPerS} }
42 func (c BenchCmp) DeltaAllocedBytesPerOp() Delta {
43 return Delta{float64(c.Before.AllocedBytesPerOp), float64(c.After.AllocedBytesPerOp)}
45 func (c BenchCmp) DeltaAllocsPerOp() Delta {
46 return Delta{float64(c.Before.AllocsPerOp), float64(c.After.AllocsPerOp)}
49 // Delta is the before and after value for a benchmark measurement.
50 // Both must be non-negative.
56 // mag calculates the magnitude of a change, regardless of the direction of
57 // the change. mag is intended for sorting and has no independent meaning.
58 func (d Delta) mag() float64 {
60 case d.Before != 0 && d.After != 0 && d.Before >= d.After:
61 return d.After / d.Before
62 case d.Before != 0 && d.After != 0 && d.Before < d.After:
63 return d.Before / d.After
64 case d.Before == 0 && d.After == 0:
68 // These are significant changes and worth surfacing.
73 // Changed reports whether the benchmark quantities are different.
74 func (d Delta) Changed() bool { return d.Before != d.After }
76 // Float64 returns After / Before. If Before is 0, Float64 returns
77 // 1 if After is also 0, and +Inf otherwise.
78 func (d Delta) Float64() float64 {
81 return d.After / d.Before
89 // Percent formats a Delta as a percent change, ranging from -100% up.
90 func (d Delta) Percent() string {
91 return fmt.Sprintf("%+.2f%%", 100*d.Float64()-100)
94 // Multiple formats a Delta as a multiplier, ranging from 0.00x up.
95 func (d Delta) Multiple() string {
96 return fmt.Sprintf("%.2fx", d.Float64())
99 func (d Delta) String() string {
100 return fmt.Sprintf("Δ(%f, %f)", d.Before, d.After)
103 // ByParseOrder sorts BenchCmps to match the order in
104 // which the Before benchmarks were presented to Parse.
105 type ByParseOrder []BenchCmp
107 func (x ByParseOrder) Len() int { return len(x) }
108 func (x ByParseOrder) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
109 func (x ByParseOrder) Less(i, j int) bool { return x[i].Before.Ord < x[j].Before.Ord }
111 // lessByDelta provides lexicographic ordering:
112 // * largest delta by magnitude
113 // * alphabetic by name
114 func lessByDelta(i, j BenchCmp, calcDelta func(BenchCmp) Delta) bool {
115 iDelta, jDelta := calcDelta(i).mag(), calcDelta(j).mag()
116 if iDelta != jDelta {
117 return iDelta < jDelta
119 return i.Name() < j.Name()
122 // ByDeltaNsPerOp sorts BenchCmps lexicographically by change
123 // in ns/op, descending, then by benchmark name.
124 type ByDeltaNsPerOp []BenchCmp
126 func (x ByDeltaNsPerOp) Len() int { return len(x) }
127 func (x ByDeltaNsPerOp) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
128 func (x ByDeltaNsPerOp) Less(i, j int) bool { return lessByDelta(x[i], x[j], BenchCmp.DeltaNsPerOp) }
130 // ByDeltaMBPerS sorts BenchCmps lexicographically by change
131 // in MB/s, descending, then by benchmark name.
132 type ByDeltaMBPerS []BenchCmp
134 func (x ByDeltaMBPerS) Len() int { return len(x) }
135 func (x ByDeltaMBPerS) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
136 func (x ByDeltaMBPerS) Less(i, j int) bool { return lessByDelta(x[i], x[j], BenchCmp.DeltaMBPerS) }
138 // ByDeltaAllocedBytesPerOp sorts BenchCmps lexicographically by change
139 // in B/op, descending, then by benchmark name.
140 type ByDeltaAllocedBytesPerOp []BenchCmp
142 func (x ByDeltaAllocedBytesPerOp) Len() int { return len(x) }
143 func (x ByDeltaAllocedBytesPerOp) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
144 func (x ByDeltaAllocedBytesPerOp) Less(i, j int) bool {
145 return lessByDelta(x[i], x[j], BenchCmp.DeltaAllocedBytesPerOp)
148 // ByDeltaAllocsPerOp sorts BenchCmps lexicographically by change
149 // in allocs/op, descending, then by benchmark name.
150 type ByDeltaAllocsPerOp []BenchCmp
152 func (x ByDeltaAllocsPerOp) Len() int { return len(x) }
153 func (x ByDeltaAllocsPerOp) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
154 func (x ByDeltaAllocsPerOp) Less(i, j int) bool {
155 return lessByDelta(x[i], x[j], BenchCmp.DeltaAllocsPerOp)