Documentation
¶
Overview ¶
Package bench keeps Go benchmark baselines in a versioned YAML text file, reads the results `go test -bench` and `nsgo test -bench` print, and reports the benchmarks that regressed past the baseline's limits. The game command's bench subcommand is built on it.
Index ¶
Constants ¶
const DefaultLimit = 25
DefaultLimit is the regression allowed for each metric, in percent, in a Baseline made by NewBaseline.
const FormatVersion = 1
FormatVersion is the version written to every Baseline and the only one Decode accepts.
Variables ¶
This section is empty.
Functions ¶
func ParseOutput ¶
ParseOutput reads benchmark result lines and returns the median metrics of each benchmark, by name without its trailing -GOMAXPROCS suffix.
It reads the lines `go test -bench -benchmem` prints, such as "BenchmarkRaycast-8 2000000 420.5 ns/op 0 B/op 0 allocs/op", and the same lines `nsgo test -bench` prints after a ✓ mark, and skips every other line. A line without ns/op is skipped; one without B/op or allocs/op counts them as 0.
Types ¶
type Baseline ¶
type Baseline struct {
// Version is the format version; it must equal FormatVersion.
Version int
// Note is free text kept with the file, such as how to update it.
Note string
// Limits are the allowed regressions.
Limits Limits
// Benchmarks are the baseline metrics by benchmark name.
Benchmarks map[string]Metrics
}
Baseline is the stored file a benchmark run is checked against: the regression limits and each benchmark's metrics, by benchmark name without its -GOMAXPROCS suffix.
func Decode ¶
Decode reads a Baseline from the YAML text Encode writes, including text a person has edited, and validates it. Unknown keys, a missing limit or metric, and trailing YAML documents are refused, and a file of another version is refused with an error naming that version.
func LoadFile ¶
LoadFile reads and decodes the Baseline at path. Its errors are *loaderr.Error values that name path, with the line of the problem and how to fix it.
func NewBaseline ¶
func NewBaseline() *Baseline
NewBaseline returns an empty Baseline of the current version with DefaultLimit for every metric.
func (*Baseline) Encode ¶
Encode validates b and writes it as deterministic YAML text: the version, the note, the limits, then one line per benchmark in name order.
version: 1
note: Update with go tool game bench -update.
max_regression_pct: {ns_per_op: 25, bytes_per_op: 25, allocs_per_op: 25}
benchmarks:
BenchmarkLoadAtlas: {ns_per_op: 300000, bytes_per_op: 7200, allocs_per_op: 59}
BenchmarkRaycast: {ns_per_op: 15000, bytes_per_op: 0, allocs_per_op: 0}
The same Baseline always encodes to the same bytes, and Decode followed by Encode reproduces a file Encode wrote byte for byte.
type Limits ¶
type Limits struct {
// NsPerOp is the allowed growth of the time per operation.
NsPerOp float64
// BytesPerOp is the allowed growth of the bytes allocated per operation.
BytesPerOp float64
// AllocsPerOp is the allowed growth of the allocations per operation.
AllocsPerOp float64
}
Limits are how much each metric may grow over its baseline, in percent, before a benchmark counts as regressed.
type Metrics ¶
type Metrics struct {
// NsPerOp is the time per operation in nanoseconds.
NsPerOp float64
// BytesPerOp is the memory allocated per operation in bytes.
BytesPerOp float64
// AllocsPerOp is the number of allocations per operation.
AllocsPerOp float64
}
Metrics are one benchmark's per-operation costs: the median of the runs measured, or the stored baseline.
type Report ¶
type Report struct {
// Limits are the limits the run was held to.
Limits Limits
// Results are the baseline's benchmarks in name order.
Results []Result
// New are the measured benchmarks the baseline does not hold, in name order; they neither pass nor fail.
New []string
}
Report is a benchmark run compared with a Baseline.
func Evaluate ¶
Evaluate compares measured with b's benchmarks: a benchmark fails when it was not measured, when a measured metric is NaN, infinite or negative, or when a metric grew past b.Limits.
func (Report) Failed ¶
Failed returns the number of baseline benchmarks that regressed or were not measured.
type Result ¶
type Result struct {
// Name is the benchmark's name.
Name string
// Missing reports that the benchmark is in the baseline but was not measured.
Missing bool
// Baseline and Measured are the stored and the measured metrics.
Baseline, Measured Metrics
// Delta is each metric's change from the baseline in percent; +Inf when the baseline is 0 and the measurement is not.
Delta Metrics
// Regressions describe each metric that grew past its limit.
Regressions []string
}
Result is one baseline benchmark compared with its measurement.