#[bench]Expand description
Turns a function into a Criterion benchmark that runs with GPUI support.
The function takes a &mut gpui::BenchAppContext, and it is not async.
Put the work to measure in the body, and the macro measures it with
Criterion. Benchmarks with an inputs argument take the input as their
first parameter, before the context: fn bench(input: T, cx: &mut BenchAppContext).
The macro generates a function that takes &mut criterion::Criterion, so
register it with criterion_group! and criterion_main! in the benchmark
target. Each benchmark prints a GPUI report when it finishes.
§Arguments
fps = Nsets the frame rate that the report budgets against.Nmust be greater than zero. The default comes fromgpui::BenchReport.inputs = EXPRruns the benchmark once for each item ofEXPR, using Criterion’sbench_with_input.input_name = "..."sets the name of each input in the report. Requiresinputs. The default is the function name.group = "..."sets the name of the benchmark group. Requiresinputs. The default is the function name.sample_size = Nsets the number of Criterion samples for the group. Requiresinputs, andNmust be greater than zero.
§Dependencies
The benchmark crate needs criterion and gpui_platform as dev-dependencies,
because the generated code calls both. Enable the bench-support feature on
gpui, which provides BenchAppContext.
A benchmark that takes a StdRng parameter (by value, in any position) is run
once per seed, with the RNG seeded from it. Seeds are chosen exactly as for
#[gpui::test]: seed = N, seeds(...), and iterations = N take the same
meaning, and the SEED and ITERATIONS environment variables override them the
same way, so one SEED reproduces a test or a benchmark. Each seed is its own
Criterion benchmark, named <input>/seed-<seed> (or <function>/seed-<seed>
without inputs), and the RNG is rebuilt for every Criterion routine call so all
samples measure the same seed. The generated code seeds it through gpui’s own
rand, so the crate needs rand only to name StdRng in the signature.
#[gpui::bench(inputs = families(), iterations = 6)]
fn full_refresh(family: &Family, mut rng: StdRng, cx: &mut BenchAppContext) {
let tree = family.sample(&mut rng);
// ...
}The benchmark crate must add criterion and gpui_platform (with its
test-support feature) to its dev-dependencies and enable gpui’s bench
feature, since the generated code references all three.