Crate chairmark

Crate chairmark 

Source
Expand description

Crate for benchmarking

Unfortunately, there’s no stable support for proper cargo bench stuff. Therefore, I made this crate

The idea is to have a function that benchmarks another given function. This will be done a specified number of times for more reliable data. During this benchmark (“chairmark”) running time data is collected that can be aggregated etc..

This data can then be displayed easliy and readable. There’s also an additional feature for comparing different chairmarks. This can be used to compare a custom implementation with one from the standard library or different implmenentations.

§Examples

§example without using macros

use chairmark::{chair, Time, Comparison};

// checks if number is power of two
fn is_power(arg: u32) -> bool {
    arg.to_ne_bytes().into_iter().sum::<u8>() == 1
}

fn main() {
    const NUMBER: u32 = 69;

    // benchmark std function
    let std = chair(1_000, || NUMBER.is_power_of_two()).aggregate::<Time>();
    // benchmark custom function
    let custom = chair(1_000, || is_power(NUMBER)).aggregate::<Time>();

    // compare
    let compare = Comparison::from([("std", std), ("custom", custom)]);

    // display as a table
    println!("{}", compare);
}

§example with all macros

use chairmark::*;

// checks if number is power of two
fn is_power(arg: u32) -> bool {
    arg.to_ne_bytes().into_iter().sum::<u8>() == 1
}

fn main() {
    const NUMBER: u32 = 69;

    // benchmark std function
    let std = chair(1_000, || NUMBER.is_power_of_two());
    // benchmark custom function
    let custom = chair(1_000, || is_power(NUMBER));

    // compare
    let compare = agg_and_cmp![std, custom];

    // display as table
    println!("{}", compare);
}

§example with prepared data

use chairmark::*;

// custom sort function
fn bubblesort(data: &mut Vec<u32>) {
    /* your great bubblesort implementation */
}

fn main() {
    let prepare = |idx| (0..idx).map(|x| x as u32 / 3).collect::<Vec<_>>();

    // benchmark std functions
    let std_stable = chair_prepare(1_000, prepare, |mut data| data.sort());
    let std_unstable = chair_prepare(1_000, prepare, |mut data| data.sort_unstable());
    // benchmark custom function
    let custom = chair_prepare(1_000, prepare, |mut data| bubblesort(&mut data));

    // aggregate and compare
    let compare = agg_and_cmp![std_stable, std_unstable, custom];

    println!("{}", compare);
}

Macros§

agg
aggregates to an Aggregate of Time
agg_and_cmp
wrappers “calls” to agg! and compare!
compare
instantiates a Comparison of the given values

Structs§

Aggregate
store aggregated data from Measurements
Comparison
comparison of multiple aggregates
Measurements
keep track of time measurements
Time
custom time storing other than Duration

Functions§

chair
runs a chairmark on a single function
chair_prepare
runs a chairmark with prepared data