testing_performance 0.1.5

A performances testing library
Documentation
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("Calcul of performances")
        }
    }
    ///
    /// To run performnce test
    ///
    impl Performances {
        ///
        /// Constructor
        ///
        pub fn new(description: &str) -> Performances {
            println!("\n{}\n", description.magenta().bold());
            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()
        }

        ///
        /// # 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()
        }

        ///
        /// # 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()
        }

        ///
        /// # 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()
        }

        ///
        /// # 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()
        }

        ///
        /// # 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 {} ms and the expected time is {} ms",
                    end,
                    v
                );
            }
            self.output()
        }

        ///
        /// # Print a point in console after a test runned successfully
        ///
        fn output(&mut self) -> &mut Performances {
            print!("{}", ".".white().bold());
            self
        }

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

    #[macro_export]
    macro_rules! millis {
        ($callbacks:expr) => {
            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
                );
            }
        };
    }

    #[macro_export]
    macro_rules! micros {
        ($callbacks:expr) => {
            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
                );
            }
        };
    }

    #[macro_export]
    macro_rules! nanos {
        ($callbacks:expr) => {
            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
                );
            }
        };
    }

    #[macro_export]
    macro_rules! f32 {
        ($callbacks:expr) => {
            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
                );
            }
        };
    }

    #[macro_export]
    macro_rules! f64 {
        ($callbacks:expr) => {
            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
                );
            }
        };
    }

    #[macro_export]
    macro_rules! secs {
        ($callbacks:expr) => {
            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 {} s and the expected time is {} s",
                    end,
                    v
                );
            }
        };
    }
}

#[cfg(test)]
mod test {
    use std::time::Instant;
    use crate::f32;
    use crate::f64;
    use crate::micros;
    use crate::millis;
    use crate::nanos;
    use crate::secs;
    use std::{collections::HashMap, thread::sleep, time::Duration};

    fn live() {
        let t = Duration::from_secs_f32(5.0f32);
        sleep(t);
    }

    fn life() {
        let t = Duration::from_secs_f64(5.0f64);
        sleep(t);
    }
    fn like() {
        let t = Duration::from_micros(5_0);
        sleep(t);
    }

    fn wife() {
        let t = Duration::from_nanos(5_0);
        sleep(t);
    }

    fn knife() {
        let t = Duration::from_millis(5_0);
        sleep(t);
    }
    fn chipher() {
        let t = Duration::from_secs(1);
        sleep(t);
    }

    #[test]
    pub fn all() {
        let mut callback_f32: HashMap<fn(), f32> = HashMap::new();
        let mut callback_f64: HashMap<fn(), f64> = HashMap::new();
        let mut callback_millis: HashMap<fn(), u128> = HashMap::new();
        let mut callback_nanos: HashMap<fn(), u128> = HashMap::new();
        let mut callback_micros: HashMap<fn(), u128> = HashMap::new();
        let mut callback_secs: HashMap<fn(), u64> = HashMap::new();

        callback_f32.insert(live, 7.0f32);
        callback_f64.insert(life, 7.0f64);
        callback_millis.insert(knife, 7_000_000);
        callback_micros.insert(like, 7_000_000);
        callback_nanos.insert(wife, 7_000_000);
        callback_secs.insert(chipher, 7);

        f32!(callback_f32);
        f64!(callback_f64);
        millis!(callback_millis);
        nanos!(callback_nanos);
        micros!(callback_micros);
        secs!(callback_secs);
    }
}