Help us shape the future of Go! Take the 2026 Go Developer Survey

perfgate

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package perfgate compares benchmark results with performance thresholds.

It reads two kinds of results: `go test -bench` output (benchmark name, iteration count, then value/unit pairs) and the JSON lines written by bench/k8s.sh (RESULTS). Thresholds live in a JSON file of named profiles (bench/thresholds.json), so CI runners and a developer machine can be held to different, documented limits.

Index

Constants

View Source
const (
	ExitOK    = 0
	ExitFail  = 1 // a threshold was exceeded or a result is missing
	ExitUsage = 2 // bad flags, unreadable input or invalid thresholds
)

Exit codes of Run.

Variables

This section is empty.

Functions

func ReadK8s

func ReadK8s(r io.Reader) (map[string]float64, error)

ReadK8s returns the numeric fields of the last JSON line of a bench/k8s.sh RESULTS file (the script appends one line per run).

func Run

func Run(args []string, stdout, stderr io.Writer) int

Run is the perfgate command line (cmd/perfgate and bench/check.sh):

perfgate [-thresholds bench/thresholds.json] [-profile plan]
         [-k8s results.jsonl] [-bench out.txt ...] [-partial]
         [-summary file] [-json file] [bench-output.txt ...]

It prints one line per check and exits ExitFail when any check fails.

Types

type BenchResults

type BenchResults map[string]map[string][]float64

BenchResults maps benchmark name to unit to every reported value.

func ParseBench

func ParseBench(r io.Reader) (BenchResults, error)

ParseBench reads `go test -bench` output; non-benchmark lines are skipped. Repeated runs (-count) of a benchmark accumulate.

func (BenchResults) Merge

func (r BenchResults) Merge(other BenchResults)

Merge adds the values of other to r.

func (BenchResults) Value

func (r BenchResults) Value(name, unit string) (float64, bool)

Value returns the median of the values reported for name and unit.

type Bound

type Bound struct {
	Min *Limit `json:"min,omitempty"`
	Max *Limit `json:"max,omitempty"`
}

Bound is an inclusive range; either side may be absent.

type Check

type Check struct {
	Source  string  // "k8s" or "bench"
	Name    string  // benchmark name, or "k8s"
	Metric  string  // unit or RESULTS field
	Value   float64 // median measured value
	Min     float64
	Max     float64
	HasMin  bool
	HasMax  bool
	Missing bool // no result for a configured limit
	OK      bool
}

Check is the outcome of one metric against its bound.

type Config

type Config struct {
	Profiles map[string]Profile `json:"profiles"`
}

Config is the thresholds file.

func ParseConfig

func ParseConfig(r io.Reader) (*Config, error)

ParseConfig decodes a thresholds file. Top-level keys other than "profiles" (such as "_doc") are ignored.

func (*Config) Check

func (c *Config) Check(profile string, k8s map[string]float64, bench BenchResults) (*Report, error)

Check compares results with the limits of profile. A nil k8s or bench argument skips that source; a configured limit without a result in a checked source fails, so a skipped or crashed benchmark cannot pass.

type Limit

type Limit struct {
	Number   float64
	Duration time.Duration
}

Limit is one threshold value: a plain number in the metric's unit, or a Go duration string ("150ms") converted to the metric's time unit.

func (*Limit) In

func (l *Limit) In(unit string) (float64, error)

In returns the limit expressed in unit. A duration converts to nanoseconds for "ns/op", milliseconds for units ending in "ms" and seconds for "s" or units ending in "_s"; any other unit takes plain numbers only.

func (*Limit) String

func (l *Limit) String() string

func (*Limit) UnmarshalJSON

func (l *Limit) UnmarshalJSON(b []byte) error

UnmarshalJSON accepts a number or a duration string.

type Profile

type Profile struct {
	// K8s maps a bench/k8s.sh RESULTS field (full_s, noop_s, ...) to its bound.
	K8s map[string]Bound `json:"k8s"`
	// Bench maps a benchmark name (without the -GOMAXPROCS suffix) to its
	// metrics; a metric key is a unit ("ns/op", "go-edit-ms") or a
	// path.Match pattern over units ("*-ms").
	Bench map[string]map[string]Bound `json:"bench"`
}

Profile holds the limits of one environment.

type Report

type Report struct {
	Profile string
	Checks  []Check
}

Report is the result of checking one profile.

func (*Report) DropAbsent

func (r *Report) DropAbsent(bench BenchResults)

DropAbsent removes bench checks of benchmarks that have no result at all in bench, for runs of a subset of the gates. Missing metrics of a benchmark that did run still fail.

func (*Report) Failures

func (r *Report) Failures() []Check

Failures returns the failed checks.

func (*Report) OK

func (r *Report) OK() bool

OK reports whether every check passed.

func (*Report) WriteMarkdown

func (r *Report) WriteMarkdown(w io.Writer)

WriteMarkdown renders the report as a table (for $GITHUB_STEP_SUMMARY).

func (*Report) WriteText

func (r *Report) WriteText(w io.Writer)

WriteText prints one line per check and a summary naming every failure.

Directories

Path Synopsis
cmd
perfgate command
Command perfgate checks benchmark results against bench/thresholds.json.
Command perfgate checks benchmark results against bench/thresholds.json.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL