/
time_summary.rs
52 lines (47 loc) · 1.63 KB
/
time_summary.rs
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
//! # time_summary
//!
//! Prints out the time summary for the flow.
//!
use crate::types::{CliArgs, Config};
use envmnt;
use std::time::SystemTime;
pub(crate) fn add(time_summary: &mut Vec<(String, u128)>, name: &str, start_time: SystemTime) {
match start_time.elapsed() {
Ok(elapsed) => time_summary.push((name.to_string(), elapsed.as_millis())),
_ => (),
};
}
pub(crate) fn print(time_summary: &Vec<(String, u128)>) {
if envmnt::is("CARGO_MAKE_PRINT_TIME_SUMMARY") {
let mut total_time = 0;
let mut max_name_size = 0;
for entry in time_summary {
total_time = total_time + entry.1;
let name_size = entry.0.len();
if max_name_size < name_size {
max_name_size = name_size;
}
}
info!("================Time Summary================");
for entry in time_summary {
let percentage = (entry.1 as f64 / total_time as f64) * 100.0;
let seconds = entry.1 as f64 / 1000.0;
let name_size = entry.0.len();
let gap_size = max_name_size - name_size + 3;
let gap = format!("{: <1$}", "", gap_size);
info!(
"{}:{}{:.2}%\t {:.2} seconds",
entry.0, gap, percentage, seconds
);
}
info!("============================================");
}
}
pub(crate) fn init(config: &Config, cli_args: &CliArgs) {
if config.config.time_summary.unwrap_or(false)
|| cli_args.print_time_summary
|| envmnt::is("CARGO_MAKE_CI")
{
envmnt::set_bool("CARGO_MAKE_PRINT_TIME_SUMMARY", true);
}
}