summaryrefslogtreecommitdiff
path: root/src/report.rs
blob: 297195d151eca73b10de0067f474e0864070941a (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use crate::result::{Measurement, Operation, SuiteMeasurements};
use std::collections::HashSet;
use std::io::Write;
use std::iter::FromIterator;

const COMMIT_DIGITS: usize = 7;

#[derive(Debug, Default)]
pub struct Reporter {
    results: Vec<SuiteMeasurements>,
}

impl Reporter {
    pub fn push(&mut self, result: SuiteMeasurements) {
        self.results.push(result);
    }

    pub fn write(&self, f: &mut dyn Write) -> Result<(), std::io::Error> {
        // Summary tables of all benchmarks.
        writeln!(f, "# Summaries of all benchmark runs")?;
        let hosts = self.hosts();
        for bench in self.benchmarks() {
            writeln!(f)?;
            writeln!(f, "## Benchmark {}", bench)?;
            for op in [Operation::Backup, Operation::Restore] {
                for host in hosts.iter() {
                    writeln!(f)?;
                    writeln!(f, "Table: {:?} on host {}, times in ms", op, host)?;
                    writeln!(f)?;
                    let mut want_headings = true;
                    for r in self.results_for(host) {
                        // Column data
                        let cols = self.durations(r, &bench, op);

                        if want_headings {
                            want_headings = false;
                            self.headings(f, &cols)?;
                        }
                        self.rows(f, r, &cols)?;
                    }
                }
            }
        }

        writeln!(f)?;
        writeln!(f, "# Individual benchmark runs")?;
        for host in self.hosts() {
            writeln!(f)?;
            writeln!(f, "## Host `{}`", host)?;
            for r in self.results_for(&host) {
                writeln!(f)?;
                writeln!(f, "### Benchmark run {}", r.timestamp())?;
                writeln!(f)?;
                writeln!(f, "* CPUs: {}", r.cpus())?;
                writeln!(f, "* RAM: {} MiB", r.ram() / 1024 / 1024)?;
                for bench in r.benchmark_names() {
                    writeln!(f)?;
                    writeln!(f, "#### Benchmark `{}`", bench)?;
                    writeln!(f)?;
                    for opm in r.ops().filter(|o| o.name() == bench) {
                        let op = opm.op();
                        match op {
                            Operation::Backup => {
                                writeln!(f, "* Backup: {} ms", opm.millis())?;
                            }
                            Operation::Restore => {
                                writeln!(f, "* Restore: {} ms", opm.millis())?;
                            }
                            _ => continue,
                        }
                        for m in opm.iter() {
                            match m {
                                Measurement::DurationMs(_) => (),
                                Measurement::TotalFiles(n) => {
                                    writeln!(f, "  * files: {}", n)?;
                                }
                                &Measurement::TotalData(n) => {
                                    writeln!(f, "  * data: {} bytes", n)?;
                                }
                            }
                        }
                    }

                    for op in [Operation::Backup, Operation::Restore] {
                        writeln!(f)?;
                        writeln!(f, "Table: {:?}", op)?;
                        writeln!(f)?;
                        let cols = self.durations(r, &bench, op);
                        self.headings(f, &cols)?;
                        self.rows(f, r, &cols)?;
                    }
                }
            }
        }

        Ok(())
    }

    fn durations(&self, r: &SuiteMeasurements, bench: &str, op: Operation) -> Vec<u128> {
        r.ops()
            .filter(|r| r.name() == bench)
            .filter(|r| r.op() == op)
            .map(|r| r.millis())
            .collect()
    }

    fn headings(&self, f: &mut dyn Write, durations: &[u128]) -> Result<(), std::io::Error> {
        // Column headings.
        let mut headings: Vec<String> = durations
            .iter()
            .enumerate()
            .map(|(i, _)| format!("step {}", i))
            .collect();
        headings.insert(0, "Commit".to_string());
        headings.insert(0, "Version".to_string());
        for h in headings.iter() {
            write!(f, "| {}", h)?;
        }
        writeln!(f, "|")?;

        // Heading separator.
        for _ in headings.iter() {
            write!(f, "|----")?;
        }
        writeln!(f, "|")?;
        Ok(())
    }

    fn rows(
        &self,
        f: &mut dyn Write,
        r: &SuiteMeasurements,
        durations: &[u128],
    ) -> Result<(), std::io::Error> {
        write!(f, "| {}", pretty_version(r.obnam_version()))?;
        write!(f, "| {}", pretty_commit(r.obnam_commit()))?;
        for ms in durations.iter() {
            write!(f, "| {}", ms)?;
        }
        writeln!(f, "|")?;
        Ok(())
    }

    fn benchmarks(&self) -> Vec<String> {
        let mut names = HashSet::new();
        for r in self.results.iter() {
            for opm in r.ops() {
                names.insert(opm.name());
            }
        }
        let mut names: Vec<String> = names.iter().map(|x| x.to_string()).collect();
        names.sort();
        names
    }

    fn hosts(&self) -> Vec<String> {
        let names: HashSet<&str> = HashSet::from_iter(self.results.iter().map(|r| r.hostname()));
        let mut names: Vec<String> = names.iter().map(|x| x.to_string()).collect();
        names.sort();
        names
    }

    fn results_for<'a>(&'a self, hostname: &'a str) -> impl Iterator<Item = &'a SuiteMeasurements> {
        self.results
            .iter()
            .filter(move |r| r.hostname() == hostname)
    }
}

fn pretty_version(v: &str) -> String {
    if let Some(v) = v.strip_prefix("obnam-backup ") {
        v.to_string()
    } else {
        v.to_string()
    }
}

fn pretty_commit(c: Option<&str>) -> String {
    if let Some(c) = c {
        c[..COMMIT_DIGITS].to_string()
    } else {
        "".to_string()
    }
}