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
Provenance
- Commit
0d46c30- Tree
d59ed26- Author
Stephen Pentland <stevepentland@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
501d7b7
1 file changed+49 added−63 removed
modified:src/hyperfine/benchmark.rs+49 added-63 removed
1 use std::io;2-use std::process::{Command, Stdio};2+use std::process::{Command, ExitStatus, Stdio};3 use std::time::Instant;4 5 use colored::*;6 use statistical::{mean, standard_deviation};30 unmodified lines37 }38 39 40 /// Run the given shell command and measure the execution time41-#[cfg(not(target_os = "windows"))]41 pub 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();48 50- 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)?;50 58- let end_cpu = get_cpu_times();51+ let (mut time_user, mut time_system) = timer();52 let duration = start.elapsed();53 54 if failure_action == CmdFailureAction::RaiseError && !status.success() {55 return Err(io::Error::new(5 unmodified lines61 62 // Real time63 let mut time_real = duration.as_secs() as f64 + (duration.subsec_nanos() as f64) * 1e-9;64 72- // 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;76-65 // 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 }71 72 Ok((73 TimingResult {4 unmodified lines78 status.success(),79 ))80 }81 94-/// Run the given shell command and measure the execution time95-#[cfg(target_os = "windows")]96-pub 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();102-103- 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()?;110-111- let duration = start.elapsed();112-113- 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- }120-121- // Real time122- let mut time_real = duration.as_secs() as f64 + (duration.subsec_nanos() as f64) * 1e-9;123-124- // 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- }128-129- Ok((130- TimingResult {131- time_real,132- time_user: 0f64,133- time_system: 0f64,134- },135- status.success(),136- ))137-}138-82 /// Measure the average shell spawning time83 pub 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 lines116 time_system: mean(×_system),117 })118 }119 120+/// Retrieve a timer Fn that starts on the initial call and stops when the121+/// returned closure is called.122+#[cfg(not(target_os = "windows"))]123+fn 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+ };130+131+ Box::new(timer)132+}133+134+/// Return a timer Fn that will always return (0,0) when called135+#[cfg(target_os = "windows")]136+fn cpu_interval_timer() -> Box<Fn() -> (f64, f64)> {137+ Box::new(|| (0f64, 0f64))138+}139+140+/// Run a standard sh command141+#[cfg(not(target_os = "windows"))]142+fn 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+}151+152+/// Run a Windows command using cmd153+#[cfg(target_os = "windows")]154+fn 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+}162+163 /// Run the command specified by `--prepare`.164 fn 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