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
Provenance
- Commit
6c7e102- Tree
b2e1d43- Author
Steve Pentland <stevepentland@gmail.com>- Committer
sharkdp <davidpeter@web.de>- Parent
36cc649
6 files changed+117 added−72 removed
modified:src/hyperfine/timer/mod.rs+23 added-5 removed
1 unmodified line2 3 pub mod wallclocktimer;4 5 #[cfg(not(windows))]6-pub mod unix_timer;6+mod unix_timer;7 8+#[cfg(not(windows))]9+pub use self::unix_timer::get_cpu_timer;10+11 #[cfg(windows)]9-pub mod windows_timer;12+mod windows_timer;13 11-/// Defines a general timer with start/stop functionality.12-pub trait Timer {13- type Result;14+#[cfg(windows)]15+pub use self::windows_timer::get_cpu_timer;16 17+use std::process::Child;18+19+pub trait Timer20+where21+ Self: TimerStart + TimerStop,22+{23+}24+25+/// Defines start functionality of a timer.26+pub trait TimerStart {27 fn start() -> Self;28+ fn start_for_process(process: &Child) -> Self;29+}30+31+/// Defines stop functionality of a timer.32+pub 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))]2 3 use super::internal::{CPUInterval, CPUTimes};4-use hyperfine::timer::Timer;4+use hyperfine::timer::{TimerStart, TimerStop};5 use hyperfine::internal::Second;6 7 use std::mem;8+use std::process::Child;9 10+pub fn get_cpu_timer() -> Box<TimerStop<Result = (Second, Second)>> {11+ Box::new(CPUTimer::start())12+}13+9-pub struct CPUTimer {14+struct CPUTimer {15 start_cpu: CPUTimes,16 }17 13-impl Timer for CPUTimer {14- type Result = (Second, Second);15-18+impl TimerStart for CPUTimer {19 fn start() -> Self {20 CPUTimer {21 start_cpu: get_cpu_times(),22 }23 }24 25+ fn start_for_process(_: &Child) -> Self {26+ Self::start()27+ }28+}29+30+impl TimerStop for CPUTimer {31+ type Result = (Second, Second);32+33 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 linesmodified:src/hyperfine/timer/wallclocktimer.rs+11 added-4 removed
1+use std::process::Child;2 use std::time::Instant;3 4 use hyperfine::internal::Second;4-use hyperfine::timer::Timer;5+use hyperfine::timer::{TimerStart, TimerStop};6 7 pub struct WallClockTimer {8 start: Instant,9 }10 10-impl Timer for WallClockTimer {11- type Result = Second;12-11+impl TimerStart for WallClockTimer {12 fn start() -> WallClockTimer {13 WallClockTimer {14 start: Instant::now(),15 }16 }17 18+ fn start_for_process(_: &Child) -> Self {19+ Self::start()20+ }21+}22+23+impl TimerStop for WallClockTimer {24+ type Result = Second;25+26 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 linemodified:src/hyperfine/timer/windows_timer.rs+17 added-11 removed
1 #![cfg(windows)]2 3 use super::internal::CPUTimes;4 use hyperfine::internal::Second;5+use hyperfine::timer::{TimerStart, TimerStop};6 7 use winapi::um::processthreadsapi::GetProcessTimes;8 use winapi::um::winnt::HANDLE;9 10 use std::mem;10-use std::os::windows::io::RawHandle;11+use std::os::windows::io::{AsRawHandle, RawHandle};12+use std::process::Child;13 14 const HUNDRED_NS_PER_MS: i64 = 10;15 14-pub trait WindowsTimer {15- type Result;16-17- fn start(handle: RawHandle) -> Self;18- fn stop(&self) -> Self::Result;16+pub fn get_cpu_timer(process: &Child) -> Box<TimerStop<Result=(Second,Second)>> {17+ Box::new(CPUTimer::start_for_process(process))18 }19 21-pub struct CPUTimer {20+struct CPUTimer {21 handle: RawHandle,22 }23 25-impl WindowsTimer for CPUTimer {26- type Result = (Second, Second);24+impl TimerStart for CPUTimer {25 28- fn start(handle: RawHandle) -> Self {29- CPUTimer { handle }26+ fn start() -> Self {27+ panic!()28 }29 30+ fn start_for_process(process: &Child) -> Self {31+ CPUTimer { handle: process.as_raw_handle() }32+ } 33+}34+35+impl TimerStop for CPUTimer {36+ type Result = (Second, Second);37+38 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 linesmodified:src/hyperfine/benchmark.rs+2 added-44 removed
1 use std::io;2-use std::process::ExitStatus;2 3 use colored::*;4 use statistical::{mean, standard_deviation};5 1 unmodified line7 OutputStyleOption, Second, MIN_EXECUTION_TIME};8 use hyperfine::warnings::Warnings;9 use hyperfine::format::{format_duration, format_duration_unit};10 use hyperfine::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};12-use hyperfine::timer::Timer;11+use hyperfine::timer::{TimerStart, TimerStop};12 use hyperfine::timer::wallclocktimer::WallClockTimer;14-use hyperfine::shell::run_shell_command;13+use hyperfine::shell::execute_and_time;14 15 16 /// Results from timing a single shell command17 #[derive(Debug, Copy, Clone)]273 unmodified lines291 println!(" ");292 293 Ok(())294 }296-297-#[derive(Debug, Copy, Clone)]298-struct ExecuteResult {299- user_time: f64,300- system_time: f64,301- status: ExitStatus,302-}303-304-#[cfg(windows)]305-fn execute_and_time(command: &str) -> io::Result<ExecuteResult> {306- use hyperfine::timer::windows_timer::{CPUTimer, WindowsTimer};307- use std::os::windows::io::AsRawHandle;308-309- let mut child = run_shell_command(command)?;310- let timer = CPUTimer::start(child.as_raw_handle());311- let status = child.wait()?;312-313- let (user_time, system_time) = timer.stop();314- Ok(ExecuteResult {315- user_time,316- system_time,317- status,318- })319-}320-321-#[cfg(not(windows))]322-fn execute_and_time(command: &str) -> io::Result<ExecuteResult> {323- use hyperfine::timer::unix_timer::CPUTimer;324-325- let cpu_timer = CPUTimer::start();326-327- let status = run_shell_command(command)?;328-329- let (user_time, system_time) = cpu_timer.stop();330-331- Ok(ExecuteResult {332- user_time,333- system_time,334- status,335- })336-}modified:src/hyperfine/shell.rs+48 added-3 removed
1 use std;2 use std::io;3-use std::process::{Command, Stdio};3+use std::process::{Command, ExitStatus, Stdio};4 5+use hyperfine::timer::get_cpu_timer;6 7+/// Used to indicate the result of running a command8+#[derive(Debug, Copy, Clone)]9+pub struct ExecuteResult {10+ /// The amount of user time the process used11+ pub user_time: f64,12+13+ /// The amount of cpu time the process used14+ pub system_time: f64,15+16+ /// The exit status of the process17+ pub status: ExitStatus,18+}19+20+/// Execute the given command and return a timing summary21+#[cfg(windows)]22+pub 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()?;26+27+ let (user_time, system_time) = cpu_timer.stop();28+ Ok(ExecuteResult {29+ user_time,30+ system_time,31+ status,32+ })33+}34+35+/// Execute the given command and return a timing summary36+#[cfg(not(windows))]37+pub fn execute_and_time(command: &str) -> io::Result<ExecuteResult> {38+ let cpu_timer = get_cpu_timer();39+40+ let status = run_shell_command(command)?;41+42+ let (user_time, system_time) = cpu_timer.stop();43+44+ Ok(ExecuteResult {45+ user_time,46+ system_time,47+ status,48+ })49+}50+51 /// Run a standard shell command52 #[cfg(not(windows))]8-pub fn run_shell_command(command: &str) -> io::Result<std::process::ExitStatus> {53+fn 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 lines61 }62 63 /// Run a Windows shell command using cmd.exe64 #[cfg(windows)]20-pub fn run_shell_command(command: &str) -> io::Result<std::process::Child> {65+fn 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