| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/sha256" |
| 5 |
"encoding/hex" |
| 6 |
"encoding/json" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"io/fs" |
| 10 |
"os" |
| 11 |
"path/filepath" |
| 12 |
) |
| 13 |
|
| 14 |
// benchConfig is the set of options which decides what is measured. |
| 15 |
type benchConfig struct { |
| 16 |
Dir string `json:"dir"` |
| 17 |
Bench string `json:"bench"` |
| 18 |
BenchTime string `json:"benchTime"` |
| 19 |
Rounds int `json:"rounds"` |
| 20 |
Layouts int `json:"layouts"` |
| 21 |
// Group, Shard and Shards select the benchmark functions measured: see benchmarkGroup and shardFuncs. |
| 22 |
Group string `json:"group"` |
| 23 |
Shard int `json:"shard"` |
| 24 |
Shards int `json:"shards"` |
| 25 |
} |
| 26 |
|
| 27 |
// machine identifies the environment in which a measurement is comparable with another one. |
| 28 |
// ns/op measured on a different machine must not be used as the baseline. |
| 29 |
type machine struct { |
| 30 |
Hostname string `json:"hostname"` |
| 31 |
GOOS string `json:"goos"` |
| 32 |
GOARCH string `json:"goarch"` |
| 33 |
NumCPU int `json:"numCpu"` |
| 34 |
GoVersion string `json:"goVersion"` |
| 35 |
} |
| 36 |
|
| 37 |
type baselineKey struct { |
| 38 |
Config benchConfig `json:"config"` |
| 39 |
Machine machine `json:"machine"` |
| 40 |
// BenchHash is the hash of the benchmark directory of the working tree, |
| 41 |
// because the benchmarks of the working tree are run against the base commit. |
| 42 |
BenchHash string `json:"benchHash"` |
| 43 |
} |
| 44 |
|
| 45 |
// verdictKey doesn't contain the machine because a verdict is relative: |
| 46 |
// both sides were measured on the same machine, whichever it was. |
| 47 |
type verdictKey struct { |
| 48 |
Config benchConfig `json:"config"` |
| 49 |
Attempts int `json:"attempts"` |
| 50 |
Tolerance float64 `json:"tolerance"` |
| 51 |
// SingleTolerance is the tolerance of a single benchmark, and Tolerance is the one of the mean. |
| 52 |
SingleTolerance float64 `json:"singleTolerance"` |
| 53 |
} |
| 54 |
|
| 55 |
type baseline struct { |
| 56 |
Commit string `json:"commit"` |
| 57 |
Key baselineKey `json:"key"` |
| 58 |
Results measurement `json:"results"` |
| 59 |
} |
| 60 |
|
| 61 |
type verdict struct { |
| 62 |
Head string `json:"head"` |
| 63 |
Base string `json:"base"` |
| 64 |
Key verdictKey `json:"key"` |
| 65 |
// MeanDeltaPercent is how much slower the head is than the base on average, in percent. |
| 66 |
MeanDeltaPercent float64 `json:"meanDeltaPercent"` |
| 67 |
Benchmarks []benchResult `json:"benchmarks"` |
| 68 |
} |
| 69 |
|
| 70 |
type cache struct { |
| 71 |
dir string |
| 72 |
// readable is false when cached data must be ignored and measured again. |
| 73 |
readable bool |
| 74 |
} |
| 75 |
|
| 76 |
func keyHash(key any) (string, error) { |
| 77 |
b, err := json.Marshal(key) |
| 78 |
if err != nil { |
| 79 |
return "", err |
| 80 |
} |
| 81 |
sum := sha256.Sum256(b) |
| 82 |
return hex.EncodeToString(sum[:8]), nil |
| 83 |
} |
| 84 |
|
| 85 |
func (c *cache) baselinePath(commit string, key baselineKey) (string, error) { |
| 86 |
hash, err := keyHash(key) |
| 87 |
if err != nil { |
| 88 |
return "", err |
| 89 |
} |
| 90 |
return filepath.Join(c.dir, "baseline", fmt.Sprintf("%s-%s.json", commit, hash)), nil |
| 91 |
} |
| 92 |
|
| 93 |
func (c *cache) verdictPath(head, base string, key verdictKey) (string, error) { |
| 94 |
hash, err := keyHash(key) |
| 95 |
if err != nil { |
| 96 |
return "", err |
| 97 |
} |
| 98 |
return filepath.Join(c.dir, "verdict", fmt.Sprintf("%s-%s-%s.json", head, base, hash)), nil |
| 99 |
} |
| 100 |
|
| 101 |
// load reads the cached value into v. It returns false if the value is not cached. |
| 102 |
func (c *cache) load(path string, v any) (bool, error) { |
| 103 |
if !c.readable { |
| 104 |
return false, nil |
| 105 |
} |
| 106 |
b, err := os.ReadFile(path) |
| 107 |
if errors.Is(err, fs.ErrNotExist) { |
| 108 |
return false, nil |
| 109 |
} |
| 110 |
if err != nil { |
| 111 |
return false, err |
| 112 |
} |
| 113 |
if err := json.Unmarshal(b, v); err != nil { |
| 114 |
return false, fmt.Errorf("failed to decode %s: %w", path, err) |
| 115 |
} |
| 116 |
return true, nil |
| 117 |
} |
| 118 |
|
| 119 |
func (c *cache) store(path string, v any) error { |
| 120 |
b, err := json.MarshalIndent(v, "", " ") |
| 121 |
if err != nil { |
| 122 |
return err |
| 123 |
} |
| 124 |
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 125 |
return err |
| 126 |
} |
| 127 |
// write to a temporary file first so that an interrupted run never leaves a broken cache. |
| 128 |
tmp, err := os.CreateTemp(filepath.Dir(path), "tmp-*") |
| 129 |
if err != nil { |
| 130 |
return err |
| 131 |
} |
| 132 |
defer os.Remove(tmp.Name()) |
| 133 |
if _, err := tmp.Write(b); err != nil { |
| 134 |
tmp.Close() |
| 135 |
return err |
| 136 |
} |
| 137 |
if err := tmp.Close(); err != nil { |
| 138 |
return err |
| 139 |
} |
| 140 |
return os.Rename(tmp.Name(), path) |
| 141 |
} |
| 142 |
|
| 143 |
func (c *cache) loadBaseline(commit string, key baselineKey) (*baseline, error) { |
| 144 |
path, err := c.baselinePath(commit, key) |
| 145 |
if err != nil { |
| 146 |
return nil, err |
| 147 |
} |
| 148 |
var v baseline |
| 149 |
found, err := c.load(path, &v) |
| 150 |
if err != nil || !found { |
| 151 |
return nil, err |
| 152 |
} |
| 153 |
return &v, nil |
| 154 |
} |
| 155 |
|
| 156 |
func (c *cache) storeBaseline(v *baseline) error { |
| 157 |
path, err := c.baselinePath(v.Commit, v.Key) |
| 158 |
if err != nil { |
| 159 |
return err |
| 160 |
} |
| 161 |
return c.store(path, v) |
| 162 |
} |
| 163 |
|
| 164 |
func (c *cache) loadVerdict(head, base string, key verdictKey) (*verdict, error) { |
| 165 |
path, err := c.verdictPath(head, base, key) |
| 166 |
if err != nil { |
| 167 |
return nil, err |
| 168 |
} |
| 169 |
var v verdict |
| 170 |
found, err := c.load(path, &v) |
| 171 |
if err != nil || !found { |
| 172 |
return nil, err |
| 173 |
} |
| 174 |
return &v, nil |
| 175 |
} |
| 176 |
|
| 177 |
func (c *cache) storeVerdict(v *verdict) error { |
| 178 |
path, err := c.verdictPath(v.Head, v.Base, v.Key) |
| 179 |
if err != nil { |
| 180 |
return err |
| 181 |
} |
| 182 |
return c.store(path, v) |
| 183 |
} |