mirror of
https://github.com/sharkdp/hyperfine.git
synced 2024-11-25 08:33:59 +03:00
Code style update
This commit is contained in:
parent
d4ebdd7b40
commit
197f9fbccb
39
src/main.rs
39
src/main.rs
@ -10,20 +10,24 @@ use ansi_term::Colour::{Cyan, Green, Red};
|
||||
use clap::{App, AppSettings, Arg};
|
||||
|
||||
struct CmdResult {
|
||||
duration_sec: f64,
|
||||
/// Execution time in seconds
|
||||
execution_time_sec: f64,
|
||||
|
||||
/// True if the command finished with exit code zero
|
||||
success: bool,
|
||||
}
|
||||
|
||||
impl CmdResult {
|
||||
fn new(duration_sec: f64, success: bool) -> CmdResult {
|
||||
fn new(execution_time_sec: f64, success: bool) -> CmdResult {
|
||||
CmdResult {
|
||||
duration_sec,
|
||||
execution_time_sec,
|
||||
success,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run_shell_command(shell_cmd: &str) -> CmdResult {
|
||||
/// Run the given shell command and measure the execution time
|
||||
fn time_shell_command(shell_cmd: &str) -> CmdResult {
|
||||
let start = Instant::now();
|
||||
|
||||
let status = Command::new("sh")
|
||||
@ -36,9 +40,9 @@ fn run_shell_command(shell_cmd: &str) -> CmdResult {
|
||||
|
||||
let duration = start.elapsed();
|
||||
|
||||
let duration_sec = duration.as_secs() as f64 + duration.subsec_nanos() as f64 * 1e-9;
|
||||
let execution_time_sec = duration.as_secs() as f64 + duration.subsec_nanos() as f64 * 1e-9;
|
||||
|
||||
CmdResult::new(duration_sec, status.success())
|
||||
CmdResult::new(execution_time_sec, status.success())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@ -58,7 +62,12 @@ fn main() {
|
||||
let min_time_sec = 5.0;
|
||||
let min_runs = 10;
|
||||
|
||||
let progressbar_style = ProgressStyle::default_spinner()
|
||||
.tick_chars("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏")
|
||||
.template(" {spinner} {msg:<30} {wide_bar} ETA {eta_precise}");
|
||||
|
||||
let commands = matches.values_of("command").unwrap();
|
||||
|
||||
for cmd in commands {
|
||||
println!("Command: {}", Cyan.paint(cmd));
|
||||
println!();
|
||||
@ -67,18 +76,14 @@ fn main() {
|
||||
|
||||
// Set up progress bar
|
||||
let bar = ProgressBar::new(min_runs);
|
||||
bar.set_style(
|
||||
ProgressStyle::default_spinner()
|
||||
.tick_chars("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏")
|
||||
.template(" {spinner} {msg:<30} {wide_bar} ETA {eta_precise}"),
|
||||
);
|
||||
bar.set_style(progressbar_style.clone());
|
||||
bar.enable_steady_tick(80);
|
||||
bar.set_message("Initial time measurement");
|
||||
|
||||
// Initial run
|
||||
let res = run_shell_command(cmd);
|
||||
let res = time_shell_command(cmd);
|
||||
|
||||
let runs_in_min_time = (min_time_sec / res.duration_sec) as u64;
|
||||
let runs_in_min_time = (min_time_sec / res.execution_time_sec) as u64;
|
||||
|
||||
let count = if runs_in_min_time >= min_runs {
|
||||
runs_in_min_time
|
||||
@ -93,20 +98,20 @@ fn main() {
|
||||
|
||||
for _ in 1..count {
|
||||
bar.inc(1);
|
||||
let res = run_shell_command(cmd);
|
||||
let res = time_shell_command(cmd);
|
||||
results.push(res);
|
||||
}
|
||||
bar.finish_and_clear();
|
||||
|
||||
let t_sum: f64 = results.iter().map(|r| r.duration_sec).sum();
|
||||
let t_sum: f64 = results.iter().map(|r| r.execution_time_sec).sum();
|
||||
let t_mean = t_sum / (results.len() as f64);
|
||||
|
||||
let t2_sum: f64 = results.iter().map(|r| r.duration_sec.powi(2)).sum();
|
||||
let t2_sum: f64 = results.iter().map(|r| r.execution_time_sec.powi(2)).sum();
|
||||
let t2_mean = t2_sum / (results.len() as f64);
|
||||
|
||||
let stddev = (t2_mean - t_mean.powi(2)).sqrt();
|
||||
|
||||
let time_fmt = format!("({:.3} ± {:.3}) s", t_mean, stddev);
|
||||
let time_fmt = format!("{:.3} s ± {:.3} s", t_mean, stddev);
|
||||
|
||||
println!(" Time: {}", Green.paint(time_fmt));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user