iai_callgrind

Struct BenchmarkId

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

low level api only: Create a new benchmark id

Implementations§

source§

impl BenchmarkId

source

pub fn with_parameter<T, P>(id: T, parameter: P) -> Self
where T: AsRef<str>, P: Display,

Convenience method to create a BenchmarkId with a parameter in the low level api

The parameter is simply appended to the id with an underscore, so BenchmarkId::with_parameter("some", 1) is equivalent to BenchmarkId::new("some_1")

§Examples
use iai_callgrind::BenchmarkId;

let new_id = BenchmarkId::new("prefix_1");
let with_parameter = BenchmarkId::with_parameter("prefix", 1);
assert_eq!(new_id, with_parameter);
use iai_callgrind::{binary_benchmark_group,BenchmarkId, BinaryBenchmark, Bench, Command};
use std::ffi::OsStr;

binary_benchmark_group!(
    name = low_level_group;
    benchmarks = |group: &mut BinaryBenchmarkGroup| {
        let mut binary_benchmark = BinaryBenchmark::new("some_id");
        for arg in 0..10 {
            let id = BenchmarkId::with_parameter("prefix", arg);
            binary_benchmark.bench(
                Bench::new(id)
                    .command(
                        Command::new("echo").arg(arg.to_string()).build()
                    )
            );
        }
        group.binary_benchmark(binary_benchmark);
    }
);
source

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

Create a new BenchmarkId

BenchmarkIds can be created from any string-like input. See BenchmarkId::validate for ids which are considered valid.

§Examples
use iai_callgrind::BenchmarkId;

let id = BenchmarkId::new("my_id");

assert!(id.validate().is_ok());
source

pub fn validate(&self) -> Result<(), String>

Returns ok if this BenchmarkId is valid

An id should be short, descriptive besides being unique. The requirements for the uniqueness differ for the structs where a BenchmarkId is used and is further described there.

We use a minimal subset of rust’s identifiers. A valid BenchmarkId starts with an ascii alphabetic letter [a-zA-Z] or underscore [_]. All following characters can be an ascii alphabetic letter, underscore or a digit [0-9]. At least one valid character must be present.

The BenchmarkId is used by iai-callgrind as file and directory name for the output files of the benchmarks. Therefore, the limit for an id is chosen to be 120 bytes. This is a calculation with some headroom. There are file systems which do not even allow 255 bytes. If you’re working on such a peculiar file system, you have to restrict your ids to even fewer bytes which is floor(MAX_LENGTH/2) - 1. Leaving the maximum bytes aside, the best IDs are simple, short and descriptive.

Usually, it is not necessary to call this function, since we already check the validity of benchmark ids prior to the execution of the benchmark runner. But if your ids come from an untrusted source you can use this method for more immediate feedback.

§Errors

This function will return an error describing the source of the error if the id is invalid

§Examples
use iai_callgrind::BenchmarkId;

assert!(BenchmarkId::new("").validate().is_err());
assert!(BenchmarkId::new("0a").validate().is_err());
assert!(BenchmarkId::new("id with spaces").validate().is_err());
assert!(BenchmarkId::new("path/to").validate().is_err());
assert!(BenchmarkId::new("no::module::too").validate().is_err());

assert!(BenchmarkId::new("_").validate().is_ok());
assert!(BenchmarkId::new("abc").validate().is_ok());
assert!(BenchmarkId::new("a9").validate().is_ok());
assert!(BenchmarkId::new("z_").validate().is_ok());
assert!(BenchmarkId::new("some_id").validate().is_ok());

Trait Implementations§

source§

impl Clone for BenchmarkId

source§

fn clone(&self) -> BenchmarkId

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 BenchmarkId

source§

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

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

impl Display for BenchmarkId

source§

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

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

impl From<BenchmarkId> for String

source§

fn from(value: BenchmarkId) -> Self

Converts to this type from the input type.
source§

impl<T> From<T> for BenchmarkId
where T: AsRef<str>,

source§

fn from(value: T) -> Self

Converts to this type from the input type.
source§

impl Hash for BenchmarkId

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for BenchmarkId

source§

fn eq(&self, other: &BenchmarkId) -> 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 Eq for BenchmarkId

source§

impl StructuralPartialEq for BenchmarkId

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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.