sagiegurari / cargo-make
1
//! # time_summary
2
//!
3
//! Prints out the time summary for the flow.
4
//!
5

6
use crate::types::{CliArgs, Config};
7
use envmnt;
8
use std::cmp::Ordering;
9
use std::time::SystemTime;
10

11 8
pub(crate) fn add(time_summary: &mut Vec<(String, u128)>, name: &str, start_time: SystemTime) {
12 8
    match start_time.elapsed() {
13 8
        Ok(elapsed) => time_summary.push((name.to_string(), elapsed.as_millis())),
14
        _ => (),
15
    };
16 8
}
17

18 2
pub(crate) fn print(time_summary: &Vec<(String, u128)>) {
19 2
    if envmnt::is("CARGO_MAKE_PRINT_TIME_SUMMARY") {
20 2
        let mut time_summary_sorted = time_summary.clone();
21 2
        time_summary_sorted
22 2
            .sort_by(|entry1, entry2| entry2.1.partial_cmp(&entry1.1).unwrap_or(Ordering::Equal));
23

24 2
        let mut total_time = 0;
25 2
        let mut max_name_size = 0;
26 2
        for entry in &time_summary_sorted {
27 2
            total_time = total_time + entry.1;
28 2
            let name_size = entry.0.len();
29 2
            if max_name_size < name_size {
30 2
                max_name_size = name_size;
31
            }
32
        }
33

34 2
        info!("================Time Summary================");
35 2
        for entry in &time_summary_sorted {
36 2
            let percentage = (entry.1 as f64 / total_time as f64) * 100.0;
37 2
            let seconds = entry.1 as f64 / 1000.0;
38 2
            let name_size = entry.0.len();
39 2
            let gap_size = max_name_size - name_size + 3;
40 2
            let gap = format!("{: <1$}", "", gap_size);
41 2
            info!(
42 0
                "{}:{}{:.2}%\t   {:.2} seconds",
43 0
                entry.0, gap, percentage, seconds
44
            );
45 2
        }
46 2
        info!("============================================");
47 2
    }
48 2
}
49

50 2
pub(crate) fn init(config: &Config, cli_args: &CliArgs) {
51 2
    if config.config.time_summary.unwrap_or(false)
52 2
        || cli_args.print_time_summary
53 2
        || envmnt::is("CARGO_MAKE_CI")
54
    {
55 2
        envmnt::set_bool("CARGO_MAKE_PRINT_TIME_SUMMARY", true);
56
    }
57 2
}

Read our documentation on viewing source code .

Loading