5 releases

Uses new Rust 2021

new 0.1.2 Jan 22, 2023
0.1.1 Jan 4, 2023
0.1.0 Jan 2, 2023
0.0.1 Nov 24, 2022
0.0.0 Nov 22, 2022

#89 in Profiling

Download history 32/week @ 2022-11-18 19/week @ 2022-11-25 4/week @ 2022-12-02 3/week @ 2022-12-23 53/week @ 2022-12-30 10/week @ 2023-01-06 7/week @ 2023-01-13 29/week @ 2023-01-20

99 downloads per month

Apache-2.0/MIT

29KB
502 lines

Calliper

Calliper is a Callgrind-based benchmarking harness with few-too-many knobs sticking out.

Table of contents

Usage

To use Calliper, you must have Valgrind installed.

To write your first benchmark with Calliper, add the following to your Cargo.toml:

[dev-dependencies]
calliper = "0.1.2"

[[bench]]
name = "my_first_calliper_benchmark"
harness = false

Then, create a file at $PROJECT/benches/my_first_calliper_benchmark.rs with the following contents:

use calliper::utils::black_box;
use calliper::{Runner, Scenario};

#[inline(never)]
#[no_mangle]
fn binary_search_impl(haystack: &[u8], needle: u8) -> Result<usize, usize> {
    haystack.binary_search(&needle)
}
fn bench_binary_search() {
    let range = (0..255).collect::<Vec<_>>();
    let _ = black_box(binary_search_impl(black_box(&range), black_box(253)));
}

#[inline(never)]
#[no_mangle]
fn linear_search_impl(haystack: &[u8], needle: u8) -> Option<usize> {
    haystack.iter().position(|n| *n == needle)
}

fn bench_linear_search() {
    let range = (0..255).collect::<Vec<_>>();
    black_box(linear_search_impl(black_box(&range), black_box(253)));
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let runner = Runner::default();
    let benches = [
        Scenario::new(bench_linear_search),
        Scenario::new(bench_binary_search),
    ];
    runner.run(&benches).unwrap();
    Ok(())
}

Now the benchmark can be executed with cargo bench.

More sophisticated examples can be found in benches folder of this repository.

License

This project is licensed under either of

at your option.

Acknowledgements

Calliper is inspired by Iai benchmarking harness.

Dependencies

~4.5MB
~99K SLoC