Expand description
kevy-bench — a pure-Rust micro-benchmark harness. Zero dependencies.
kevy is 0-dependency by policy, so we cannot pull criterion. This is the
self-hosted equivalent, used by two callers:
- per-crate exploration benches (
examples/bench_*.rs) — compare two implementations head-to-head and print a speedup ratio. This ratio is the evidence the dep/std self-host evaluation is gated on (“if the candidate isn’t ≥ N× the incumbent, don’t self-host it”). - per-crate regression gates (
tests/perf_gate.rs) — assert a hot path stays under a documented budget (seetime_median).
§Why the ratio survives a loaded host
The campaign that birthed this crate could not get a clean full-system
throughput number because the dev host is permanently busy. The escape is to
measure components, not the whole server: run the variants back-to-back
in one process. Both pay the same contention in the same window, so the
absolute ns drifts with load but the ratio between them holds. Treat
median_ns as relative, not absolute, unless the host is known idle.
§Method
Each sample times inner iterations of the closure and divides, so the
per-op figure isn’t swamped by Instant::now() overhead (~tens of ns) for
sub-microsecond work. We report the median sample (robust to the
occasional scheduler hiccup) plus p95 and min.
use kevy_bench::{bench, black_box};
let s = bench(50, 1000, || { black_box(1u64 + black_box(2)); });
assert!(s.median_ns < 1_000); // trivial add is nanosecondsStructs§
Functions§
- bench
- Run
opand return per-operation timing stats. - black_
box - Re-exported so benches can fence the optimizer without importing
std::hint. An identity function that hints to the compiler to be maximally pessimistic about whatblack_boxcould do. - compare
- Print both timings and the candidate’s speedup over the baseline (by median).
- time_
median - Median wall-clock time of a single
opcall overitersruns.