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

add prettier progress with `indicatif`

parent 04f448bd
Branches
Tags
No related merge requests found
......@@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
indicatif = "0.17.8"
[dev-dependencies]
clap = { version = "4.5.4", features = ["derive"] }
......
......@@ -16,12 +16,13 @@ cargo run --release --example arithmetic -- --nb-measurements 1000
the output of the snippet above will have two parts:
### on STDERR
the benchmarking process for debugging
the benchmarking process will show progress bars while running and the details
of each benchmark will be shown as follows
```text
bencher: arithmetic, label: addition [ 10/10]
bencher: arithmetic, label: substraction [ 10/10]
bencher: arithmetic, label: multiplication [ 10/10]
bencher: random, label: sampling [ 10/10]
bencher: arithmetic, label: addition
bencher: arithmetic, label: substraction
bencher: arithmetic, label: multiplication
bencher: random, label: sampling
```
### on STDOUT
......
......@@ -2,6 +2,8 @@
use std::time::{Duration, Instant};
use indicatif::{ProgressBar, ProgressStyle};
/// hold information about the benchmark to run
pub struct Bencher {
/// the number of times each bench should run, the higher this number the lower the variance
......@@ -37,20 +39,16 @@ pub fn bench<F>(b: &Bencher, label: &str, mut f: F)
where
F: FnMut() -> Duration,
{
let mut times = vec![];
for i in 0..b.nb_measurements {
eprint!(
"bencher: {}, label: {} [{:>width$}/{}]\r",
b.name,
label,
i + 1,
b.nb_measurements,
width = format!("{}", b.nb_measurements).len()
);
eprintln!("bencher: {}, label: {}", b.name, label,);
let pb = ProgressBar::new(b.nb_measurements as u64)
.with_style(ProgressStyle::default_bar().progress_chars("#*-"));
let mut times = vec![];
for _ in 0..b.nb_measurements {
times.push(f().as_nanos());
pb.inc(1);
}
eprintln!();
println!(
r#"{{"label": "{}", "name": "{}", "times": {:?}}}"#,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment