iai_callgrind

Struct Bench

source
pub struct Bench {
    pub id: BenchmarkId,
    pub commands: Vec<Command>,
    pub config: Option<InternalBinaryBenchmarkConfig>,
    pub setup: Option<fn()>,
    pub teardown: Option<fn()>,
}
Available on crate feature default only.
Expand description

low level api only: This struct mirrors the #[bench] and #[benches] attribute of a crate::binary_benchmark

Fields§

§id: BenchmarkId

The BenchmarkId used to uniquely identify this benchmark within a BinaryBenchmark

§commands: Vec<Command>

All Commands

§config: Option<InternalBinaryBenchmarkConfig>

An optional BinaryBenchmarkConfig

This field stores the internal representation of the BinaryBenchmarkConfig. Use BinaryBenchmarkConfig::into to generate the internal configuration from a BinaryBenchmarkConfig

§setup: Option<fn()>

The setup function to be executed before the Command is executed

§teardown: Option<fn()>

The teardown function to be executed after the Command is executed

Implementations§

source§

impl Bench

source

pub fn new<T>(id: T) -> Self
where T: Into<BenchmarkId>,

Create a new Bench with a unique BenchmarkId

If the provided BenchmarkId is invalid, iai-callgrind exits with an error.

§Scope of uniqueness of the BenchmarkId

The id needs to be unique within the same BinaryBenchmark

§Examples

The BenchmarkId can be created from any &str-like

use iai_callgrind::Bench;

let bench = Bench::new("my_unique_id");

but you can also provide the BenchmarkId

use iai_callgrind::{Bench, BenchmarkId};

let bench = Bench::new(BenchmarkId::new("my_unique_id"));
source

pub fn config<T>(&mut self, config: T) -> &mut Self

Add a BinaryBenchmarkConfig for this Bench

A Bench without a BinaryBenchmarkConfig behaves like having specified the default BinaryBenchmarkConfig. This BinaryBenchmarkConfig overwrites the values of a BinaryBenchmarkConfig specified at a higher level. See there for more details.

§Examples
use iai_callgrind::{Bench, BinaryBenchmarkConfig};

let bench = Bench::new("some_id").config(BinaryBenchmarkConfig::default().env("FOO", "BAR"));
source

pub fn command<T>(&mut self, command: T) -> &mut Self
where T: Into<Command>,

Add a Command to this Bench

A Bench with multiple Commands behaves exactly as the #[benches] attribute

§Errors

It is an error if a Bench does not contain any Commands, so this method or Bench::commands has to be called at least once.

§Examples

Suppose the crate’s binary is called my-echo:

use iai_callgrind::{Bench, Command};

let bench = Bench::new("some_id")
    .command(Command::new(env!("CARGO_BIN_EXE_my-echo")))
    .clone();

assert_eq!(bench.commands.len(), 1);
source

pub fn commands<I, T>(&mut self, commands: T) -> &mut Self
where I: Into<Command>, T: IntoIterator<Item = I>,

Add multiple Commands to this Bench

See also Bench::command.

§Examples

Suppose the crate’s binary is called my-echo

use iai_callgrind::{Bench, Command};

let mut command = Command::new(env!("CARGO_BIN_EXE_my-echo"));

let echo_foo = command.clone().arg("foo").build();
let echo_bar = command.arg("bar").build();

let mut bench = Bench::new("some_id");
bench.commands([echo_foo, echo_bar]).clone();

assert_eq!(bench.commands.len(), 2);
source

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

Add a setup function to be executed before the Command is executed

This setup function overwrites the setup function of BinaryBenchmark. In the presence of a Sandbox, this function is executed in the sandbox.

§Examples
use iai_callgrind::Bench;

fn my_setup() {
    println!("Place everything in this function you want to be executed prior to the Command");
}

let mut bench = Bench::new("some_id");
bench.setup(my_setup);

assert!(bench.setup.is_some())

Overwrite the setup function from a BinaryBenchmark

use iai_callgrind::{Bench, BinaryBenchmark};
fn binary_benchmark_setup() {
    println!("setup in BinaryBenchmark")
}

fn bench_setup() {
    println!("setup in Bench")
}

BinaryBenchmark::new("bench_binary")
    .setup(binary_benchmark_setup)
    .bench(Bench::new("some_id").setup(bench_setup));
source

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

Add a teardown function to be executed after the Command is executed

This teardown function overwrites the teardown function of BinaryBenchmark. In the presence of a Sandbox, this function is executed in the sandbox.

§Examples
use iai_callgrind::Bench;

fn my_teardown() {
    println!(
        "Place everything in this function you want to be executed after the execution of the \
         Command"
    );
}

let mut bench = Bench::new("some_id");
bench.teardown(my_teardown);

assert!(bench.teardown.is_some())

Overwrite the teardown function from a BinaryBenchmark

use iai_callgrind::{Bench, BinaryBenchmark};
fn binary_benchmark_teardown() {
    println!("teardown in BinaryBenchmark")
}

fn bench_teardown() {
    println!("teardown in Bench")
}

BinaryBenchmark::new("bench_binary")
    .teardown(binary_benchmark_teardown)
    .bench(Bench::new("some_id").teardown(bench_teardown));
source

pub fn file<T>( &mut self, path: T, generator: fn(_: String) -> Result<Command, String>, ) -> Result<&mut Self, String>
where T: AsRef<Path>,

Add each line of a file as Command to this Bench using a generator function.

This method mirrors the file parameter of the #[benches] attribute as far as possible. In the low-level api you can achieve the same or more quickly yourself and this method exists for the sake of completeness (and convenience).

The file has to exist at the time you’re using this method and the file has to be encoded in UTF-8. The generator function tells us how to convert each line of the file into a Command.

§Notable differences to high-level api

If the file path in the high-level api is relative we interpret the path relative to the workspace root (and make it absolute). In this method we use the path AS IS.

§Errors

If the file is empty, cannot be opened for reading or a line in the file cannot be converted to a String. Also, the error from the generator is propagated. The errors containing the line number use a 0-indexed line number.

§Examples

Suppose your cargo’s binary is named my-echo and you want to convert a file with inputs benches/inputs into commands and each line is the only argument for your my-echo binary:

use iai_callgrind::{binary_benchmark_group, BinaryBenchmark, Bench};

binary_benchmark_group!(
    name = my_group;
    benchmarks = |group: &mut BinaryBenchmarkGroup| {
        group.binary_benchmark(BinaryBenchmark::new("some_id")
            .bench(Bench::new("bench_id")
                .file("benches/inputs", |line| {
                    Ok(iai_callgrind::Command::new(env!("CARGO_BIN_EXE_my-echo"))
                        .arg(line)
                        .build())
                }).unwrap()
            )
        )
    }
);

Trait Implementations§

source§

impl Clone for Bench

source§

fn clone(&self) -> Bench

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Bench

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<&Bench> for Bench

source§

fn from(value: &Bench) -> Self

Converts to this type from the input type.
source§

impl From<&mut Bench> for Bench

source§

fn from(value: &mut Bench) -> Self

Converts to this type from the input type.
source§

impl PartialEq for Bench

source§

fn eq(&self, other: &Bench) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl StructuralPartialEq for Bench

Auto Trait Implementations§

§

impl Freeze for Bench

§

impl RefUnwindSafe for Bench

§

impl Send for Bench

§

impl Sync for Bench

§

impl Unpin for Bench

§

impl UnwindSafe for Bench

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> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.