Move wall clock timer into execute_and_measure

This commit is contained in:
David Peter 2022-02-22 12:56:04 +01:00 committed by David Peter
parent e001530f61
commit 55f0ef6637
2 changed files with 32 additions and 32 deletions

View File

@ -3,7 +3,7 @@ use std::process::{ExitStatus, Stdio};
use crate::command::Command;
use crate::options::{CmdFailureAction, Options, OutputStyleOption, Shell};
use crate::output::progress_bar::get_progress_bar;
use crate::timer::{execute_and_measure, wall_clock_timer::WallClockTimer};
use crate::timer::execute_and_measure;
use crate::util::randomized_environment_offset;
use crate::util::units::Second;
@ -77,13 +77,13 @@ impl<'a> Executor for ShellExecutor<'a> {
.arg(if cfg!(windows) { "/C" } else { "-c" })
.arg(command.get_command_line());
let wallclock_timer = WallClockTimer::start();
let result = execute_and_measure(command_builder)
.with_context(|| format!("Failed to run command '{}'", command.get_command_line()))?;
let mut time_real = wallclock_timer.stop();
let mut time_user = result.user_time;
let mut time_system = result.system_time;
let mut time_real = result.time_real;
let mut time_user = result.time_user;
let mut time_system = result.time_system;
if command_failure_action.unwrap_or(self.options.command_failure_action)
== CmdFailureAction::RaiseError

View File

@ -1,4 +1,4 @@
pub mod wall_clock_timer;
mod wall_clock_timer;
#[cfg(windows)]
mod windows_timer;
@ -7,6 +7,7 @@ mod windows_timer;
mod unix_timer;
use crate::util::units::Second;
use wall_clock_timer::WallClockTimer;
use std::process::{Command, ExitStatus};
@ -24,41 +25,40 @@ struct CPUTimes {
/// Used to indicate the result of running a command
#[derive(Debug, Copy, Clone)]
pub struct ExecuteResult {
/// The amount of user time the process used
pub user_time: Second,
/// The amount of cpu time the process used
pub system_time: Second,
pub time_real: Second,
pub time_user: Second,
pub time_system: Second,
/// The exit status of the process
pub status: ExitStatus,
}
/// Execute the given command and return a timing summary
#[cfg(not(windows))]
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()?;
let (user_time, system_time) = cpu_timer.stop();
(cpu_timer.stop(), status)
};
Ok(ExecuteResult {
user_time,
system_time,
status,
})
}
/// Execute the given command and return a timing summary
#[cfg(windows)]
pub fn execute_and_measure(mut command: Command) -> Result<ExecuteResult> {
#[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()?;
let (user_time, system_time) = cpu_timer.stop();
(cpu_timer.stop(), status)
};
let time_real = wallclock_timer.stop();
Ok(ExecuteResult {
user_time,
system_time,
time_real,
time_user,
time_system,
status,
})
}