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
pub mod performances {
    use colored_truecolor::Colorize;
    use std::collections::HashMap;
    use std::time::Instant;

    pub struct Performances {}

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

    ///
    /// To run performance test
    ///
    impl Performances {
        ///
        /// Constructor
        ///
        pub fn new() -> Performances {
            println!();
            Self {}
        }

        ///
        /// # Check if all callback's time are less than expected time
        ///
        /// - `callbacks`   The expected callback with the expected time in f32s unit
        ///
        ///
        pub fn f32(&mut self, callbacks: HashMap<fn(), f32>) -> &mut Performances {
            for (&k, &v) in callbacks.iter() {
                let now: Instant = Instant::now();
                k();
                let end: f32 = now.elapsed().as_secs_f32();
                assert!(
                    end < v,
                    "A callback take {} f32s and the expected time is {} f32s",
                    end,
                    v
                );
                Self::output("The callback take less than the expected max time");
            }
            Self::output("End of f32 callback measure test");
            self
        }

        ///
        /// # Check if all callback's time are less than expected time
        ///
        /// - `callbacks`   The expected callback with the expected time in f64s unit
        ///
        ///
        pub fn f64(&mut self, callbacks: HashMap<fn(), f64>) -> &mut Performances {
            for (&k, &v) in callbacks.iter() {
                let now: Instant = Instant::now();
                k();
                let end: f64 = now.elapsed().as_secs_f64();
                assert!(
                    end < v,
                    "A callback take {} f64s and the expected time is {} f64s",
                    end,
                    v
                );
                Self::output("The callback take less than the expected max time");
            }
            Self::output("End of f64 callback measure test");
            self
        }

        ///
        /// # Check if all callback's time are less than expected time
        ///
        /// - `callbacks`   The expected callback with the expected time in nanos unit
        ///
        ///
        pub fn nanos(&mut self, callbacks: HashMap<fn(), u128>) -> &mut Performances {
            for (&k, &v) in callbacks.iter() {
                let now: Instant = Instant::now();
                k();
                let end: u128 = now.elapsed().as_nanos();
                assert!(
                    end < v,
                    "A callback take {} ns and the expected time is {} ns",
                    end,
                    v
                );
                Self::output("The callback take less than the expected max time");
            }
            Self::output("End of nanos callback measure test");
            self
        }

        ///
        /// # Check if all callback's time are less than expected time
        ///
        /// - `callbacks`   The expected callback with the expected time in micros seconds unit
        ///
        pub fn micros(&mut self, callbacks: HashMap<fn(), u128>) -> &mut Performances {
            for (&k, &v) in callbacks.iter() {
                let now: Instant = Instant::now();
                k();
                let end: u128 = now.elapsed().as_micros();
                assert!(
                    end < v,
                    "A callback take {} µs and the expected time is {} µs",
                    end,
                    v
                );
                Self::output("The callback take less than the expected max time");
            }
            Self::output("End of micros callback measure test");
            self
        }

        ///
        /// # Check if all callback's time are less than expected time
        ///
        /// - `callbacks`   The expected callback with the expected time in millis seconds unit
        ///
        pub fn millis(&mut self, callbacks: HashMap<fn(), u128>) -> &mut Performances {
            for (&k, &v) in callbacks.iter() {
                let now: Instant = Instant::now();
                k();
                let end: u128 = now.elapsed().as_millis();
                assert!(
                    end < v,
                    "A callback take {} ms and the expected time is {} ms",
                    end,
                    v
                );
                Self::output("The callback take less than the expected max time");
            }
            Self::output("End of millis callback measure test");
            self
        }

        ///
        /// # Check if all callback's time are less than expected time
        ///
        /// - `callbacks`   The expected callback with the expected time in seconds unit
        ///
        pub fn secs(&mut self, callbacks: HashMap<fn(), u64>) -> &mut Performances {
            for (&k, &v) in callbacks.iter() {
                let now: Instant = Instant::now();
                k();
                let end: u64 = now.elapsed().as_secs();
                assert!(
                    end < v,
                    "A callback take {} secs and the expected time is {} secs",
                    end,
                    v
                );
                Self::output("The callback take less than the expected max time");
            }
            Self::output("End of secs callback measure test");
            self
        }

        ///
        /// # Print a point in console after a test run successfully
        ///
        fn output(x: &str) {
            println!(
                "     {}",
                format!("{} {}", "[ ✓ ]".green().bold(), x.blue().bold()).as_str()
            );
        }

        ///
        /// # End of the test
        ///
        pub fn end(&mut self) -> Result<(), String> {
            println!();
            Ok(())
        }
    }

    #[macro_export]
    macro_rules! millis {
        ($callbacks:expr) => {
            let mut p = Performances::default();
            assert!(p.millis($callbacks).end().is_ok());
        };
    }

    #[macro_export]
    macro_rules! micros {
        ($callbacks:expr) => {
            let mut p = Performances::default();
            assert!(p.micros($callbacks).end().is_ok());
        };
    }

    #[macro_export]
    macro_rules! nanos {
        ($callbacks:expr) => {
            let mut p = Performances::default();
            assert!(p.nanos($callbacks).end().is_ok());
        };
    }

    #[macro_export]
    macro_rules! f32 {
        ($callbacks:expr) => {
            let mut p = Performances::default();
            assert!(p.f32($callbacks).end().is_ok());
        };
    }

    #[macro_export]
    macro_rules! f64 {
        ($callbacks:expr) => {
            let mut p = Performances::default();
            assert!(p.f64($callbacks).end().is_ok());
        };
    }

    #[macro_export]
    macro_rules! secs {
        ($callbacks:expr) => {
            let mut p = Performances::default();
            assert!(p.secs($callbacks).end().is_ok());
        };
    }
}