Struct criterion::Benchmark

source ·
pub struct Benchmark { /* private fields */ }
Expand description

Structure representing a benchmark (or group of benchmarks) which takes no parameters.

Implementations§

source§

impl Benchmark

source

pub fn sample_size(self, n: usize) -> 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 2.

§Panics

Panics if set to zero or one.

source

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

Changes the warm up time for this benchmark

§Panics

Panics if the input duration is zero

source

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

Changes the target measurement time for this benchmark. Criterion will attempt to spent approximately this amount of time measuring the benchmark. With a longer time, the measurement will become more resilient to transitory peak loads caused by external programs.

§Panics

Panics if the input duration in zero

source

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

Changes the number of resamples for this benchmark

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(self, threshold: f64) -> Self

Changes the noise threshold for this benchmark

This threshold is used to decide if an increase of X% in the execution time is considered significant or should be flagged as noise

Note: A value of 0.02 is equivalent to 2%

§Panics

Panics is the threshold is set to a negative value

source

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

Changes the confidence level for this benchmark

The confidence level is used to calculate the confidence intervals of the estimated statistics

§Panics

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

source

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

Changes the significance level for this benchmark

The significance level is used for hypothesis testing

§Panics

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

source

pub fn plot_config(self, new_config: PlotConfiguration) -> Self

Changes the plot configuration for this benchmark.

source

pub fn new<S, F>(id: S, f: F) -> Benchmark
where S: Into<String>, F: FnMut(&mut Bencher) + 'static,

Create a new benchmark group and adds the given function to it.

§Example

fn bench(c: &mut Criterion) {
    // One-time setup goes here
    c.bench(
        "my_group",
        Benchmark::new("my_function", |b| b.iter(|| {
            // Code to benchmark goes here
        })),
    );
}

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

pub fn new_external<S>(id: S, program: Command) -> Benchmark
where S: Into<String>,

👎Deprecated since 0.2.6: External program benchmarks were rarely used and are awkward to maintain, so they are scheduled for deletion in 0.3.0

Create a new benchmark group and add the given program to it.

The external program must:

  • Read the number of iterations from stdin
  • Execute the routine to benchmark that many times
  • Print the elapsed time (in nanoseconds) to stdout
// Example of an external program that implements this protocol

fn main() {
    let stdin = io::stdin();
    let ref mut stdin = stdin.lock();

    // For each line in stdin
    for line in stdin.lines() {
        // Parse line as the number of iterations
        let iters: u64 = line.unwrap().trim().parse().unwrap();

        // Setup

        // Benchmark
        let start = Instant::now();
        // Execute the routine "iters" times
        for _ in 0..iters {
            // Code to benchmark goes here
        }
        let elapsed = start.elapsed();

        // Teardown

        // Report elapsed time in nanoseconds to stdout
        println!("{}", elapsed.to_nanos());
    }
}
source

pub fn with_function<S, F>(self, id: S, f: F) -> Benchmark
where S: Into<String>, F: FnMut(&mut Bencher) + 'static,

Add a function to the benchmark group.

§Example:
Benchmark::new("return 10", |b| b.iter(|| 10))
    .with_function("return 20", |b| b.iter(|| 20));
source

pub fn with_program<S>(self, id: S, program: Command) -> Benchmark
where S: Into<String>,

👎Deprecated since 0.2.6: External program benchmarks were rarely used and are awkward to maintain, so they are scheduled for deletion in 0.3.0

Add an external program to the benchmark group.

§Example:
Benchmark::new("internal", |b| b.iter(|| 10))
    .with_program("external", Command::new("my_external_benchmark"));
source

pub fn throughput(self, throughput: Throughput) -> Benchmark

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

Benchmark::new("strlen", |b| b.iter(|| "foo".len()))
    .throughput(Throughput::Bytes(3));

Trait Implementations§

Auto Trait Implementations§

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, 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.