Skip to content

Commit

Permalink
Use criterion for benches
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarb committed Apr 13, 2022
1 parent 94f59cd commit a202c05
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 36 deletions.
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,14 @@ nightly = ["no_cc"]
cc = "1.0"

[dependencies]

[dev-dependencies]
criterion = { version = "0.3", features = ["cargo_bench_support", "html_reports"] }

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

[[bench]]
name = "clear_stack_on_return"
harness = false
41 changes: 27 additions & 14 deletions benches/clear_on_drop.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,53 @@
#![feature(test)]
use criterion::{criterion_group, criterion_main, Criterion};

extern crate test;
use test::Bencher;

extern crate clear_on_drop;
use clear_on_drop::ClearOnDrop;

#[bench]
fn clear_on_drop_small(b: &mut Bencher) {
fn clear_on_drop_small(c: &mut Criterion) {
#[derive(Default)]
struct Data {
_data: u64,
}

let mut place = Data::default();
b.iter(|| { ClearOnDrop::new(&mut place); })
c.bench_function("clear_on_drop_small", |b| {
b.iter(|| {
ClearOnDrop::new(&mut place);
})
});
}

#[bench]
fn clear_on_drop_medium(b: &mut Bencher) {
fn clear_on_drop_medium(c: &mut Criterion) {
#[derive(Default)]
struct Data {
_data: [u64; 32],
}

let mut place = Data::default();
b.iter(|| { ClearOnDrop::new(&mut place); })
c.bench_function("clear_on_drop_medium", |b| {
b.iter(|| {
ClearOnDrop::new(&mut place);
})
});
}

#[bench]
fn clear_on_drop_large(b: &mut Bencher) {
fn clear_on_drop_large(c: &mut Criterion) {
#[derive(Default)]
struct Data {
_data: [[u64; 32]; 32],
}

let mut place = Data::default();
b.iter(|| { ClearOnDrop::new(&mut place); })
c.bench_function("clear_on_drop_large", |b| {
b.iter(|| {
ClearOnDrop::new(&mut place);
})
});
}

criterion_group!(
benches,
clear_on_drop_small,
clear_on_drop_medium,
clear_on_drop_large
);
criterion_main!(benches);
43 changes: 26 additions & 17 deletions benches/clear_stack_on_return.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
#![feature(test)]
use criterion::{criterion_group, criterion_main, Criterion};

extern crate test;
use test::Bencher;

extern crate clear_on_drop;
use clear_on_drop::{clear_stack_on_return, clear_stack_on_return_fnonce};

#[bench]
fn clear_stack_on_return_tiny(b: &mut Bencher) {
b.iter(|| clear_stack_on_return(1, || 0x41))
fn clear_stack_on_return_tiny(c: &mut Criterion) {
c.bench_function("clear_stack_on_return_tiny", |b| {
b.iter(|| clear_stack_on_return(1, || 0x41))
});
}

#[bench]
fn clear_stack_on_return_small(b: &mut Bencher) {
b.iter(|| clear_stack_on_return(2, || 0x41))
fn clear_stack_on_return_small(c: &mut Criterion) {
c.bench_function("clear_stack_on_return_small", |b| {
b.iter(|| clear_stack_on_return(2, || 0x41))
});
}

#[bench]
fn clear_stack_on_return_fnonce_tiny(b: &mut Bencher) {
b.iter(|| clear_stack_on_return_fnonce(1, || 0x41))
fn clear_stack_on_return_fnonce_tiny(c: &mut Criterion) {
c.bench_function("clear_stack_on_return_fnonce_tiny", |b| {
b.iter(|| clear_stack_on_return_fnonce(1, || 0x41))
});
}

#[bench]
fn clear_stack_on_return_fnonce_small(b: &mut Bencher) {
b.iter(|| clear_stack_on_return_fnonce(2, || 0x41))
fn clear_stack_on_return_fnonce_small(c: &mut Criterion) {
c.bench_function("clear_stack_on_return_fnonce_small", |b| {
b.iter(|| clear_stack_on_return_fnonce(2, || 0x41))
});
}

criterion_group!(
benches,
clear_stack_on_return_tiny,
clear_stack_on_return_small,
clear_stack_on_return_fnonce_tiny,
clear_stack_on_return_fnonce_small
);
criterion_main!(benches);
2 changes: 0 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate cc;

fn main() {
if !cfg!(feature = "no_cc") {
cc::Build::new()
Expand Down
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@
//! the `no_cc` feature, works on stable Rust, and does not need a C
//! compiler.

#[cfg(test)]
extern crate core;

pub mod clear;
mod clear_on_drop;
mod clear_stack_on_return;
Expand Down

0 comments on commit a202c05

Please sign in to comment.