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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//! `Comparator` Structure for easy comparisons of different algorithms.
use crate::stats::Stats;
use crate::ThreadPool;
use crate::{
    log::RunLog,
    svg::{histogram, HISTOGRAM_COLORS},
};
use crate::{svg::fill_svg_file, visualisation};
use itertools::{izip, Itertools};
use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::*;
use std::io::Error;
use std::iter::repeat_with;
use std::path::Path;

/// The comparator structure enables you to easily compare performances of different algorithms.
///
/// It runs each algorithm several times before displaying some simple statistics and for each
/// algorithm the median and best execution trace.
/// See for example the `filter_collect` example.
pub struct Comparator<'a> {
    labels: Vec<String>,
    logs: Vec<Vec<RunLog>>,
    pool: &'a ThreadPool,
    runs_number: usize,
    display_preferences: Vec<bool>,
}

impl<'a> Comparator<'a> {
    pub(crate) fn new(pool: &'a ThreadPool) -> Self {
        Comparator {
            labels: Vec::new(),
            logs: Vec::new(),
            pool,
            runs_number: 100,
            display_preferences: Vec::new(),
        }
    }
    /// Renumber all tags accross all logs such that tags number match.
    /// Return vector of all tags.
    fn fuse_tags(&mut self) -> Vec<String> {
        let mut global_tags = HashMap::new();
        for experiment in &self.logs {
            for log in experiment {
                log.scan_tags(&mut global_tags);
            }
        }
        for experiment in &mut self.logs {
            for log in experiment {
                log.update_tags(&global_tags);
            }
        }
        global_tags
            .into_iter()
            .sorted_by_key(|&(_, i)| i)
            .map(|(t, _)| t)
            .collect()
    }
    /// Sets the number of runs for each algorithm.
    /// PRECONDITION: call that BEFORE attaching algorithms
    pub fn runs_number(self, runs_wanted: usize) -> Self {
        Comparator {
            labels: self.labels,
            logs: self.logs,
            pool: self.pool,
            runs_number: runs_wanted,
            display_preferences: self.display_preferences,
        }
    }

    fn record_experiments<F: FnMut() -> RunLog>(&self, run_function: F) -> Vec<RunLog> {
        let mut experiments_logs: Vec<_> =
            repeat_with(run_function).take(self.runs_number).collect();
        experiments_logs.sort_unstable_by_key(|run| run.duration);
        experiments_logs
    }

    /// Log an algorithm's performances but do not generate svg traces.
    pub fn attach_algorithm_nodisplay<A, STR>(mut self, label: STR, algorithm: A) -> Self
    where
        A: Fn() + Send + Sync,
        STR: Into<String>,
    {
        let logs = self.record_experiments(|| self.pool.logging_install(&algorithm).1);
        self.logs.push(logs);
        self.labels.push(label.into());
        self.display_preferences.push(false);
        self
    }
    /// Log an algorithm's performances and generate svg traces.
    pub fn attach_algorithm<A, STR>(mut self, label: STR, algorithm: A) -> Self
    where
        A: Fn() + Send + Sync,
        STR: Into<String>,
    {
        let logs = self.record_experiments(|| self.pool.logging_install(&algorithm).1);
        self.logs.push(logs);
        self.labels.push(label.into());
        self.display_preferences.push(true);
        self
    }

    /// Log an algorithm but prepare an input (un-timed) for each execution.
    /// No svg traces.
    pub fn attach_algorithm_nodisplay_with_setup<A, I, S, T, STR>(
        mut self,
        label: STR,
        mut setup_function: S,
        algorithm: A,
    ) -> Self
    where
        S: FnMut() -> I,
        I: Send,
        A: Fn(I) -> T + Send + Sync,
        T: Send + Sync,
        STR: Into<String>,
    {
        let logs = self.record_experiments(|| {
            let input = setup_function();
            self.pool.logging_install(|| algorithm(input)).1
        });
        self.logs.push(logs);
        self.labels.push(label.into());
        self.display_preferences.push(false);
        self
    }

    /// Log an algorithm but prepare an input (un-timed) for each execution.
    /// With svg traces.
    pub fn attach_algorithm_with_setup<A, I, S, T, STR>(
        mut self,
        label: STR,
        mut setup_function: S,
        algorithm: A,
    ) -> Self
    where
        S: FnMut() -> I,
        I: Send,
        A: Fn(I) -> T + Send + Sync,
        T: Send + Sync,
        STR: Into<String>,
    {
        let logs = self.record_experiments(|| {
            let input = setup_function();
            self.pool.logging_install(|| algorithm(input)).1
        });
        self.logs.push(logs);
        self.labels.push(label.into());
        self.display_preferences.push(true);
        self
    }

