GitHubEmpezar

La referencia de API proviene de los comentarios de documentación de Go en el código fuente y está redactada en inglés.

package bench

import "github.com/nsgo-dev/game/pkg/tooling/bench"

Resumen

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.

Índice

Constantes

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.

Funciones

func ParseOutput(r io.Reader) (map[string]Metrics, error)

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.

func SaveFile(path string, b *Baseline) error

SaveFile writes b to path as the YAML text Encode produces, creating path's directory if needed.

Tipos

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(data []byte) (*Baseline, error)

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(path string) (*Baseline, error)

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() *Baseline

NewBaseline returns an empty Baseline of the current version with DefaultLimit for every metric.

func (b *Baseline) Encode() ([]byte, error)

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.

func (b *Baseline) Update(measured map[string]Metrics)

Update sets the baseline of every benchmark in measured to its measured metrics, leaving the others as they were.

func (b *Baseline) Validate() error

Validate checks that b has the supported version, positive finite limits, and benchmark names starting with "Benchmark" whose metrics are finite and not negative.

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 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 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(b *Baseline, measured map[string]Metrics) Report

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 (r Report) Failed() int

Failed returns the number of baseline benchmarks that regressed or were not measured.

func (r Report) Passed() bool

Passed reports whether the baseline holds at least one benchmark and every one of them passed.

func (r Report) String() string

String renders r as a table with a PASS or FAIL line per baseline benchmark, the reason under each failure, and the measured benchmarks the baseline lacks.

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.

func (r Result) Passed() bool

Passed reports whether the benchmark was measured and no metric grew past its limit.