1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::collections::BTreeMap;
use std::fmt::{Display, Formatter};
use std::thread;
use std::time::Duration;

use benchmark_rs::{Benchmarks, StopWatch};

#[derive(Clone)]
struct Config {
    pub workloads: BTreeMap<u64, Duration>,
}

impl Display for Config {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        for k in self.workloads.keys() {
            write!(f, "{k} ")?;
        }
        Ok(())
    }
}

fn example(stop_watch: &mut StopWatch, config: Config, work: u64) -> Result<(), anyhow::Error> {
    stop_watch.pause();
    // perform potentially lengthy preparation that will not reflect in the measurement
    let sleep_time = config.workloads.get(&work).unwrap().clone();
    stop_watch.resume();
    // perform measured computation
    thread::sleep(sleep_time);
    stop_watch.pause();
    // perform potentially lengthy cleanup
    Ok(())
}

fn main() -> Result<(), anyhow::Error> {
    let mut benchmarks = Benchmarks::new("Example");
    let workloads: BTreeMap<u64, Duration> = (0..=10).map(|i| (i, Duration::from_millis(i))).collect();
    benchmarks.add("Another Benchmark", example, Config { workloads }, (1..=10).collect(), 2, 1)?;
    benchmarks.run()?;

    let summary = benchmarks.summary_as_json();
    println!("Summary: {summary}");
    let csv_headers = benchmarks.csv_headers();
    let csv_data = benchmarks.summary_as_csv();
    for (k, v) in csv_data {
        println!("Benchmark name: {k}");
        println!("{csv_headers}");
        for line in v {
            println!("{line}")
        }
    }

    // ignore new series
    let analysis_result = benchmarks.analyze(None, 5.0)?;
    println!("Analysis result: {}", analysis_result.to_string());
    // compare results with threshold of 5 percent
    let analysis_result = benchmarks.analyze(Some(summary), 5.0)?;
    println!("Analysis result: {}", analysis_result.to_string());
    Ok(())
}