Skip to content
Myr

Initial changes to skip libc for cpu time on windows

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

Mark more non-windows as conditional

authored by Steve Pentland · committed by David Peter · parent 5b755f9 · Browse files · 4 files+83 added−13 removed
This commit

State

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

Diff stats

4 files+83 added−13 removed

  • other:+83 added−13 removed

4 files+83 added−13 removed

modified:src/hyperfine/benchmark.rs+76 added-10 removed
7 unmodified lines
8use hyperfine::internal::{get_progress_bar, max, min, CmdFailureAction, HyperfineOptions,9 OutputStyleOption, Second, MIN_EXECUTION_TIME};10use hyperfine::warnings::Warnings;11use hyperfine::format::{format_duration, format_duration_unit};12use hyperfine::cputime::{cpu_time_interval, get_cpu_times};12use hyperfine::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};1314#[cfg(not(target_os = "windows"))]15use hyperfine::cputime::{cpu_time_interval, get_cpu_times};1617/// Results from timing a single shell command18#[derive(Debug, Copy, Clone)]19pub struct TimingResult {20 /// Wall clock time
14 unmodified lines
35 time - shell_spawning_time36 }37}383940/// Run the given shell command and measure the execution time41#[cfg(not(target_os = "windows"))]42pub fn time_shell_command(43 shell_cmd: &str,44 failure_action: CmdFailureAction,45 shell_spawning_time: Option<TimingResult>,
44 unmodified lines
90 status.success(),91 ))92}9394/// 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("sh")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: 0,133 time_system: 0,134 },135 status.success(),136 ))137}138139/// Measure the average shell spawning time140pub fn mean_shell_spawning_time(style: &OutputStyleOption) -> io::Result<TimingResult> {141 const COUNT: u64 = 200;142 let progress_bar = get_progress_bar(COUNT, "Measuring shell spawning time", style);
157 unmodified lines
300301 let (user_str, user_unit) = format_duration_unit(user_mean, None);302 let system_str = format_duration(system_mean, Some(user_unit));303255 println!(256 " Time ({} ± {}): {:>8} ± {:>8} [User: {}, System: {}]",257 "mean".green().bold(),258 "σ".green(),259 mean_str.green().bold(),260 stddev_str.green(),261 user_str.blue(),262 system_str.blue()263 );304 output_times(mean_str, stddev_str, user_str, system_str);305306 println!(" ");307308 println!(309 " Range ({} … {}): {:>8} … {:>8}",
35 unmodified lines
345 println!(" ");346347 Ok(())348}349350#[cfg(not(target_os = "windows"))]351fn output_times(mean_str: String, stddev_str: String, user_str: String, system_str: String) {352 println!(353 " Time ({} ± {}): {:>8} ± {:>8} [User: {}, System: {}]",354 "mean".green().bold(),355 "σ".green(),356 mean_str.green().bold(),357 stddev_str.green(),358 user_str.blue(),359 system_str.blue()360 );361}362363#[cfg(target_os = "windows")]364fn output_times(mean_str: String, stddev_str: String, _user_str: String, _system_str: String) {365 println!(366 " Time ({} ± {}): {:>8} ± {:>8}",367 "mean".green().bold(),368 "σ".green(),369 mean_str.green().bold(),370 stddev_str.green(),371 );372}
modified:src/hyperfine/cputime.rs+1 added
1use libc::{getrusage, rusage, RUSAGE_CHILDREN};23use std::mem;45use hyperfine::internal::Second;6
69 unmodified lines
modified:src/main.rs+3 added-2 removed
1extern crate atty;22#[macro_use]3extern crate clap;4extern crate colored;5extern crate indicatif;7extern crate libc;6extern crate statistical;78#[cfg(not(target_os = "windows"))]9extern crate libc;1011#[cfg(test)]12#[macro_use]13extern crate approx;14
149 unmodified lines
modified:Cargo.toml+3 added-1 removed