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
//! Benchmark struct implementation, update functions and generic
//! function benchmark.

use std::fmt::{Display, Formatter, Result};
use std::ops::Add;
use std::ops::Sub;
use std::time::Instant;

/// Holds the times for read, write and blend operations.
#[derive(Clone)]
pub struct Benchmark {
    blend_time: f64,
    read_png_time: f64,
    write_png_time: f64,
}

impl Benchmark {
    /// Creates a new instance of the Benchmark struct with
    /// counters set to zero.
    pub fn new() -> Self {
        Benchmark {
            blend_time: 0.0,
            read_png_time: 0.0,
            write_png_time: 0.0,
        }
    }

    /// Returns the total time (in milliseconds), i.e., the sum of read,
    /// write and blend times.
    pub fn total(&self) -> f64 {
        self.blend_time + self.read_png_time + self.write_png_time
    }

    /// Executes the function to benchmark and adds the time spent
    /// to a certain counter with the given `target_fn`.
    ///
    /// ```no_run
    /// use pconvert_rust::benchmark::Benchmark;
    /// use pconvert_rust::utils::read_png_from_file;
    ///
    /// let mut benchmark = Benchmark::new();
    /// let demultiply = false;
    /// let path = "path/to/file.png".to_owned();
    /// let top = benchmark.execute(Benchmark::add_read_png_time, || {
    ///    read_png_from_file(path, demultiply)
    /// }).unwrap();
    /// ```
    pub fn execute<F, T, H>(&mut self, update_fn: H, target_fn: F) -> T
    where
        F: FnOnce() -> T,
        H: FnOnce(&mut Self, f64),
    {
        // saves beginning Instant, executes the target function,
        // measures time spent and updates the benchmark struct according
        // to the update function (read, write or blend time)
        let start = Instant::now();
        let result = target_fn();
        let duration = start.elapsed().as_micros() as f64;
        update_fn(self, duration / 1000.0);
        result
    }

    /// Adds time spent blending to the blend time counter.
    pub fn add_blend_time(benchmark: &mut Benchmark, blend_time: f64) {
        benchmark.blend_time += blend_time;
    }

    /// Adds time spent reading to the read time counter.
    pub fn add_read_png_time(benchmark: &mut Benchmark, read_png_time: f64) {
        benchmark.read_png_time += read_png_time;
    }

    /// Adds time spent writing to the write time counter.
    pub fn add_write_png_time(benchmark: &mut Benchmark, write_png_time: f64) {
        benchmark.write_png_time += write_png_time;
    }
}

impl Default for Benchmark {
    fn default() -> Self {
        Benchmark::new()
    }
}

impl Display for Benchmark {
    fn fmt(&self, fmt: &mut Formatter) -> Result {
        fmt.write_str(&format!(
            "{:.2}ms (blend {:.2}ms, read {:.2}ms, write {:.2}ms)",
            self.total(),
            self.blend_time,
            self.read_png_time,
            self.write_png_time
        ))?;
        Ok(())
    }
}

impl Add for Benchmark {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        Self {
            blend_time: self.blend_time + other.blend_time,
            read_png_time: self.read_png_time + other.read_png_time,
            write_png_time: self.write_png_time + other.write_png_time,
        }
    }
}

impl Sub for Benchmark {
    type Output = Self;

    fn sub(self, other: Self) -> Self {
        Self {
            blend_time: self.blend_time - other.blend_time,
            read_png_time: self.read_png_time - other.read_png_time,
            write_png_time: self.write_png_time - other.write_png_time,
        }
    }
}