Skip to content
Myr

Refactor, keep common code

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Stephen Pentland · committed by David Peter · parent 501d7b7 · Browse files · 1 file+49 added−63 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+49 added−63 removed

  • other:+49 added−63 removed

1 file+49 added−63 removed

modified:src/hyperfine/benchmark.rs+49 added-63 removed
1use std::io;2use std::process::{Command, Stdio};2use std::process::{Command, ExitStatus, Stdio};3use std::time::Instant;45use colored::*;6use statistical::{mean, standard_deviation};
30 unmodified lines
37}383940/// Run the given shell command and measure the execution time41#[cfg(not(target_os = "windows"))]41pub fn time_shell_command(42 shell_cmd: &str,43 failure_action: CmdFailureAction,44 shell_spawning_time: Option<TimingResult>,45) -> io::Result<(TimingResult, bool)> {46 let start = Instant::now();48 let start_cpu = get_cpu_times();47 let timer = cpu_interval_timer();4850 let status = Command::new("sh")51 .arg("-c")52 .arg(shell_cmd)53 .stdin(Stdio::null())54 .stdout(Stdio::null())55 .stderr(Stdio::null())56 .status()?;49 let status = run_shell_command(shell_cmd)?;5058 let end_cpu = get_cpu_times();51 let (mut time_user, mut time_system) = timer();52 let duration = start.elapsed();5354 if failure_action == CmdFailureAction::RaiseError && !status.success() {55 return Err(io::Error::new(
5 unmodified lines
6162 // Real time63 let mut time_real = duration.as_secs() as f64 + (duration.subsec_nanos() as f64) * 1e-9;6472 // User and system time73 let cpu_interval = cpu_time_interval(&start_cpu, &end_cpu);74 let mut time_user = cpu_interval.user;75 let mut time_system = cpu_interval.system;7665 // Correct for shell spawning time66 if let Some(spawning_time) = shell_spawning_time {67 time_real = subtract_shell_spawning_time(time_real, spawning_time.time_real);80 time_user = subtract_shell_spawning_time(cpu_interval.user, spawning_time.time_user);81 time_system = subtract_shell_spawning_time(cpu_interval.system, spawning_time.time_system);68 time_user = subtract_shell_spawning_time(time_user, spawning_time.time_user);69 time_system = subtract_shell_spawning_time(time_system, spawning_time.time_system);70 }7172 Ok((73 TimingResult {
4 unmodified lines
78 status.success(),79 ))80}8194/// Run the given shell command and measure the execution time95#[cfg(target_os = "windows")]96pub fn time_shell_command(97 shell_cmd: &str,98 failure_action: CmdFailureAction,99 shell_spawning_time: Option<TimingResult>,100) -> io::Result<(TimingResult, bool)> {101 let start = Instant::now();102103 let status = Command::new("cmd")104 // .arg("-c")105 .arg(shell_cmd)106 .stdin(Stdio::null())107 .stdout(Stdio::null())108 .stderr(Stdio::null())109 .status()?;110111 let duration = start.elapsed();112113 if failure_action == CmdFailureAction::RaiseError && !status.success() {114 return Err(io::Error::new(115 io::ErrorKind::Other,116 "Command terminated with non-zero exit code. \117 Use the '-i'/'--ignore-failure' option if you want to ignore this.",118 ));119 }120121 // Real time122 let mut time_real = duration.as_secs() as f64 + (duration.subsec_nanos() as f64) * 1e-9;123124 // Correct for shell spawning time125 if let Some(spawning_time) = shell_spawning_time {126 time_real = subtract_shell_spawning_time(time_real, spawning_time.time_real);127 }128129 Ok((130 TimingResult {131 time_real,132 time_user: 0f64,133 time_system: 0f64,134 },135 status.success(),136 ))137}13882/// Measure the average shell spawning time83pub fn mean_shell_spawning_time(style: &OutputStyleOption) -> io::Result<TimingResult> {84 const COUNT: u64 = 200;85 let progress_bar = get_progress_bar(COUNT, "Measuring shell spawning time", style);
30 unmodified lines
116 time_system: mean(&times_system),117 })118}119120/// Retrieve a timer Fn that starts on the initial call and stops when the121/// returned closure is called.122#[cfg(not(target_os = "windows"))]123fn cpu_interval_timer() -> Box<Fn() -> (f64, f64)> {124 let start_cpu = get_cpu_times();125 let timer = move || {126 let end_cpu = get_cpu_times();127 let cpu_interval = cpu_time_interval(&start_cpu, &end_cpu);128 (cpu_interval.user, cpu_interval.system)129 };130131 Box::new(timer)132}133134/// Return a timer Fn that will always return (0,0) when called135#[cfg(target_os = "windows")]136fn cpu_interval_timer() -> Box<Fn() -> (f64, f64)> {137 Box::new(|| (0f64, 0f64))138}139140/// Run a standard sh command141#[cfg(not(target_os = "windows"))]142fn run_shell_command(command: &str) -> io::Result<ExitStatus> {143 Command::new("sh")144 .arg("-c")145 .arg(command)146 .stdin(Stdio::null())147 .stdout(Stdio::null())148 .stderr(Stdio::null())149 .status()150}151152/// Run a Windows command using cmd153#[cfg(target_os = "windows")]154fn run_shell_command(command: &str) -> io::Result<ExitStatus> {155 Command::new("cmd")156 .arg(command)157 .stdin(Stdio::null())158 .stdout(Stdio::null())159 .stderr(Stdio::null())160 .status()161}162163/// Run the command specified by `--prepare`.164fn run_preparation_command(command: &Option<String>) -> io::Result<()> {165 if let &Some(ref preparation_command) = command {166 let res = time_shell_command(preparation_command, CmdFailureAction::RaiseError, None);
192 unmodified lines