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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use {
    crate::*,
    minimad::OwningTemplateExpander,
};

static MD: &str = r#"
# ${bench-title}
${comparison
comparison with ${previous-date} ${git-diff}
}
|-:|:-:|:-:|:-:|:-:
|#|**task**|**iterations**|**total duration**|**mean duration**|**change**
|-:|:-|-:|-:|-:|-:
${tasks
|**${task-num}**|${task-name}|${iterations}|${total-duration}|**${mean-duration}**|${change}
}
|-:
"#;

/// A temporary structure to print the result of a benchmark to the console
pub struct Report<'b> {
    bench: &'b Bench,
    previous: &'b Option<Bench>,
}

impl<'b> Report<'b> {

    pub fn new(
        bench: &'b Bench,
        previous: &'b Option<Bench>,
    ) -> Self {
        Self {
            bench,
            previous,
        }
    }

    /// Print the report to the console
    ///
    /// You don't have to call this yourself if you use
    /// the `glassbench!` macro.
    pub fn print(&self, printer: &Printer) {
        let mut expander = OwningTemplateExpander::new();
        expander
            .set("bench-title", &self.bench.title)
            .set("bench-name", &self.bench.name);
        if let Some(previous) = self.previous.as_ref() {
            expander.sub("comparison")
                .set("previous-date", previous.time)
                .set("git-diff", GitInfo::diff(&previous.git_info, &self.bench.git_info));
        }
        for (idx, task) in self.bench.tasks.iter().enumerate() {
            if let Some(mes) = &task.measure {
                let sub = expander.sub("tasks");
                sub
                    .set("task-num", idx+1)
                    .set("task-name", &task.name)
                    .set("iterations", &mes.iterations)
                    .set("total-duration", format!("{:?}", &mes.total_duration))
                    .set("mean-duration", format!("{:?}", mes.mean_duration()));
                let diff = self.previous
                    .as_ref()
                    .and_then(|obench| task.diff_with(obench));
                if let Some(diff) = diff {
                    sub.set_md(
                        "change",
                        if diff.percents < 0.0 {
                            if diff.percents < -2.0 {
                                format!("*{:.2}%*", diff.percents)
                            } else {
                                format!("{:.2}%", diff.percents)
                            }
                        } else {
                            if diff.percents > 2.0 {
                                format!("~~+{:.2}%~~", diff.percents)
                            } else {
                                format!("+{:.2}%", diff.percents)
                            }
                        },
                    );
                } else {
                    sub.set("change", " ");
                }
            }
        }
        printer.print(expander, MD);
    }
}