liveBook · Manning

Criterion_group macro

As explained in Refactoring to Rust

The criterion_group macro defines a group of Criterion benchmark functions sharing a configuration.

The criterion_group macro defines a named collection of Criterion benchmark functions that run with shared configuration. Each group can use the default configuration or specified overrides, and criterion_main can run one or more groups. section 6.5

Registering a benchmark group

criterion_main!(python_vs_rust); criterion_group!(python_vs_rust, bench_fn); fn bench_fn(c: &mut Criterion) { c.bench_function("u8", |b| { b.iter(|| black_box(3u8 + 4)); }); } section 6.5

Listing 6.13 Basic benchmark example in benches/py-vs-rust.rs
use criterion::{black_box, criterion_group, criterion_main, Criterion};

criterion_main!(python_vs_rust);
criterion_group!(python_vs_rust, bench_fn);

fn bench_fn(c: &mut Criterion) {
  c.bench_function("u8", |b| {
    b.iter(|| {
      black_box(3u8 + 4);
    });
  });

  c.bench_function("u128", |b| {
    b.iter(|| {
      black_box(3u128 + 4);
    });
  });
}

Inspect the complete benchmark file to see how criterion_group connects bench_fn with individual benchmark functions. section 6.5

How Refactoring to Rust covers it

How deep it goes
Explains group registration, configuration sharing, and the required matching function name, but does not detail macro expansion internals.
Examples
Uses python_vs_rust, bench_fn, and u8/u128 benchmarks in a complete benchmark-file example.
What you can do after
Build it from scratch by registering benchmark functions in a Criterion group and linking that group to criterion_main.
How it connects
Connects Criterion benchmark functions with criterion_main, Criterion, Bencher, and black_box.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.