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
pub mod zuu {
use colored_truecolor::Colorize;
use progress_bar::{
finalize_progress_bar, inc_progress_bar, init_progress_bar, print_progress_bar_info,
set_progress_bar_action, Color, Style,
};
use std::cell::Cell;
use std::collections::HashMap;
use std::ops::Add;
use std::thread::sleep;
use std::time::{Duration, Instant};
#[macro_export]
macro_rules! take {
// This match captures the `expr` passed in as `$e`,
// which the macro will assume is callable (E.g. a closure or function)
($e:expr) => {
let mut x = Performances::new();
for (k, v) in $e.iter() {
x.capture(k, v);
}
x.sleep_time(500);
x.print().expect("Failed");
};
($e:expr,$t:expr) => {
let mut x = Performances::new();
for (k, v) in $e.iter() {
x.capture(k, v);
}
x.sleep_time($t);
x.print().expect("Failed");
};
}
pub struct Performances {
start: Instant,
begin: Instant,
all: Cell<usize>,
callback: HashMap<String, u128>,
sleep_time: u64,
}
impl Default for Performances {
fn default() -> Self {
Self::new()
}
}
impl Performances {
///
/// Constructor
///
///
pub fn new() -> Performances {
println!(
"\n {}\n",
"Calculate function execution time".magenta().bold()
);
Self {
start: Instant::now(),
begin: Instant::now(),
all: Cell::new(0),
callback: HashMap::new(),
sleep_time: 500,
}
}
pub fn sleep_time(&mut self, t: u64) -> &mut Performances {
self.sleep_time = t;
self
}
///
/// Capture the execution time of the callback
///
/// - `name` The function name
/// - `f` The pointer to the callback
///
///
pub fn capture<T>(&mut self, name: &str, f: &dyn Fn() -> T) -> &mut Performances {
self.all.set(self.all.get() + 1);
f();
let time = self.begin.elapsed();
self.begin = Instant::now();
self.callback.insert(String::from(name), time.as_micros());
self
}
///
///
/// Print the result
///
///```
/// use std::thread::sleep;
/// use std::time::Duration;
/// use performances::zuu::Performances;
///
/// fn kool() -> bool
/// {
/// return false;
/// }
/// fn look() -> bool
/// {
/// return true;
/// }
///
/// fn s() -> i32
/// {
/// return 0;
/// }
///
/// fn get_fans() -> i32
/// {
/// return 255;
/// }
/// fn f() -> i32
/// {
/// return 1;
/// }
///
/// fn bad()
/// {
/// sleep(Duration::from_nanos(50));
/// }
///
/// Performances::new().capture("bad", &bad)
/// .capture("kool", &kool)
/// .capture("look", &look)
/// .capture("s", &s)
/// .capture("get_fans", &get_fans)
/// .print()
/// .expect("panic message");
///```
///
pub fn print(&mut self) -> Result<(), String> {
if self.all.get() < 1 {
return Err(String::from("No captured function founded"));
}
init_progress_bar(self.all.get());
set_progress_bar_action("Loading", Color::Blue, Style::Bold);
for test in self.callback.iter() {
sleep(Duration::from_millis(self.sleep_time));
print_progress_bar_info(
test.0,
format!("{} µs", test.1.to_string().blue().bold()).as_str(),
Color::Green,
Style::Bold,
);
inc_progress_bar();
}
set_progress_bar_action("Finnish", Color::Green, Style::Bold);
finalize_progress_bar();
println!(
"\n {} {}\n",
"Execution time : ".blue().bold(),
self.start
.elapsed()
.as_secs()
.to_string()
.add(" Secs")
.cyan()
.bold()
);
Ok(())
}
}
}