Files
hyperfine/src/timer/mod.rs
T

64 lines
1.5 KiB
Rust
Raw Normal View History

mod wall_clock_timer;
2018-02-05 18:22:59 -05:00
2021-08-24 21:36:37 +02:00
#[cfg(windows)]
2022-02-22 11:34:30 +01:00
mod windows_timer;
2019-05-07 11:55:38 +02:00
2021-08-24 21:36:37 +02:00
#[cfg(not(windows))]
2022-02-22 11:34:30 +01:00
mod unix_timer;
use crate::util::units::Second;
use wall_clock_timer::WallClockTimer;
2022-02-22 11:34:30 +01:00
use std::process::{Command, ExitStatus};
2022-02-22 11:45:46 +01:00
use anyhow::Result;
2021-08-24 21:48:10 +02:00
#[derive(Debug, Copy, Clone)]
2022-02-22 11:34:30 +01:00
struct CPUTimes {
2021-08-24 21:48:10 +02:00
/// Total amount of time spent executing in user mode
pub user_usec: i64,
/// Total amount of time spent executing in kernel mode
pub system_usec: i64,
}
2022-02-22 11:34:30 +01:00
/// Used to indicate the result of running a command
2021-08-24 21:48:10 +02:00
#[derive(Debug, Copy, Clone)]
2022-02-22 11:34:30 +01:00
pub struct ExecuteResult {
pub time_real: Second,
pub time_user: Second,
pub time_system: Second,
2022-02-22 11:34:30 +01:00
/// The exit status of the process
pub status: ExitStatus,
}
/// Execute the given command and return a timing summary
2022-02-22 11:45:46 +01:00
pub fn execute_and_measure(mut command: Command) -> Result<ExecuteResult> {
let wallclock_timer = WallClockTimer::start();
#[cfg(not(windows))]
let ((time_user, time_system), status) = {
let cpu_timer = self::unix_timer::CPUTimer::start();
let status = command.status()?;
(cpu_timer.stop(), status)
};
#[cfg(windows)]
let ((time_user, time_system), status) = {
let mut child = command.spawn()?;
let cpu_timer = self::windows_timer::CPUTimer::start_for_process(&child);
let status = child.wait()?;
(cpu_timer.stop(), status)
};
let time_real = wallclock_timer.stop();
2022-02-22 11:34:30 +01:00
Ok(ExecuteResult {
time_real,
time_user,
time_system,
2022-02-22 11:34:30 +01:00
status,
})
2021-08-24 21:48:10 +02:00
}