pub mod performances {
use colored_truecolor::Colorize;
use std::{collections::HashMap, time::Instant};
pub struct Performances {}
impl Default for Performances {
fn default() -> Self {
Self::new("Calcul of performances")
}
}
impl Performances {
pub fn new(description: &str) -> Performances {
println!("\n{}\n", description.magenta().bold());
Self {}
}
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()
}
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()
}
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()
}
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()
}
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()
}
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()
}
pub fn output(&mut self) -> &mut Performances {
print!("{}", ".".white().bold());
self
}
pub fn end(&mut self) -> Result<String, String> {
print!("\n");
Ok(String::from("ok"))
}
}
}
#[cfg(test)]
mod test {
use std::{collections::HashMap, thread::sleep, time::Duration};
use crate::performances::Performances;
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);
let mut p = Performances::default();
p.f32(callback_f32);
p.f64(callback_f64);
p.millis(callback_millis);
p.nanos(callback_nanos);
p.micros(callback_micros);
p.secs(callback_secs);
assert!(p.end().is_ok());
}
}