pconvert_rust/
benchmark.rs

1//! Benchmark struct implementation, update functions and generic
2//! function benchmark.
3
4use std::fmt::{Display, Formatter, Result};
5use std::ops::Add;
6use std::ops::Sub;
7use std::time::Instant;
8
9/// Holds the times for read, write and blend operations.
10#[derive(Clone)]
11pub struct Benchmark {
12    blend_time: f64,
13    read_png_time: f64,
14    write_png_time: f64,
15}
16
17impl Benchmark {
18    /// Creates a new instance of the Benchmark struct with
19    /// counters set to zero.
20    pub fn new() -> Self {
21        Benchmark {
22            blend_time: 0.0,
23            read_png_time: 0.0,
24            write_png_time: 0.0,
25        }
26    }
27
28    /// Returns the total time (in milliseconds), i.e., the sum of read,
29    /// write and blend times.
30    pub fn total(&self) -> f64 {
31        self.blend_time + self.read_png_time + self.write_png_time
32    }
33
34    /// Executes the function to benchmark and adds the time spent
35    /// to a certain counter with the given `target_fn`.
36    ///
37    /// ```no_run
38    /// use pconvert_rust::benchmark::Benchmark;
39    /// use pconvert_rust::utils::read_png_from_file;
40    ///
41    /// let mut benchmark = Benchmark::new();
42    /// let demultiply = false;
43    /// let path = "path/to/file.png".to_owned();
44    /// let top = benchmark.execute(Benchmark::add_read_png_time, || {
45    ///    read_png_from_file(path, demultiply)
46    /// }).unwrap();
47    /// ```
48    pub fn execute<F, T, H>(&mut self, update_fn: H, target_fn: F) -> T
49    where
50        F: FnOnce() -> T,
51        H: FnOnce(&mut Self, f64),
52    {
53        // saves beginning Instant, executes the target function,
54        // measures time spent and updates the benchmark struct according
55        // to the update function (read, write or blend time)
56        let start = Instant::now();
57        let result = target_fn();
58        let duration = start.elapsed().as_micros() as f64;
59        update_fn(self, duration / 1000.0);
60        result
61    }
62
63    /// Adds time spent blending to the blend time counter.
64    pub fn add_blend_time(benchmark: &mut Benchmark, blend_time: f64) {
65        benchmark.blend_time += blend_time;
66    }
67
68    /// Adds time spent reading to the read time counter.
69    pub fn add_read_png_time(benchmark: &mut Benchmark, read_png_time: f64) {
70        benchmark.read_png_time += read_png_time;
71    }
72
73    /// Adds time spent writing to the write time counter.
74    pub fn add_write_png_time(benchmark: &mut Benchmark, write_png_time: f64) {
75        benchmark.write_png_time += write_png_time;
76    }
77}
78
79impl Default for Benchmark {
80    fn default() -> Self {
81        Benchmark::new()
82    }
83}
84
85impl Display for Benchmark {
86    fn fmt(&self, fmt: &mut Formatter) -> Result {
87        fmt.write_str(&format!(
88            "{:.2}ms (blend {:.2}ms, read {:.2}ms, write {:.2}ms)",
89            self.total(),
90            self.blend_time,
91            self.read_png_time,
92            self.write_png_time
93        ))?;
94        Ok(())
95    }
96}
97
98impl Add for Benchmark {
99    type Output = Self;
100
101    fn add(self, other: Self) -> Self {
102        Self {
103            blend_time: self.blend_time + other.blend_time,
104            read_png_time: self.read_png_time + other.read_png_time,
105            write_png_time: self.write_png_time + other.write_png_time,
106        }
107    }
108}
109
110impl Sub for Benchmark {
111    type Output = Self;
112
113    fn sub(self, other: Self) -> Self {
114        Self {
115            blend_time: self.blend_time - other.blend_time,
116            read_png_time: self.read_png_time - other.read_png_time,
117            write_png_time: self.write_png_time - other.write_png_time,
118        }
119    }
120}