Debian Codegraph @

internal/cmd/benchcheck/compare.go

raw
1 package main
2
3 import (
4 "math"
5 "sort"
6 )
7
8 type status string
9
10 const (
11 // statusOK means the benchmark is not slower than the base beyond the tolerance of a single benchmark.
12 statusOK status = "ok"
13 // statusRegressed means the benchmark is slower than the base beyond the tolerance of a single benchmark.
14 statusRegressed status = "regressed"
15 // statusNew means the benchmark doesn't exist in the base, so there is nothing to compare.
16 statusNew status = "new"
17 )
18
19 type benchResult struct {
20 Name string `json:"name"`
21 // Func is the top-level benchmark function which the benchmark belongs to.
22 Func string `json:"func"`
23 Status status `json:"status"`
24 // BaseNs and HeadNs are the fastest results of all the attempts.
25 // The noise of a measurement only makes it slower, so the fastest one is the closest to the real cost.
26 // Each side takes its own fastest result: if a pair measured in the same attempt were compared,
27 // a base which happened to be slow in an attempt would let a degraded head pass.
28 BaseNs float64 `json:"baseNs"`
29 HeadNs float64 `json:"headNs"`
30 Attempts int `json:"attempts"`
31 // baseSeen is whether the base has reported the benchmark.
32 // It is not the same as BaseNs != 0: the testing package reports zero ns/op as well.
33 baseSeen bool
34 }
35
36 // comparable reports whether the result has both sides to compare.
37 // ns/op is zero if the testing package omitted it, and the ratio to zero can't be represented.
38 func (r *benchResult) comparable() bool {
39 return r.baseSeen && r.BaseNs > 0 && r.HeadNs > 0
40 }
41
42 // deltaPercent returns how much slower the head is than the base, in percent.
43 func (r *benchResult) deltaPercent() float64 {
44 return (r.HeadNs/r.BaseNs - 1) * 100
45 }
46
47 // comparison collects the results of the attempts and decides whether the head is degraded.
48 //
49 // A single benchmark is too noisy to be judged with a small tolerance: on a shared machine the same code
50 // differs by several percent between two builds. So the head is judged by the mean of all the benchmarks,
51 // whose noise is far smaller, with meanTolerance. A single benchmark is judged only with singleTolerance,
52 // which is beyond the noise, because the mean can't notice the degradation of only a few benchmarks.
53 type comparison struct {
54 meanTolerance float64
55 singleTolerance float64
56 results map[string]*benchResult
57 }
58
59 func newComparison(meanTolerance, singleTolerance float64) *comparison {
60 return &comparison{
61 meanTolerance: meanTolerance,
62 singleTolerance: singleTolerance,
63 results: map[string]*benchResult{},
64 }
65 }
66
67 // add records one attempt of the benchmark function fn:
68 // base and head are the results of the base and of the working tree measured for the attempt.
69 func (c *comparison) add(fn string, base, head measurement) {
70 for name, headNs := range head {
71 result, exists := c.results[name]
72 if !exists {
73 result = &benchResult{Name: name, Func: fn, HeadNs: headNs}
74 c.results[name] = result
75 }
76 result.Attempts++
77 if headNs < result.HeadNs {
78 result.HeadNs = headNs
79 }
80 if baseNs, hasBase := base[name]; hasBase {
81 if !result.baseSeen || baseNs < result.BaseNs {
82 result.BaseNs = baseNs
83 }
84 result.baseSeen = true
85 }
86 c.judge(result)
87 }
88 }
89
90 func (c *comparison) judge(result *benchResult) {
91 switch {
92 case !result.baseSeen:
93 result.Status = statusNew
94 case result.BaseNs == 0 && result.HeadNs > 0:
95 // the base was too fast to be measured and the head is not.
96 // The ratio can't be represented, so it is not a part of the mean either.
97 result.Status = statusRegressed
98 case result.comparable() && result.deltaPercent() > c.singleTolerance:
99 result.Status = statusRegressed
100 default:
101 result.Status = statusOK
102 }
103 }
104
105 // meanDeltaPercent returns how much slower the head is than the base on average, in percent.
106 // It is the geometric mean of the ratios, which is the mean of how many times slower each benchmark is.
107 func (c *comparison) meanDeltaPercent() float64 {
108 var (
109 sum float64
110 count int
111 )
112 for _, result := range c.results {
113 if !result.comparable() {
114 continue
115 }
116 sum += math.Log(result.HeadNs / result.BaseNs)
117 count++
118 }
119 if count == 0 {
120 return 0
121 }
122 return (math.Exp(sum/float64(count)) - 1) * 100
123 }
124
125 // degraded reports whether the head is degraded: the mean is beyond its tolerance,
126 // or a benchmark is beyond the tolerance of a single benchmark.
127 func (c *comparison) degraded() bool {
128 if c.meanDeltaPercent() > c.meanTolerance {
129 return true
130 }
131 for _, result := range c.results {
132 if result.Status == statusRegressed {
133 return true
134 }
135 }
136 return false
137 }
138
139 // pendingFuncs returns the benchmark functions to measure again: nothing if the head is not degraded,
140 // otherwise the ones which have a benchmark slower than the tolerance of the mean, which are what makes
141 // the mean slow. Measuring them again gets rid of the noise, because the fastest result is kept.
142 func (c *comparison) pendingFuncs() []string {
143 if !c.degraded() {
144 return nil
145 }
146 seen := map[string]struct{}{}
147 funcs := []string{}
148 for _, result := range c.results {
149 if !result.comparable() || result.deltaPercent() <= c.meanTolerance {
150 continue
151 }
152 if _, exists := seen[result.Func]; exists {
153 continue
154 }
155 seen[result.Func] = struct{}{}
156 funcs = append(funcs, result.Func)
157 }
158 sort.Strings(funcs)
159 return funcs
160 }
161
162 func (c *comparison) sortedResults() []benchResult {
163 results := make([]benchResult, 0, len(c.results))
164 for _, result := range c.results {
165 results = append(results, *result)
166 }
167 sort.Slice(results, func(i, j int) bool { return results[i].Name < results[j].Name })
168 return results
169 }