    /// This method should be called in the end to write the logs to a desired html file.
    pub fn generate_logs<P: AsRef<Path>>(mut self, filename: P) -> Result<(), Error> {
        let tags = self.fuse_tags(); // have a consistent tags numbering accross all logs
        let mut html_file = File::create(filename)?;

        writeln!(html_file, "<!DOCTYPE html>")?;
        writeln!(
            html_file,
            r#"
<html><head><style>
table, th, td {{
  border: 1px solid black;
  border-collapse: collapse;
}}
</style>
</head>
<body><center>"#,
        )?;
        let (last_label, first_labels) = self.labels.split_last().expect("not enough experiments");
        writeln!(
            html_file,
            "<H1> Comparing {} and {}</H1>",
            first_labels.join(", "),
            last_label
        )?;

        writeln!(
            html_file,
            "<H2>Distribution of execution times over {} runs ",
            self.runs_number
        )?;
        for (label, color) in self.labels.iter().zip(HISTOGRAM_COLORS.iter().cycle()) {
            writeln!(
                html_file,
                "<text style=\"color:{0}\">{0}</text> is {1}, ",
                color, label
            )?;
        }
        writeln!(html_file, "</H2>")?;
        histogram(&mut html_file, &self.logs, 30)?;
        let number_of_threads = self.logs[0][0].threads_number;
        let statistics = Stats::get_statistics(&self.logs, number_of_threads, self.runs_number);
        writeln!(html_file, "<H2> The Mean statistics are</H2>")?;
        writeln!(
            html_file,
            "<table><tr><th></th><th>algorithm</th><th>net time</th>{}<th>idle time</th></tr>",
            tags.iter()
                .map(|t| format!("<th>{}</th>", t))
                .collect::<String>()
        )?;
        for (name, total_time, idle_time, algo_color) in izip!(
            //for (name, total_time, sequential_times, idle_time, algo_color) in izip!(
            self.labels.iter(),
            statistics.total_times(),
            //statistics.sequential_times(),
            statistics.idle_times(),
            HISTOGRAM_COLORS.iter().cycle()
        ) {
            writeln!(
                html_file,
                "<tr><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>",
                // "<tr><td>{}</td><td>{}</td><td>{}</td>{}<td>{}</td></tr>",
                format!("<span style='color:{}'>&#9632;</span>", algo_color),
                name,
                time_string(total_time),
                //(0..tags.len())
                //    .map(|i| sequential_times.get(&i).unwrap_or(&0))
                //    .map(|t| format!("<td>{}</td>", time_string(*t)))
                //    .collect::<String>(),
                time_string(idle_time)
            )?;
        }
        writeln!(html_file, "</table>",)?;
        writeln!(html_file, "<H2> The Median statistics are</H2>")?;
        writeln!(
            html_file,
            "<table><tr><th></th><th>algorithm</th><th>net time</th>{}<th>idle time</th></tr>",
            tags.iter()
                .map(|t| format!("<th>{}</th>", t))
                .collect::<String>()
        )?;
        for (name, total_time, idle_time, algo_color) in izip!(
            // for (name, total_time, sequential_times, idle_time, algo_color) in izip!(
            self.labels.iter(),
            statistics.total_times_median(),
            // statistics.sequential_times_median(),
            statistics.idle_times_median(),
            HISTOGRAM_COLORS.iter().cycle()
        ) {
            writeln!(
                html_file,
                //"<tr><td>{}</td><td>{}</td><td>{}</td>{}<td>{}</td></tr>",
                "<tr><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>",
                format!("<span style='color:{}'>&#9632;</span>", algo_color),
                name,
                time_string(total_time),
                // (0..tags.len())
                //     .map(|i| sequential_times.get(&i).unwrap_or(&0))
                //     .map(|t| format!("<td>{}</td>", time_string(*t)))
                //     .collect::<String>(),
                time_string(idle_time)
            )?;
        }
        writeln!(html_file, "</table>",)?;
        if self.display_preferences.iter().any(|b| *b) {
            writeln!(html_file, "<H2>Comparing median runs</H2>")?;
            let median_index = (self.runs_number) / 2;
            for (pos, (log, name)) in self.logs.iter().zip(self.labels.iter()).enumerate() {
                if self.display_preferences[pos] {
                    let scene = visualisation(&log[median_index]);
                    writeln!(html_file, "<H3 align=\"left\"><u>{}</u> :</H3>", name)?;
                    fill_svg_file(&scene, &mut html_file)?;
                    writeln!(html_file, "<p>")?;
                }
            }

            writeln!(html_file, "<H2>Comparing best runs</H2>")?;
            for (pos, (log, name)) in self.logs.iter().zip(self.labels.iter()).enumerate() {
                if self.display_preferences[pos] {
                    let scene = visualisation(&log[0]);
                    writeln!(html_file, "<H3 align=\"left\"><u>{}</u> :</H3>", name)?;
                    fill_svg_file(&scene, &mut html_file)?;
                    writeln!(html_file, "<p>")?;
                }
            }
            write!(html_file, "</body></html>")?;
        }
        Ok(())
    }
}

fn time_string(nano: u64) -> String {
    match nano {
        n if n < 1_000 => format!("{}ns", n),
        n if n < 1_000_000 => format!("{}us", n / 1_000),
        n if n < 1_000_000_000 => format!("{}ms", n / 1_000_000),
        n if n < 60_000_000_000 => format!("{}s", n / 1_000_000_000),
        n => format!("{}m{}s", n / 60_000_000_000, n % 60_000_000_000),
    }
}