Move to timer module
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Peter · committed by David Peter · parent 1f3943e · Browse files · 7 files·+81 added−82 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+81 added−82 removed
- other:+81 added−82 removed
Provenance
- Commit
0ffc6d7- Tree
54ff8a5- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
1f3943e
7 files changed+81 added−82 removed
modified:src/benchmark/executor.rs+1 added-2 removed
1 use std::process::{ExitStatus, Stdio};2 3 use crate::command::Command;4-use crate::execute::execute_and_measure;4 use crate::options::{CmdFailureAction, CommandOutputPolicy, Options, OutputStyleOption, Shell};5 use crate::output::progress_bar::get_progress_bar;7-use crate::timer::wallclocktimer::WallClockTimer;6+use crate::timer::{execute_and_measure, wall_clock_timer::WallClockTimer};7 use crate::util::randomized_environment_offset;8 use crate::util::units::Second;9 10 use super::timing_result::TimingResult;239 unmodified linesmodified:src/timer/mod.rs+50 added-11 removed
1-use crate::util::units::Second;1+pub mod wall_clock_timer;2 3-pub mod wallclocktimer;4-3 #[cfg(windows)]6-pub mod windows_timer;4+mod windows_timer;5 6 #[cfg(not(windows))]9-pub mod unix_timer;7+mod unix_timer;8 9+use crate::util::units::Second;10+11+use std::process::{Command, ExitStatus};12+13+use anyhow::{Context, Result};14+15 #[derive(Debug, Copy, Clone)]12-pub struct CPUTimes {16+struct CPUTimes {17 /// Total amount of time spent executing in user mode18 pub user_usec: i64,19 20 /// Total amount of time spent executing in kernel mode21 pub system_usec: i64,22 }23 24+/// Used to indicate the result of running a command25 #[derive(Debug, Copy, Clone)]21-pub struct CPUInterval {22- /// Total amount of time spent executing in user mode23- pub user: Second,26+pub struct ExecuteResult {27+ /// The amount of user time the process used28+ pub user_time: Second,29 25- /// Total amount of time spent executing in kernel mode26- pub system: Second,30+ /// The amount of cpu time the process used31+ pub system_time: Second,32+33+ /// The exit status of the process34+ pub status: ExitStatus,35 }36+37+/// Execute the given command and return a timing summary38+#[cfg(not(windows))]39+pub fn execute_and_measure(mut command: Command, error_message: &str) -> Result<ExecuteResult> {40+ let cpu_timer = self::unix_timer::CPUTimer::start();41+ let status = command42+ .status()43+ .with_context(|| error_message.to_string())?;44+ let (user_time, system_time) = cpu_timer.stop();45+46+ Ok(ExecuteResult {47+ user_time,48+ system_time,49+ status,50+ })51+}52+53+/// Execute the given command and return a timing summary54+#[cfg(windows)]55+pub fn execute_and_measure(mut command: Command, error_message: &str) -> Result<ExecuteResult> {56+ let mut child = command.spawn().with_context(|| error_message.to_string())?;57+ let cpu_timer = self::windows_timer::CPUTimer::start_for_process(&child);58+ let status = child.wait()?;59+60+ let (user_time, system_time) = cpu_timer.stop();61+ Ok(ExecuteResult {62+ user_time,63+ system_time,64+ status,65+ })66+}modified:src/timer/unix_timer.rs+10 added-1 removed
1 #![cfg(not(windows))]2 3 use std::mem;4 5-use crate::timer::{CPUInterval, CPUTimes};5+use crate::timer::CPUTimes;6 use crate::util::units::Second;7 8+#[derive(Debug, Copy, Clone)]9+pub struct CPUInterval {10+ /// Total amount of time spent executing in user mode11+ pub user: Second,12+13+ /// Total amount of time spent executing in kernel mode14+ pub system: Second,15+}16+17 pub struct CPUTimer {18 start_cpu: CPUTimes,19 }20 71 unmodified linesadded:src/timer/wall_clock_timer.rs+20 added
1+use std::time::Instant;2+3+use crate::util::units::Second;4+5+pub struct WallClockTimer {6+ start: Instant,7+}8+9+impl WallClockTimer {10+ pub fn start() -> WallClockTimer {11+ WallClockTimer {12+ start: Instant::now(),13+ }14+ }15+16+ pub fn stop(&self) -> Second {17+ let duration = self.start.elapsed();18+ duration.as_secs() as f64 + f64::from(duration.subsec_nanos()) * 1e-919+ }20+}deleted:src/timer/wallclocktimer.rs-20 removed
1-use std::time::Instant;2-3-use crate::util::units::Second;4-5-pub struct WallClockTimer {6- start: Instant,7-}8-9-impl WallClockTimer {10- pub fn start() -> WallClockTimer {11- WallClockTimer {12- start: Instant::now(),13- }14- }15-16- pub fn stop(&self) -> Second {17- let duration = self.start.elapsed();18- duration.as_secs() as f64 + f64::from(duration.subsec_nanos()) * 1e-919- }20-}deleted:src/execute.rs-47 removed
1-use std::process::{Command, ExitStatus};2-3-use anyhow::{Context, Result};4-5-/// Used to indicate the result of running a command6-#[derive(Debug, Copy, Clone)]7-pub struct ExecuteResult {8- /// The amount of user time the process used9- pub user_time: f64,10-11- /// The amount of cpu time the process used12- pub system_time: f64,13-14- /// The exit status of the process15- pub status: ExitStatus,16-}17-18-/// Execute the given command and return a timing summary19-#[cfg(not(windows))]20-pub fn execute_and_measure(mut command: Command, error_message: &str) -> Result<ExecuteResult> {21- let cpu_timer = crate::timer::unix_timer::CPUTimer::start();22- let status = command23- .status()24- .with_context(|| error_message.to_string())?;25- let (user_time, system_time) = cpu_timer.stop();26-27- Ok(ExecuteResult {28- user_time,29- system_time,30- status,31- })32-}33-34-/// Execute the given command and return a timing summary35-#[cfg(windows)]36-pub fn execute_and_measure(mut command: Command, error_message: &str) -> Result<ExecuteResult> {37- let mut child = command.spawn().with_context(|| error_message.to_string())?;38- let cpu_timer = crate::timer::windows_timer::CPUTimer::start_for_process(&child);39- let status = child.wait()?;40-41- let (user_time, system_time) = cpu_timer.stop();42- Ok(ExecuteResult {43- user_time,44- system_time,45- status,46- })47-}modified:src/main.rs-1 removed
11 unmodified lines12 pub mod app;13 pub mod benchmark;14 pub mod command;15 pub mod error;16-pub mod execute;16 pub mod export;17 pub mod options;18 pub mod outlier_detection;19 pub mod output;32 unmodified lines