Skip to main content

bench

Attribute Macro bench 

Source
#[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 = N sets the frame rate that the report budgets against. N must be greater than zero. The default comes from gpui::BenchReport.
  • inputs = EXPR runs the benchmark once for each item of EXPR, using Criterion’s bench_with_input.
  • input_name = "..." sets the name of each input in the report. Requires inputs. The default is the function name.
  • group = "..." sets the name of the benchmark group. Requires inputs. The default is the function name.
  • sample_size = N sets the number of Criterion samples for the group. Requires inputs, and N must 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.