performances/
performances.rs

1pub mod performances {
2    use colored_truecolor::Colorize;
3    use std::collections::HashMap;
4    use std::time::Instant;
5    pub struct Performances {}
6
7    impl Default for Performances {
8        fn default() -> Self {
9            Self::new("Calcul of performances")
10        }
11    }
12    ///
13    /// To run performnce test
14    ///
15    impl Performances {
16        ///
17        /// Constructor
18        ///
19        pub fn new(description: &str) -> Performances {
20            println!("\n{}\n", description.magenta().bold());
21            Self {}
22        }
23
24        ///
25        /// # Check if all callback's time are less than expected time
26        ///
27        /// - `callbacks`   The expected callback with the expected time in f32s unit
28        ///
29        ///
30        pub fn f32(&mut self, callbacks: HashMap<fn(), f32>) -> &mut Performances {
31            for (&k, &v) in callbacks.iter() {
32                let now: Instant = Instant::now();
33                k();
34                let end: f32 = now.elapsed().as_secs_f32();
35                assert!(
36                    end < v,
37                    "A callback take {} f32s and the expected time is {} f32s",
38                    end,
39                    v
40                );
41            }
42            self.output()
43        }
44
45        ///
46        /// # Check if all callback's time are less than expected time
47        ///
48        /// - `callbacks`   The expected callback with the expected time in f64s unit
49        ///
50        ///
51        pub fn f64(&mut self, callbacks: HashMap<fn(), f64>) -> &mut Performances {
52            for (&k, &v) in callbacks.iter() {
53                let now: Instant = Instant::now();
54                k();
55                let end: f64 = now.elapsed().as_secs_f64();
56                assert!(
57                    end < v,
58                    "A callback take {} f64s and the expected time is {} f64s",
59                    end,
60                    v
61                );
62            }
63            self.output()
64        }
65
66        ///
67        /// # Check if all callback's time are less than expected time
68        ///
69        /// - `callbacks`   The expected callback with the expected time in nanos unit
70        ///
71        ///
72        pub fn nanos(&mut self, callbacks: HashMap<fn(), u128>) -> &mut Performances {
73            for (&k, &v) in callbacks.iter() {
74                let now: Instant = Instant::now();
75                k();
76                let end: u128 = now.elapsed().as_nanos();
77                assert!(
78                    end < v,
79                    "A callback take {} ns and the expected time is {} ns",
80                    end,
81                    v
82                );
83            }
84            self.output()
85        }
86
87        ///
88        /// # Check if all callback's time are less than expected time
89        ///
90        /// - `callbacks`   The expected callback with the expected time in micros seconds unit
91        ///
92        pub fn micros(&mut self, callbacks: HashMap<fn(), u128>) -> &mut Performances {
93            for (&k, &v) in callbacks.iter() {
94                let now: Instant = Instant::now();
95                k();
96                let end: u128 = now.elapsed().as_micros();
97                assert!(
98                    end < v,
99                    "A callback take {} µs and the expected time is {} µs",
100                    end,
101                    v
102                );
103            }
104            self.output()
105        }
106
107        ///
108        /// # Check if all callback's time are less than expected time
109        ///
110        /// - `callbacks`   The expected callback with the expected time in millis seconds unit
111        ///
112        pub fn millis(&mut self, callbacks: HashMap<fn(), u128>) -> &mut Performances {
113            for (&k, &v) in callbacks.iter() {
114                let now: Instant = Instant::now();
115                k();
116                let end: u128 = now.elapsed().as_millis();
117                assert!(
118                    end < v,
119                    "A callback take {} ms and the expected time is {} ms",
120                    end,
121                    v
122                );
123            }
124            self.output()
125        }
126
127        ///
128        /// # Check if all callback's time are less than expected time
129        ///
130        /// - `callbacks`   The expected callback with the expected time in seconds unit
131        ///
132        pub fn secs(&mut self, callbacks: HashMap<fn(), u64>) -> &mut Performances {
133            for (&k, &v) in callbacks.iter() {
134                let now: Instant = Instant::now();
135                k();
136                let end: u64 = now.elapsed().as_secs();
137                assert!(
138                    end < v,
139                    "A callback take {} ms and the expected time is {} ms",
140                    end,
141                    v
142                );
143            }
144            self.output()
145        }
146
147        ///
148        /// # Print a point in console after a test runned successfully
149        ///
150        fn output(&mut self) -> &mut Performances {
151            print!("{}", ".".white().bold());
152            self
153        }
154
155        ///
156        /// # End of the test
157        ///
158        pub fn end(&mut self) -> Result<String, String> {
159            println!();
160            Ok(String::from("ok"))
161        }
162    }
163
164    #[macro_export]
165    macro_rules! millis {
166        ($callbacks:expr) => {
167            for (&k, &v) in $callbacks.iter() {
168                let now: Instant = Instant::now();
169                k();
170                let end: u128 = now.elapsed().as_millis();
171                assert!(
172                    end < v,
173                    "A callback take {} ms and the expected time is {} ms",
174                    end,
175                    v
176                );
177            }
178        };
179    }
180
181    #[macro_export]
182    macro_rules! micros {
183        ($callbacks:expr) => {
184            for (&k, &v) in $callbacks.iter() {
185                let now: Instant = Instant::now();
186                k();
187                let end: u128 = now.elapsed().as_micros();
188                assert!(
189                    end < v,
190                    "A callback take {} µs and the expected time is {} µs",
191                    end,
192                    v
193                );
194            }
195        };
196    }
197
198    #[macro_export]
199    macro_rules! nanos {
200        ($callbacks:expr) => {
201            for (&k, &v) in $callbacks.iter() {
202                let now: Instant = Instant::now();
203                k();
204                let end: u128 = now.elapsed().as_nanos();
205                assert!(
206                    end < v,
207                    "A callback take {} ns and the expected time is {} ns",
208                    end,
209                    v
210                );
211            }
212        };
213    }
214
215    #[macro_export]
216    macro_rules! f32 {
217        ($callbacks:expr) => {
218            for (&k, &v) in $callbacks.iter() {
219                let now: Instant = Instant::now();
220                k();
221                let end: f32 = now.elapsed().as_secs_f32();
222                assert!(
223                    end < v,
224                    "A callback take {} f32s and the expected time is {} f32s",
225                    end,
226                    v
227                );
228            }
229        };
230    }
231
232    #[macro_export]
233    macro_rules! f64 {
234        ($callbacks:expr) => {
235            for (&k, &v) in $callbacks.iter() {
236                let now: Instant = Instant::now();
237                k();
238                let end: f64 = now.elapsed().as_secs_f64();
239                assert!(
240                    end < v,
241                    "A callback take {} f64s and the expected time is {} f64s",
242                    end,
243                    v
244                );
245            }
246        };
247    }
248
249    #[macro_export]
250    macro_rules! secs {
251        ($callbacks:expr) => {
252            for (&k, &v) in $callbacks.iter() {
253                let now: Instant = Instant::now();
254                k();
255                let end: u64 = now.elapsed().as_secs();
256                assert!(
257                    end < v,
258                    "A callback take {} s and the expected time is {} s",
259                    end,
260                    v
261                );
262            }
263        };
264    }
265}
266
267#[cfg(test)]
268mod test {
269    use std::time::Instant;
270    use crate::f32;
271    use crate::f64;
272    use crate::micros;
273    use crate::millis;
274    use crate::nanos;
275    use crate::secs;
276    use std::{collections::HashMap, thread::sleep, time::Duration};
277
278    fn live() {
279        let t = Duration::from_secs_f32(5.0f32);
280        sleep(t);
281    }
282
283    fn life() {
284        let t = Duration::from_secs_f64(5.0f64);
285        sleep(t);
286    }
287    fn like() {
288        let t = Duration::from_micros(5_0);
289        sleep(t);
290    }
291
292    fn wife() {
293        let t = Duration::from_nanos(5_0);
294        sleep(t);
295    }
296
297    fn knife() {
298        let t = Duration::from_millis(5_0);
299        sleep(t);
300    }
301    fn chipher() {
302        let t = Duration::from_secs(1);
303        sleep(t);
304    }
305
306    #[test]
307    pub fn all() {
308        let mut callback_f32: HashMap<fn(), f32> = HashMap::new();
309        let mut callback_f64: HashMap<fn(), f64> = HashMap::new();
310        let mut callback_millis: HashMap<fn(), u128> = HashMap::new();
311        let mut callback_nanos: HashMap<fn(), u128> = HashMap::new();
312        let mut callback_micros: HashMap<fn(), u128> = HashMap::new();
313        let mut callback_secs: HashMap<fn(), u64> = HashMap::new();
314
315        callback_f32.insert(live, 7.0f32);
316        callback_f64.insert(life, 7.0f64);
317        callback_millis.insert(knife, 7_000_000);
318        callback_micros.insert(like, 7_000_000);
319        callback_nanos.insert(wife, 7_000_000);
320        callback_secs.insert(chipher, 7);
321
322        f32!(callback_f32);
323        f64!(callback_f64);
324        millis!(callback_millis);
325        nanos!(callback_nanos);
326        micros!(callback_micros);
327        secs!(callback_secs);
328    }
329}
330