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
Provenance
- Commit
1ddf729- Tree
f9e5d92- Author
Steve Pentland <stevepentland@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
5b755f9
4 files changed+83 added−13 removed
modified:src/hyperfine/benchmark.rs+76 added-10 removed
7 unmodified lines8 use hyperfine::internal::{get_progress_bar, max, min, CmdFailureAction, HyperfineOptions,9 OutputStyleOption, Second, MIN_EXECUTION_TIME};10 use hyperfine::warnings::Warnings;11 use hyperfine::format::{format_duration, format_duration_unit};12-use hyperfine::cputime::{cpu_time_interval, get_cpu_times};12 use hyperfine::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};13 14+#[cfg(not(target_os = "windows"))]15+use hyperfine::cputime::{cpu_time_interval, get_cpu_times};16+17 /// Results from timing a single shell command18 #[derive(Debug, Copy, Clone)]19 pub struct TimingResult {20 /// Wall clock time14 unmodified lines35 time - shell_spawning_time36 }37 }38 39+40 /// Run the given shell command and measure the execution time41+#[cfg(not(target_os = "windows"))]42 pub fn time_shell_command(43 shell_cmd: &str,44 failure_action: CmdFailureAction,45 shell_spawning_time: Option<TimingResult>,44 unmodified lines90 status.success(),91 ))92 }93 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("sh")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: 0,133+ time_system: 0,134+ },135+ status.success(),136+ ))137+}138+139 /// Measure the average shell spawning time140 pub 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 lines300 301 let (user_str, user_unit) = format_duration_unit(user_mean, None);302 let system_str = format_duration(system_mean, Some(user_unit));303 255- 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);305+306 println!(" ");307 308 println!(309 " Range ({} … {}): {:>8} … {:>8}",35 unmodified lines345 println!(" ");346 347 Ok(())348 }349+350+#[cfg(not(target_os = "windows"))]351+fn 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+}362+363+#[cfg(target_os = "windows")]364+fn 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
1 use libc::{getrusage, rusage, RUSAGE_CHILDREN};2+3 use std::mem;4 5 use hyperfine::internal::Second;6 69 unmodified linesmodified:src/main.rs+3 added-2 removed
1 extern crate atty;2-2 #[macro_use]3 extern crate clap;4 extern crate colored;5 extern crate indicatif;7-extern crate libc;6 extern crate statistical;7 8+#[cfg(not(target_os = "windows"))]9+extern crate libc;10+11 #[cfg(test)]12 #[macro_use]13 extern crate approx;14 149 unmodified linesmodified:Cargo.toml+3 added-1 removed
11 unmodified lines12131415161617181920212223243 unmodified lines11 unmodified lines[dependencies]colored = "1.6"indicatif = "0.9"statistical = "0.1"libc = "0.2"atty = "0.2.2"
[target.'cfg(not(target_os = "windows"))'.dependencies]libc = "0.2"
[dependencies.clap]version = "2"default-features = falsefeatures = ["suggestions", "color", "wrap_help"]3 unmodified lines