pub struct Criterion<M: Measurement = WallTime> { /* private fields */ }
Expand description

The benchmark manager

Criterion lets you configure and execute benchmarks

Each benchmark consists of four phases:

  • Warm-up: The routine is repeatedly executed, to let the CPU/OS/JIT/interpreter adapt to the new load
  • Measurement: The routine is repeatedly executed, and timing information is collected into a sample
  • Analysis: The sample is analyzed and distilled into meaningful statistics that get reported to stdout, stored in files, and plotted
  • Comparison: The current sample is compared with the sample obtained in the previous benchmark.

Implementations

Changes the measurement for the benchmarks run with this runner. See the Measurement trait for more details

Changes the internal profiler for benchmarks run with this runner. See the Profiler trait for more details.

Set the plotting backend. By default, Criterion will use gnuplot if available, or plotters if not.

Panics if backend is PlottingBackend::Gnuplot and gnuplot is not available.

Changes the default size of the sample for benchmarks run with this runner.

A bigger sample should yield more accurate results if paired with a sufficiently large measurement time.

Sample size must be at least 10.

Panics

Panics if n < 10

Changes the default warm up time for benchmarks run with this runner.

Panics

Panics if the input duration is zero

Changes the default measurement time for benchmarks run with this runner.

With a longer time, the measurement will become more resilient to transitory peak loads caused by external programs

Note: If the measurement time is too “low”, Criterion will automatically increase it

Panics

Panics if the input duration in zero

Changes the default number of resamples for benchmarks run with this runner.

Number of resamples to use for the bootstrap

A larger number of resamples reduces the random sampling errors, which are inherent to the bootstrap method, but also increases the analysis time

Panics

Panics if the number of resamples is set to zero

Changes the default noise threshold for benchmarks run with this runner. The noise threshold is used to filter out small changes in performance, even if they are statistically significant. Sometimes benchmarking the same code twice will result in small but statistically significant differences solely because of noise. This provides a way to filter out some of these false positives at the cost of making it harder to detect small changes to the true performance of the benchmark.

The default is 0.01, meaning that changes smaller than 1% will be ignored.

Panics

Panics if the threshold is set to a negative value

Changes the default confidence level for benchmarks run with this runner. The confidence level is the desired probability that the true runtime lies within the estimated confidence interval. The default is 0.95, meaning that the confidence interval should capture the true value 95% of the time.

Panics

Panics if the confidence level is set to a value outside the (0, 1) range

Changes the default significance level for benchmarks run with this runner. This is used to perform a hypothesis test to see if the measurements from this run are different from the measured performance of the last run. The significance level is the desired probability that two measurements of identical code will be considered ‘different’ due to noise in the measurements. The default value is 0.05, meaning that approximately 5% of identical benchmarks will register as different due to noise.

This presents a trade-off. By setting the significance level closer to 0.0, you can increase the statistical robustness against noise, but it also weakens Criterion.rs’ ability to detect small but real changes in the performance. By setting the significance level closer to 1.0, Criterion.rs will be more able to detect small true changes, but will also report more spurious differences.

See also the noise threshold setting.

Panics

Panics if the significance level is set to a value outside the (0, 1) range

Enables plotting

Disables plotting

👎 Deprecated since 0.3.4:

No longer useful; since the plotters backend is available Criterion.rs can always generate plots

Return true if generation of the plots is possible.

Names an explicit baseline and enables overwriting the previous results.

Names an explicit baseline and disables overwriting the previous results.

Filters the benchmarks. Only benchmarks with names that contain the given string will be executed.

Override whether the CLI output will be colored or not. Usually you would use the --color CLI argument, but this is available for programmmatic use as well.

Configure this criterion struct based on the command-line arguments to this process.

Return a benchmark group. All benchmarks performed using a benchmark group will be grouped together in the final report.

Examples:
#[macro_use] extern crate criterion;
use self::criterion::*;

fn bench_simple(c: &mut Criterion) {
    let mut group = c.benchmark_group("My Group");

    // Now we can perform benchmarks with this group
    group.bench_function("Bench 1", |b| b.iter(|| 1 ));
    group.bench_function("Bench 2", |b| b.iter(|| 2 ));
    
    group.finish();
}
criterion_group!(benches, bench_simple);
criterion_main!(benches);
Panics:

Panics if the group name is empty

Benchmarks a function. For comparing multiple functions, see benchmark_group.

Example
#[macro_use] extern crate criterion;
use self::criterion::*;

fn bench(c: &mut Criterion) {
    // Setup (construct data, allocate memory, etc)
    c.bench_function(
        "function_name",
        |b| b.iter(|| {
            // Code to benchmark goes here
        }),
    );
}

criterion_group!(benches, bench);
criterion_main!(benches);

Benchmarks a function with an input. For comparing multiple functions or multiple inputs, see benchmark_group.

Example
#[macro_use] extern crate criterion;
use self::criterion::*;

fn bench(c: &mut Criterion) {
    // Setup (construct data, allocate memory, etc)
    let input = 5u64;
    c.bench_with_input(
        BenchmarkId::new("function_name", input), &input,
        |b, i| b.iter(|| {
            // Code to benchmark using input `i` goes here
        }),
    );
}

criterion_group!(benches, bench);
criterion_main!(benches);

Trait Implementations

Creates a benchmark manager with the following default settings:

  • Sample size: 100 measurements
  • Warm-up time: 3 s
  • Measurement time: 5 s
  • Bootstrap size: 100 000 resamples
  • Noise threshold: 0.01 (1%)
  • Confidence level: 0.95
  • Significance level: 0.05
  • Plotting: enabled, using gnuplot if available or plotters if gnuplot is not available
  • No filter

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.