Skip to content
Myr

Refactor execute and measure to shell, abstract away cpu timer

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

authored by Steve Pentland · committed by sharkdp · parent 36cc649 · Browse files · 6 files+117 added−72 removed
This commit

State

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

Diff stats

6 files+117 added−72 removed

  • other:+117 added−72 removed

6 files+117 added−72 removed

modified:src/hyperfine/timer/mod.rs+23 added-5 removed
1 unmodified line
23pub mod wallclocktimer;45#[cfg(not(windows))]6pub mod unix_timer;6mod unix_timer;78#[cfg(not(windows))]9pub use self::unix_timer::get_cpu_timer;1011#[cfg(windows)]9pub mod windows_timer;12mod windows_timer;1311/// Defines a general timer with start/stop functionality.12pub trait Timer {13 type Result;14#[cfg(windows)]15pub use self::windows_timer::get_cpu_timer;1617use std::process::Child;1819pub trait Timer20where21 Self: TimerStart + TimerStop,22{23}2425/// Defines start functionality of a timer.26pub trait TimerStart {27 fn start() -> Self;28 fn start_for_process(process: &Child) -> Self;29}3031/// Defines stop functionality of a timer.32pub trait TimerStop {33 type Result;34 fn stop(&self) -> Self::Result;35}
modified:src/hyperfine/timer/unix_timer.rs+16 added-5 removed
1#![cfg(not(windows))]23use super::internal::{CPUInterval, CPUTimes};4use hyperfine::timer::Timer;4use hyperfine::timer::{TimerStart, TimerStop};5use hyperfine::internal::Second;67use std::mem;8use std::process::Child;910pub fn get_cpu_timer() -> Box<TimerStop<Result = (Second, Second)>> {11 Box::new(CPUTimer::start())12}139pub struct CPUTimer {14struct CPUTimer {15 start_cpu: CPUTimes,16}1713impl Timer for CPUTimer {14 type Result = (Second, Second);1518impl TimerStart for CPUTimer {19 fn start() -> Self {20 CPUTimer {21 start_cpu: get_cpu_times(),22 }23 }2425 fn start_for_process(_: &Child) -> Self {26 Self::start()27 }28}2930impl TimerStop for CPUTimer {31 type Result = (Second, Second);3233 fn stop(&self) -> Self::Result {34 let end_cpu = get_cpu_times();35 let cpu_interval = cpu_time_interval(&self.start_cpu, &end_cpu);36 (cpu_interval.user, cpu_interval.system)
56 unmodified lines
modified:src/hyperfine/timer/wallclocktimer.rs+11 added-4 removed
1use std::process::Child;2use std::time::Instant;34use hyperfine::internal::Second;4use hyperfine::timer::Timer;5use hyperfine::timer::{TimerStart, TimerStop};67pub struct WallClockTimer {8 start: Instant,9}1010impl Timer for WallClockTimer {11 type Result = Second;1211impl TimerStart for WallClockTimer {12 fn start() -> WallClockTimer {13 WallClockTimer {14 start: Instant::now(),15 }16 }1718 fn start_for_process(_: &Child) -> Self {19 Self::start()20 }21}2223impl TimerStop for WallClockTimer {24 type Result = Second;2526 fn stop(&self) -> Second {27 let duration = self.start.elapsed();28 duration.as_secs() as f64 + (duration.subsec_nanos() as f64) * 1e-929 }
1 unmodified line
modified:src/hyperfine/timer/windows_timer.rs+17 added-11 removed
1#![cfg(windows)]23use super::internal::CPUTimes;4use hyperfine::internal::Second;5use hyperfine::timer::{TimerStart, TimerStop};67use winapi::um::processthreadsapi::GetProcessTimes;8use winapi::um::winnt::HANDLE;910use std::mem;10use std::os::windows::io::RawHandle;11use std::os::windows::io::{AsRawHandle, RawHandle};12use std::process::Child;1314const HUNDRED_NS_PER_MS: i64 = 10;1514pub trait WindowsTimer {15 type Result;1617 fn start(handle: RawHandle) -> Self;18 fn stop(&self) -> Self::Result;16pub fn get_cpu_timer(process: &Child) -> Box<TimerStop<Result=(Second,Second)>> {17 Box::new(CPUTimer::start_for_process(process))18}1921pub struct CPUTimer {20struct CPUTimer {21 handle: RawHandle,22}2325impl WindowsTimer for CPUTimer {26 type Result = (Second, Second);24impl TimerStart for CPUTimer {2528 fn start(handle: RawHandle) -> Self {29 CPUTimer { handle }26 fn start() -> Self {27 panic!()28 }2930 fn start_for_process(process: &Child) -> Self {31 CPUTimer { handle: process.as_raw_handle() }32 } 33}3435impl TimerStop for CPUTimer {36 type Result = (Second, Second);3738 fn stop(&self) -> Self::Result {39 let times = get_cpu_times(self.handle);40 (times.user_usec as f64 * 1e-6, times.system_usec as f64 * 1e-6)41 }
33 unmodified lines
modified:src/hyperfine/benchmark.rs+2 added-44 removed
1use std::io;2use std::process::ExitStatus;23use colored::*;4use statistical::{mean, standard_deviation};5
1 unmodified line
7 OutputStyleOption, Second, MIN_EXECUTION_TIME};8use hyperfine::warnings::Warnings;9use hyperfine::format::{format_duration, format_duration_unit};10use hyperfine::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};12use hyperfine::timer::Timer;11use hyperfine::timer::{TimerStart, TimerStop};12use hyperfine::timer::wallclocktimer::WallClockTimer;14use hyperfine::shell::run_shell_command;13use hyperfine::shell::execute_and_time;141516/// Results from timing a single shell command17#[derive(Debug, Copy, Clone)]
273 unmodified lines
291 println!(" ");292293 Ok(())294}296297#[derive(Debug, Copy, Clone)]298struct ExecuteResult {299 user_time: f64,300 system_time: f64,301 status: ExitStatus,302}303304#[cfg(windows)]305fn execute_and_time(command: &str) -> io::Result<ExecuteResult> {306 use hyperfine::timer::windows_timer::{CPUTimer, WindowsTimer};307 use std::os::windows::io::AsRawHandle;308309 let mut child = run_shell_command(command)?;310 let timer = CPUTimer::start(child.as_raw_handle());311 let status = child.wait()?;312313 let (user_time, system_time) = timer.stop();314 Ok(ExecuteResult {315 user_time,316 system_time,317 status,318 })319}320321#[cfg(not(windows))]322fn execute_and_time(command: &str) -> io::Result<ExecuteResult> {323 use hyperfine::timer::unix_timer::CPUTimer;324325 let cpu_timer = CPUTimer::start();326327 let status = run_shell_command(command)?;328329 let (user_time, system_time) = cpu_timer.stop();330331 Ok(ExecuteResult {332 user_time,333 system_time,334 status,335 })336}
modified:src/hyperfine/shell.rs+48 added-3 removed
1use std;2use std::io;3use std::process::{Command, Stdio};3use std::process::{Command, ExitStatus, Stdio};45use hyperfine::timer::get_cpu_timer;67/// Used to indicate the result of running a command8#[derive(Debug, Copy, Clone)]9pub struct ExecuteResult {10 /// The amount of user time the process used11 pub user_time: f64,1213 /// The amount of cpu time the process used14 pub system_time: f64,1516 /// The exit status of the process17 pub status: ExitStatus,18}1920/// Execute the given command and return a timing summary21#[cfg(windows)]22pub fn execute_and_time(command: &str) -> io::Result<ExecuteResult> {23 let mut child = run_shell_command(command)?;24 let cpu_timer = get_cpu_timer(&child);25 let status = child.wait()?;2627 let (user_time, system_time) = cpu_timer.stop();28 Ok(ExecuteResult {29 user_time,30 system_time,31 status,32 })33}3435/// Execute the given command and return a timing summary36#[cfg(not(windows))]37pub fn execute_and_time(command: &str) -> io::Result<ExecuteResult> {38 let cpu_timer = get_cpu_timer();3940 let status = run_shell_command(command)?;4142 let (user_time, system_time) = cpu_timer.stop();4344 Ok(ExecuteResult {45 user_time,46 system_time,47 status,48 })49}5051/// Run a standard shell command52#[cfg(not(windows))]8pub fn run_shell_command(command: &str) -> io::Result<std::process::ExitStatus> {53fn run_shell_command(command: &str) -> io::Result<std::process::ExitStatus> {54 Command::new("sh")55 .arg("-c")56 .arg(command)57 .stdin(Stdio::null())
3 unmodified lines
61}6263/// Run a Windows shell command using cmd.exe64#[cfg(windows)]20pub fn run_shell_command(command: &str) -> io::Result<std::process::Child> {65fn run_shell_command(command: &str) -> io::Result<std::process::Child> {66 Command::new("cmd")67 .arg("/C")68 .arg(command)69 .stdin(Stdio::null())
4 unmodified lines