1
2
3
4
5 package main
6
7 import (
8 "fmt"
9 "math"
10
11 "golang.org/x/tools/benchmark/parse"
12 )
13
14
15 type BenchCmp struct {
16 Before *parse.Benchmark
17 After *parse.Benchmark
18 }
19
20
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)))
27 continue
28 }
29 for i, beforeb := range beforebb {
30 afterb := afterbb[i]
31 cmps = append(cmps, BenchCmp{beforeb, afterb})
32 }
33 }
34 return
35 }
36
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)}
44 }
45 func (c BenchCmp) DeltaAllocsPerOp() Delta {
46 return Delta{float64(c.Before.AllocsPerOp), float64(c.After.AllocsPerOp)}
47 }
48
49
50
51 type Delta struct {
52 Before float64
53 After float64
54 }
55
56
57
58 func (d Delta) mag() float64 {
59 switch {
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:
65 return 1
66 default:
67
68
69 return math.Inf(1)
70 }
71 }
72
73
74 func (d Delta) Changed() bool { return d.Before != d.After }
75
76
77
78 func (d Delta) Float64() float64 {
79 switch {
80 case d.Before != 0:
81 return d.After / d.Before
82 case d.After == 0:
83 return 1
84 default:
85 return math.Inf(1)
86 }
87 }
88
89
90 func (d Delta) Percent() string {
91 return fmt.Sprintf("%+.2f%%", 100*d.Float64()-100)
92 }
93
94
95 func (d Delta) Multiple() string {
96 return fmt.Sprintf("%.2fx", d.Float64())
97 }
98
99 func (d Delta) String() string {
100 return fmt.Sprintf("Δ(%f, %f)", d.Before, d.After)
101 }
102
103
104
105 type ByParseOrder []BenchCmp
106
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 }
110
111
112
113
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
118 }
119 return i.Name() < j.Name()
120 }
121
122
123
124 type ByDeltaNsPerOp []BenchCmp
125
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) }
129
130
131
132 type ByDeltaMBPerS []BenchCmp
133
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) }
137
138
139
140 type ByDeltaAllocedBytesPerOp []BenchCmp
141
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)
146 }
147
148
149
150 type ByDeltaAllocsPerOp []BenchCmp
151
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)
156 }
157
View as plain text