Documentation
¶
Overview ¶
Package bench is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.
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.
Example ¶
package main
import (
"fmt"
"github.com/nsgo-dev/game/pkg/tooling/bench"
)
func main() {
fmt.Println(bench.DefaultLimit)
}
Output: 25
Index ¶
Examples ¶
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 ¶
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
// CPU is the processor the times were measured on, as go test's "cpu:" header names it, or "" when the baseline does not say. When it is set, a run on another processor, or one that does not name its processor, does not compare ns/op; B/op and allocs/op, which do not depend on the processor, are compared on every run.
CPU 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 CPU, the limits, then one line per benchmark in name order. The note and the CPU are left out when empty.
version: 1
note: Update with go tool game bench -update.
cpu: AMD EPYC 9R45 96-Core Processor
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 Reference ¶
type Reference struct {
// Name says what the reference is, such as its commit; reports print it.
Name string
// Run is the reference's measurement.
Run Run
// Baseline is the baseline file the reference's commit stores, or nil when it has none.
Baseline *Baseline
}
Reference is the same benchmarks of another commit, usually the one a change is based on, measured on the same machine as the run being checked and interleaved with it, so that both see the same processor and the same load from other tenants of the machine.
type Report ¶
type Report struct {
// Limits are the limits the run was held to.
Limits Limits
// BaselineCPU and CPU are the processors the baseline and the run name; see [Baseline.CPU].
BaselineCPU, CPU string
// TimesNotCompared reports that ns/op was not held to its limit because the baseline names a CPU and the run names another one or none. ns/op and its change are still reported but never count as a regression.
TimesNotCompared bool
// Reference names the [Reference] a report made by [EvaluateAgainst] held ns/op to, or is "" when ns/op was held to the baseline's stored times.
Reference string
// Results are the baseline's benchmarks in name order; a strict report also lists each benchmark of New, as an unbaselined failure.
Results []Result
// New are the measured benchmarks the baseline does not hold, in name order; they neither pass nor fail unless Strict is set.
New []string
// Strict marks unbaselined measured benchmarks as failures.
Strict bool
}
Report is a benchmark run compared with a Baseline.
func Evaluate ¶
Evaluate compares run's benchmarks with b's: 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. ns/op is held to its limit only when b names no CPU or run names the same one, since times measured on different processors are not comparable.
func EvaluateAgainst ¶
EvaluateAgainst is Evaluate, or EvaluateStrict when strict is set, with ns/op held to its limit against ref's time for the same benchmark instead of b's stored time, on any CPU. When b and ref.Baseline both store the benchmark and their stored times differ, ref's time is scaled by their ratio, so a change that records a new cost for a benchmark in b is held to that cost. A benchmark ref did not measure is not held to a time limit and has Result.NoReferenceTime set. B/op and allocs/op are held to b's stored values, as Evaluate holds them.
Times measured on one machine in one job differ far less than times measured on different machines, even machines with the same processor, so this is the comparison to use on shared CI runners.
func EvaluateStrict ¶
EvaluateStrict is Evaluate that also fails every measured benchmark the baseline does not hold, listing each in Results as unbaselined, so a CI job that adds a benchmark must add its baseline in the same change.
func (Report) Failed ¶
Failed returns the number of benchmarks that regressed or were not measured and, in a strict report, the measured benchmarks the baseline does not hold.
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
// Unbaselined reports that the benchmark was measured but the baseline does not hold it; only [EvaluateStrict] lists such a benchmark in Results, as a failure.
Unbaselined 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
// NoReferenceTime reports that a report made by [EvaluateAgainst] did not hold ns/op to its limit because the reference did not measure the benchmark.
NoReferenceTime bool
}
Result is one baseline benchmark compared with its measurement.
type Run ¶
type Run struct {
// CPU is the processor the run names in go test's "cpu:" header line, or "" when it names none or its header lines name different processors.
CPU string
// Benchmarks are the median metrics of each benchmark, by name without its trailing -GOMAXPROCS suffix.
Benchmarks map[string]Metrics
}
Run is a benchmark run read by ParseOutput.
func ParseOutput ¶
ParseOutput reads benchmark result lines and returns the median metrics of each benchmark, by name without its trailing -GOMAXPROCS suffix, with the processor go test's "cpu:" header line names.
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.
A trailing -N suffix (N >= 2, as Go's testing package formats GOMAXPROCS > 1) is stripped only when every parsed benchmark line in the input ends with the same -N suffix and verbose output does not declare those raw names verbatim. Lines with different full names are never merged.