Switch to unit-safe quantities

This commit is contained in:
David Peter authored and David Peter committed 2026-10-05 15:58:56 +02:00
1 parent 914d5cbc59
commit f9aa299c0f
15 files changed
+399 -287

No files matched your search

+25 -16
View File
@@ -2,6 +2,9 @@
use std::os::windows::process::CommandExt;
use std::process::ExitStatus;
use crate::benchmark::quantity::mean;
use crate::benchmark::quantity::Byte;
use crate::benchmark::quantity::Second;
use crate::command::Command;
use crate::options::{
CmdFailureAction, CommandInputPolicy, CommandOutputPolicy, Options, OutputStyleOption, Shell,
@@ -161,8 +164,8 @@ impl Executor for RawExecutor<'_> {
Ok(())
}
fn time_overhead(&self) -> Time {
Time::zero()
fn time_overhead(&self) -> Second {
Second::zero()
}
}
@@ -212,11 +215,20 @@ impl Executor for ShellExecutor<'_> {
)?;
// Subtract shell spawning time
fn ensure_non_negative(time: Second) -> Second {
if time < Second::zero() {
Second::zero()
} else {
time
}
}
if let Some(spawning_time) = self.shell_spawning_time {
result.time_wall_clock =
(result.time_wall_clock - spawning_time.time_wall_clock).max(0.0);
result.time_user = (result.time_user - spawning_time.time_user).max(0.0);
result.time_system = (result.time_system - spawning_time.time_system).max(0.0);
ensure_non_negative(result.time_wall_clock - spawning_time.time_wall_clock);
result.time_user = ensure_non_negative(result.time_user - spawning_time.time_user);
result.time_system =
ensure_non_negative(result.time_system - spawning_time.time_system);
}
Ok((
@@ -243,7 +255,7 @@ impl Executor for ShellExecutor<'_> {
None
};
let mut times_wall_clock: Vec<Second> = vec![];
let mut times_wall_clock: Vec<Second> = vec![]; // TODO
let mut times_user: Vec<Second> = vec![];
let mut times_system: Vec<Second> = vec![];
@@ -289,7 +301,7 @@ impl Executor for ShellExecutor<'_> {
time_wall_clock: mean(&times_wall_clock),
time_user: mean(&times_user),
time_system: mean(&times_system),
memory_usage_byte: 0,
memory_usage_byte: Byte::new(0),
});
Ok(())
@@ -312,7 +324,7 @@ impl MockExecutor {
fn extract_time<S: AsRef<str>>(sleep_command: S) -> Time {
assert!(sleep_command.as_ref().starts_with("sleep "));
Time::new::<second>(
Second::new(
sleep_command
.as_ref()
.trim_start_matches("sleep ")
@@ -345,9 +357,9 @@ impl Executor for MockExecutor {
Ok((
TimingResult {
time_wall_clock: Self::extract_time(command.get_command_line()),
time_user: 0.0,
time_system: 0.0,
memory_usage_byte: 0,
time_user: Second::zero(),
time_system: Second::zero(),
memory_usage_byte: Byte::new(0),
},
status,
))
@@ -359,7 +371,7 @@ impl Executor for MockExecutor {
fn time_overhead(&self) -> Time {
match &self.shell {
None => Time::zero(),
None => Second::zero(),
Some(shell) => Self::extract_time(shell),
}
}
@@ -367,8 +379,5 @@ impl Executor for MockExecutor {
#[test]
fn test_mock_executor_extract_time() {
assert_eq!(
MockExecutor::extract_time("sleep 0.1"),
Time::new::<second>(0.1)
);
assert_eq!(MockExecutor::extract_time("sleep 0.1"), Second::new(0.1));
}