Skip to content
Snippets Groups Projects
Verified Commit fa3004fb authored by STEVAN Antoine's avatar STEVAN Antoine :crab:
Browse files

add support for running multiple benches

parent 18c687f5
Branches
No related tags found
No related merge requests found
Pipeline #9090 failed
......@@ -23,6 +23,38 @@ fn arithmetic(b: &plnk::Bencher) {
eprintln!("last value: {}", res.unwrap());
}
fn arithmetic_multiple(b: &plnk::Bencher) {
plnk::bench_multiple(
b,
&mut [
(
plnk::label! { operation: "addition" }.to_string(),
Box::new(|| {
let a = rand::thread_rng().gen::<u128>();
let b = rand::thread_rng().gen::<u128>();
plnk::timeit(|| a + b)
}),
),
(
plnk::label! { operation: "substraction" }.to_string(),
Box::new(|| {
let a = rand::thread_rng().gen::<u128>();
let b = rand::thread_rng().gen::<u128>();
plnk::timeit(|| a - b)
}),
),
(
plnk::label! { operation: "multiplication" }.to_string(),
Box::new(|| {
let a = rand::thread_rng().gen::<u128>();
let b = rand::thread_rng().gen::<u128>();
plnk::timeit(|| a * b)
}),
),
],
)
}
fn random(b: &plnk::Bencher) {
let mut rng = rand::thread_rng();
plnk::bench(b, plnk::label! { operation: "sampling" }, || {
......@@ -34,6 +66,9 @@ fn random(b: &plnk::Bencher) {
struct Args {
#[arg(short, long, default_value_t = 10)]
nb_measurements: usize,
#[arg(short, long)]
multiple: bool,
}
fn main() {
......@@ -41,6 +76,10 @@ fn main() {
let bencher = plnk::Bencher::new(args.nb_measurements);
if args.multiple {
arithmetic_multiple(&bencher.with_name("arithmetic"));
} else {
arithmetic(&bencher.with_name("arithmetic"));
}
random(&bencher.with_name("random"));
}
......@@ -2,7 +2,7 @@
use std::time::{Duration, Instant};
use indicatif::{ProgressBar, ProgressStyle};
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
#[derive(Debug, Clone)]
/// hold information about the benchmark to run
......@@ -85,6 +85,44 @@ where
println!("{}", label! { label: label, name: b.name, times: times });
}
/// run a given piece of code a bunch of times and output the measurements
///
/// - benches is a list of label-f pairs:
/// - label: an additional label to differentiate similar but different things to benchmark in the
/// same bencher
/// - f: the piece of code to run and measure
/// - the measurements will be printed to STDOUT as JSON
pub fn bench_multiple(b: &Bencher, benches: &mut [(String, Box<dyn FnMut() -> Duration>)]) {
let style = ProgressStyle::with_template(
"[{elapsed_precise}] {bar:40.cyan/blue} {pos:>10}/{len:10} {msg}",
)
.unwrap()
.progress_chars("##-");
let mpb = MultiProgress::new();
let pb_main = mpb.add(ProgressBar::new(benches.len() as u64).with_style(style.clone()));
pb_main.set_message(b.name.clone());
for (label, f) in benches.iter_mut() {
let pb = mpb.add(ProgressBar::new(b.nb_measurements as u64).with_style(style.clone()));
pb.set_message(label.to_string());
let mut times = vec![];
for _ in 0..b.nb_measurements {
times.push(f().as_nanos());
pb.inc(1);
}
pb_main.inc(1);
pb.finish_and_clear();
println!("{}", label! { label: label, name: b.name, times: times });
}
pb_main.finish_with_message(format!("{} done", b.name));
}
/// run a given piece of code a bunch of times and output the measurements and the last return
/// value
///
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment