criterion

Struct BenchmarkGroup

Source
pub struct BenchmarkGroup<'a, M: Measurement> { /* private fields */ }
Available on non-crate feature codspeed only.
Expand description

Structure used to group together a set of related benchmarks, along with custom configuration settings for groups of benchmarks. All benchmarks performed using a benchmark group will be grouped together in the final report.

§Examples:

use self::criterion::*;
use std::time::Duration;

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 ));

    // It's recommended to call group.finish() explicitly at the end, but if you don't it will
    // be called automatically when the group is dropped.
    group.finish();
}

fn bench_nested(c: &mut Criterion) {
    let mut group = c.benchmark_group("My Second Group");
    // We can override the configuration on a per-group level
    group.measurement_time(Duration::from_secs(1));

    // We can also use loops to define multiple benchmarks, even over multiple dimensions.
    for x in 0..3 {
        for y in 0..3 {
            let point = (x, y);
            let parameter_string = format!("{} * {}", x, y);
            group.bench_with_input(BenchmarkId::new("Multiply", parameter_string), &point,
                |b, (p_x, p_y)| b.iter(|| p_x * p_y));
        }
    }

    group.finish();
}

fn bench_throughput(c: &mut Criterion) {
    let mut group = c.benchmark_group("Summation");

    for size in [1024, 2048, 4096].iter() {
        // Generate input of an appropriate size...
        let input = vec![1u64, *size];

        // We can use the throughput function to tell Criterion.rs how large the input is
        // so it can calculate the overall throughput of the function. If we wanted, we could
        // even change the benchmark configuration for different inputs (eg. to reduce the
        // number of samples for extremely large and slow inputs) or even different functions.
        group.throughput(Throughput::Elements(*size as u64));

        group.bench_with_input(BenchmarkId::new("sum", *size), &input,
            |b, i| b.iter(|| i.iter().sum::<u64>()));
        group.bench_with_input(BenchmarkId::new("fold", *size), &input,
            |b, i| b.iter(|| i.iter().fold(0u64, |a, b| a + b)));
    }

    group.finish();
}

criterion_group!(benches, bench_simple, bench_nested, bench_throughput);
criterion_main!(benches);

Implementations§

Source§

impl<'a, M: Measurement> BenchmarkGroup<'a, M>

Source

pub fn sample_size(&mut self, n: usize) -> &mut Self

Changes the size of the sample for this benchmark

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.

Source

pub fn warm_up_time(&mut self, dur: Duration) -> &mut Self

Changes the warm up time for this benchmark

§Panics

Panics if the input duration is zero

Source

pub fn measurement_time(&mut self, dur: Duration) -> &mut Self

Changes the target measurement time for this benchmark group.

Criterion will attempt to spent approximately this amount of time measuring each benchmark on a best-effort basis. If it is not possible to perform the measurement in the requested time (eg. because each iteration of the benchmark is long) then Criterion will spend as long as is needed to collect the desired number of samples. With a longer time, the measurement will become more resilient to interference from other programs.

§Panics

Panics if the input duration is zero

Source

pub fn nresamples(&mut self, n: usize) -> &mut Self

Changes the number of resamples for this benchmark group

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

Source

pub fn noise_threshold(&mut self, threshold: f64) -> &mut Self

Changes the noise threshold for benchmarks in this group. The noise threshold is used to filter out small changes in performance from one run to the next, 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

Source

pub fn confidence_level(&mut self, cl: f64) -> &mut Self

Changes the confidence level for benchmarks in this group. 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

Source

pub fn significance_level(&mut self, sl: f64) -> &mut Self

Changes the significance level for benchmarks in this group. 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

Source

pub fn throughput(&mut self, throughput: Throughput) -> &mut Self

Set the input size for this benchmark group. Used for reporting the throughput.

Source

pub fn sampling_mode(&mut self, new_mode: SamplingMode) -> &mut Self

Set the sampling mode for this benchmark group.

Source

pub fn bench_function<ID: IntoBenchmarkId, F>( &mut self, id: ID, f: F, ) -> &mut Self
where F: FnMut(&mut Bencher<'_, M>),

Benchmark the given parameterless function inside this benchmark group.

Source

pub fn bench_with_input<ID: IntoBenchmarkId, F, I>( &mut self, id: ID, input: &I, f: F, ) -> &mut Self
where F: FnMut(&mut Bencher<'_, M>, &I), I: ?Sized,

Benchmark the given parameterized function inside this benchmark group.

Source

pub fn finish(self)

Consume the benchmark group and generate the summary reports for the group.

It is recommended to call this explicitly, but if you forget it will be called when the group is dropped.

Trait Implementations§

Source§

impl<'a, M: Measurement> Drop for BenchmarkGroup<'a, M>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a, M> Freeze for BenchmarkGroup<'a, M>

§

impl<'a, M> !RefUnwindSafe for BenchmarkGroup<'a, M>

§

impl<'a, M> !Send for BenchmarkGroup<'a, M>

§

impl<'a, M> !Sync for BenchmarkGroup<'a, M>

§

impl<'a, M> Unpin for BenchmarkGroup<'a, M>

§

impl<'a, M> !UnwindSafe for BenchmarkGroup<'a, M>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize = _

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more