Documentation
¶
Overview ¶
Package k6bench runs k6 scenarios as Go benchmarks.
A k6 script becomes a testing.B benchmark: k6 metrics are reported through b.ReportMetric in benchstat-compatible units, k6 checks and thresholds become test failures, and parameter grids materialize as sub-benchmarks whose names carry the full configuration.
func BenchmarkAPI(b *testing.B) {
k6bench.Run(b, k6bench.Script{Path: "script.js"})
}
Each measurement runs a k6 binary — the K6 option, the K6BENCH_K6 environment variable, or k6 on PATH — with b.N mapped to k6 iterations, so ns/op is wall time per k6 iteration and benchstat can compare runs directly. k6bench imports nothing of k6: the measurement is parsed from the run's json output stream, which is also kept in the benchmark's artifact directory as the run's complete evidence trail. Scripts that use xk6 extensions need a binary built with them (xk6 build); outputs the binary was built with are reachable via Output.
Index ¶
- func Grid(b *testing.B, s Script, axes Axes, opts ...Option)
- func Run(b *testing.B, s Script, opts ...Option)
- type Axes
- type Axis
- type Mode
- type Option
- func AsDeclared() Option
- func Bytes(metric string) Option
- func CheckMode(m Mode) Option
- func HigherIsBetter(metrics ...string) Option
- func K6(path string) Option
- func K6Args(args ...string) Option
- func LowerIsBetter(metrics ...string) Option
- func Metrics(names ...string) Option
- func OmitTime() Option
- func Output(spec string) Option
- func OutputEnv(key, value string) Option
- func Percentiles(p ...float64) Option
- func Setup(f func() error) Option
- func SetupEnv(f func() (map[string]string, error)) Option
- func TagMetrics(metric, tagKey string, values ...string) Option
- func Teardown(f func() error) Option
- func ThresholdMode(m Mode) Option
- func VUs(n int) Option
- func Warmup(n int) Option
- type Script
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Axes ¶
type Axes []Axis
Axes is an ordered set of grid dimensions. Sub-benchmark names list every axis in declaration order, so a configuration can never pool with another under one name.
type Option ¶
type Option func(*config)
Option configures Run and Grid.
func AsDeclared ¶
func AsDeclared() Option
AsDeclared runs the script's own scenarios exactly as written instead of mapping b.N to iterations. The scenario executes once per benchmark; run with -benchtime 1x. Metrics are reported in absolute units, and dropped_iterations is always reported.
func Bytes ¶
Bytes designates the counter whose sum is passed to b.SetBytes, making benchstat print MB/s. Never auto-detected.
func HigherIsBetter ¶
HigherIsBetter declares that larger values of the named k6 metrics are better (throughput-like). Every reported unit derived from them gets a "Unit <name> better=higher" metadata line, so benchstat colors their deltas correctly. Undeclared metrics get no polarity (except time-valued units, which are better=lower automatically).
func K6 ¶
K6 sets the k6 binary to run. The default is the K6BENCH_K6 environment variable, then k6 on PATH. Scripts that use xk6 extensions need a binary built with them.
func K6Args ¶
K6Args appends raw arguments to the child's k6 run invocation. Escape hatch; anything load-bearing deserves a real Option.
func LowerIsBetter ¶
LowerIsBetter declares that smaller values of the named k6 metrics are better (latency- or error-like). See HigherIsBetter.
func Metrics ¶
Metrics restricts reporting to the named metrics. The default reports every custom (script- or extension-defined) metric.
func OmitTime ¶
func OmitTime() Option
OmitTime blanks the ns/op column (testing omits a zero ns/op from the result line). Use when wall time per iteration is not the quantity under study and would invite misreading — the script's own metrics remain the reported observables. AsDeclared implies it.
func Output ¶
Output adds a k6 output to the run, in k6 --out syntax: "csv=out.csv", or "name=arg" for an output extension the k6 binary was built with. The json output k6bench measures from always runs; outputs are additive.
func OutputEnv ¶
OutputEnv passes one environment variable through to the k6 process, for outputs configured by environment. The k6 env is otherwise minimal by design: the script sees only Script.Env, and nothing the benchmark did not declare leaks in.
func Percentiles ¶
Percentiles sets which trend percentiles are reported (default 50, 99).
func Setup ¶
Setup registers a function run before the k6 child starts, outside the timer. Use it to start servers the script targets.
func SetupEnv ¶
SetupEnv registers a function run before each k6 run, after Setup, whose returned variables are merged over Script.Env for that run (the caller's map is not modified). Use it for values that exist only once setup has run — a listen address, a connection ticket — so the script receives them through __ENV like any declared input. The merged environment is also what k6 inspect sees.
func TagMetrics ¶
TagMetrics additionally reports metric broken down by a declared tag key. Values are declared, never discovered: an observed undeclared value fails the benchmark, and a declared value with zero samples is fatal (the anti-vacuous rule applies per tag value).
func ThresholdMode ¶
ThresholdMode sets how crossed k6 thresholds are treated (default Fail).
func VUs ¶
VUs sets the VU count for the injected shared-iterations scenario (default 1). Ignored under AsDeclared.
func Warmup ¶
Warmup runs n extra iterations before the measured b.N. The collector discards everything they produce — metrics, checks, and the measurement window all start after them — so cold-start effects (goja warmup, first connections) stay out of the numbers. A check that fails only during warmup is therefore not reported; keep n small. Incompatible with AsDeclared.