iai_callgrind

Struct Sandbox

source
pub struct Sandbox(/* private fields */);
Available on crate feature default only.
Expand description

The Sandbox in which the setup, teardown and the Command are run

The Sandbox is a temporary directory which is created before the execution of the setup and deleted after the teardown. setup, the Command and teardown are executed inside this temporary directory.

§Background and reasons for using a Sandbox

A Sandbox can help mitigating differences in benchmark results on different machines. As long as $TMP_DIR is unset or set to /tmp, the temporary directory has a constant length on unix machines (except android which uses /data/local/tmp). The directory itself is created with a constant length but random name like /tmp/.a23sr8fk. It is not implausible that an executable has different event counts just because the directory it is executed in has a different length. For example, if a member of your project has set up the project in /home/bob/workspace/our-project running the benchmarks in this directory, and the ci runs the benchmarks in /runner/our-project, the event counts might differ. If possible, the benchmarks should be run in an as constant as possible environment. Clearing the environment variables is also such a counter-measure.

Other reasons for using a Sandbox are convenience, such as if you’re creating files during setup and the Command run and don’t want to delete all the files manually. Or, more importantly, if the Command is destructive and deletes files, it is usually safer to execute such a Command in a temporary directory where it cannot do any harm to your or others file systems during the benchmark runs.

§Sandbox cleanup

The changes the setup makes in this directory persist until the teardown has finished. So, the Command can for example pick up any files created by the setup method. If run in a Sandbox, the teardown usually doesn’t have to delete any files, because the whole directory is deleted after its usage. There is an exception to the rule. If any of the files inside the directory is not removable, for example because the permissions of a file don’t allow the file to be deleted, then the whole directory persists. You can use the teardown to reset all permission bits to be readable and writable, so the cleanup can succeed.

To simply copy fixtures or whole directories into the Sandbox use Sandbox::fixtures.

Implementations§

source§

impl Sandbox

source

pub fn new(enabled: bool) -> Self

Create a new Sandbox builder

Per default, a Command is not run in a Sandbox because setting up a Sandbox usually involves some user interaction, for example copying fixtures into it with Sandbox::fixtures.

The temporary directory is only created immediately before the setup and the Command are executed.

§Examples

Enable the sandbox for all benchmarks

use iai_callgrind::{BinaryBenchmarkConfig, Sandbox, main};
main!(
    config = BinaryBenchmarkConfig::default().sandbox(Sandbox::new(true));
    binary_benchmark_groups = my_group
);
source

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

Specify the directories and/or files you want to copy into the root of the Sandbox

The paths are interpreted relative to the workspace root as it is reported by cargo. In a multi-crate project this is the directory with the top-level Cargo.toml. Otherwise, it is simply the directory with your Cargo.toml file in it.

§Examples

Assuming you crate’s binary is called my-foo taking a file path as the first argument and the fixtures directory is $WORKSPACE_ROOT/benches/fixtures containing a fixture fix_1.txt:

use iai_callgrind::{binary_benchmark, BinaryBenchmarkConfig, Sandbox};

#[binary_benchmark]
#[bench::fix_1(
     args = ("fix_1.txt"),
     config = BinaryBenchmarkConfig::default()
         .sandbox(Sandbox::new(true)
             .fixtures(["benches/fixtures/fix_1.txt"])
        )
)]
fn bench_with_fixtures(path: &str) -> iai_callgrind::Command {
    iai_callgrind::Command::new(env!("CARGO_BIN_EXE_my-foo"))
        .arg(path)
        .build()
}

Trait Implementations§

source§

impl AsRef<Sandbox> for Sandbox

source§

fn as_ref(&self) -> &InternalSandbox

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Clone for Sandbox

source§

fn clone(&self) -> Sandbox

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 Sandbox

source§

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

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

impl From<&Sandbox> for InternalSandbox

source§

fn from(value: &Sandbox) -> Self

Converts to this type from the input type.
source§

impl From<&mut Sandbox> for InternalSandbox

source§

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

Converts to this type from the input type.
source§

impl From<Sandbox> for InternalSandbox

source§

fn from(value: Sandbox) -> Self

Converts to this type from the input type.

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