mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-10-28 08:08:58 +00:00
Refactoring
This commit is contained in:
1 parent
be66a6668f
commit
71365c227e
6 files changed
+34
-64
No files matched your search
+15
-27
@@ -78,10 +78,11 @@ fn run_command_and_measure_common(
|
||||
command.env("HYPERFINE_ITERATION", value);
|
||||
}
|
||||
|
||||
let result = execute_and_measure(command)
|
||||
let measurement = execute_and_measure(command)
|
||||
.with_context(|| format!("Failed to run command '{command_name}'"))?;
|
||||
|
||||
if command_failure_action == CmdFailureAction::RaiseError && !result.exit_status.success() {
|
||||
if command_failure_action == CmdFailureAction::RaiseError && !measurement.exit_status.success()
|
||||
{
|
||||
let when = match iteration {
|
||||
BenchmarkIteration::NonBenchmarkRun => "a non-benchmark run".to_string(),
|
||||
BenchmarkIteration::Warmup(0) => "the first warmup run".to_string(),
|
||||
@@ -92,7 +93,7 @@ fn run_command_and_measure_common(
|
||||
bail!(
|
||||
"{cause} in {when}. Use the '-i'/'--ignore-failure' option if you want to ignore this. \
|
||||
Alternatively, use the '--show-output' option to debug what went wrong.",
|
||||
cause=result.exit_status.code().map_or(
|
||||
cause=measurement.exit_status.code().map_or(
|
||||
"The process has been terminated by a signal".into(),
|
||||
|c| format!("Command terminated with non-zero exit code {c}")
|
||||
|
||||
@@ -130,7 +131,7 @@ fn run_command_and_measure_common(
|
||||
}
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
Ok(measurement)
|
||||
}
|
||||
|
||||
pub struct RawExecutor<'a> {
|
||||
@@ -151,22 +152,14 @@ impl Executor for RawExecutor<'_> {
|
||||
command_failure_action: Option<CmdFailureAction>,
|
||||
output_policy: &CommandOutputPolicy,
|
||||
) -> Result<Measurement> {
|
||||
let result = run_command_and_measure_common(
|
||||
run_command_and_measure_common(
|
||||
command.get_command()?,
|
||||
iteration,
|
||||
command_failure_action.unwrap_or_else(|| self.options.command_failure_action.clone()),
|
||||
&self.options.command_input_policy,
|
||||
output_policy,
|
||||
&command.get_command_line(),
|
||||
)?;
|
||||
|
||||
Ok(Measurement {
|
||||
time_wall_clock: result.time_wall_clock,
|
||||
time_user: result.time_user,
|
||||
time_system: result.time_system,
|
||||
peak_memory_usage: result.peak_memory_usage,
|
||||
exit_status: result.exit_status,
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
fn calibrate(&mut self) -> Result<()> {
|
||||
@@ -214,7 +207,7 @@ impl Executor for ShellExecutor<'_> {
|
||||
command_builder.arg(command.get_command_line());
|
||||
}
|
||||
|
||||
let mut result = run_command_and_measure_common(
|
||||
let mut measurement = run_command_and_measure_common(
|
||||
command_builder,
|
||||
iteration,
|
||||
command_failure_action.unwrap_or_else(|| self.options.command_failure_action.clone()),
|
||||
@@ -233,20 +226,15 @@ impl Executor for ShellExecutor<'_> {
|
||||
}
|
||||
|
||||
if let Some(ref spawning_time) = self.shell_spawning_time {
|
||||
result.time_wall_clock =
|
||||
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);
|
||||
measurement.time_wall_clock =
|
||||
ensure_non_negative(measurement.time_wall_clock - spawning_time.time_wall_clock);
|
||||
measurement.time_user =
|
||||
ensure_non_negative(measurement.time_user - spawning_time.time_user);
|
||||
measurement.time_system =
|
||||
ensure_non_negative(measurement.time_system - spawning_time.time_system);
|
||||
}
|
||||
|
||||
Ok(Measurement {
|
||||
time_wall_clock: result.time_wall_clock,
|
||||
time_user: result.time_user,
|
||||
time_system: result.time_system,
|
||||
peak_memory_usage: result.peak_memory_usage,
|
||||
exit_status: result.exit_status,
|
||||
})
|
||||
Ok(measurement)
|
||||
}
|
||||
|
||||
/// Measure the average shell spawning time
|
||||
|
||||
+9
-22
@@ -269,20 +269,20 @@ impl<'a> Benchmark<'a> {
|
||||
if let Some(bar) = progress_bar.as_ref() {
|
||||
start_initial_measurement(bar, Instant::now());
|
||||
}
|
||||
let result = self.executor.run_command_and_measure(
|
||||
let measurement = self.executor.run_command_and_measure(
|
||||
self.command,
|
||||
benchmark_iteration,
|
||||
None,
|
||||
output_policy,
|
||||
)?;
|
||||
let success = result.exit_status.success();
|
||||
let success = measurement.exit_status.success();
|
||||
|
||||
if let Some(bar) = progress_bar.as_ref() {
|
||||
let time_unit = self
|
||||
.options
|
||||
.time_unit
|
||||
.unwrap_or(result.time_wall_clock.suitable_unit());
|
||||
let estimate = result.time_wall_clock.format(time_unit);
|
||||
.unwrap_or(measurement.time_wall_clock.suitable_unit());
|
||||
let estimate = measurement.time_wall_clock.format(time_unit);
|
||||
finish_initial_measurement(
|
||||
bar,
|
||||
format!("Current estimate: {}", estimate.to_string().green()),
|
||||
@@ -296,7 +296,7 @@ impl<'a> Benchmark<'a> {
|
||||
|
||||
// Determine number of benchmark runs
|
||||
let runs_in_min_time = (self.options.min_benchmarking_time
|
||||
/ (result.time_wall_clock
|
||||
/ (measurement.time_wall_clock
|
||||
+ self.executor.time_overhead()
|
||||
+ preparation_overhead
|
||||
+ conclusion_overhead))
|
||||
@@ -316,13 +316,7 @@ impl<'a> Benchmark<'a> {
|
||||
let count_remaining = count - 1;
|
||||
|
||||
// Save the first result
|
||||
measurements.push(Measurement {
|
||||
time_wall_clock: result.time_wall_clock,
|
||||
time_user: result.time_user,
|
||||
time_system: result.time_system,
|
||||
peak_memory_usage: result.peak_memory_usage,
|
||||
exit_status: result.exit_status,
|
||||
});
|
||||
measurements.push(measurement);
|
||||
|
||||
all_succeeded = all_succeeded && success;
|
||||
|
||||
@@ -348,21 +342,14 @@ impl<'a> Benchmark<'a> {
|
||||
|
||||
run_preparation_command(benchmark_iteration)?;
|
||||
|
||||
let result = self.executor.run_command_and_measure(
|
||||
let measurement = self.executor.run_command_and_measure(
|
||||
self.command,
|
||||
benchmark_iteration,
|
||||
None,
|
||||
output_policy,
|
||||
)?;
|
||||
let success = result.exit_status.success();
|
||||
|
||||
measurements.push(Measurement {
|
||||
time_wall_clock: result.time_wall_clock,
|
||||
time_user: result.time_user,
|
||||
time_system: result.time_system,
|
||||
peak_memory_usage: result.peak_memory_usage,
|
||||
exit_status: result.exit_status,
|
||||
});
|
||||
let success = measurement.exit_status.success();
|
||||
measurements.push(measurement);
|
||||
|
||||
all_succeeded = all_succeeded && success;
|
||||
|
||||
|
||||
@@ -151,12 +151,9 @@ pub fn compute<'a>(
|
||||
|
||||
#[cfg(test)]
|
||||
fn create_result(name: &str, mean: f64) -> BenchmarkResult {
|
||||
use std::{collections::BTreeMap, process::ExitStatus};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use crate::benchmark::{
|
||||
measurement::{Measurement, Measurements},
|
||||
quantity::{Information, InformationQuantity},
|
||||
};
|
||||
use crate::benchmark::measurement::{Measurement, Measurements};
|
||||
|
||||
BenchmarkResult {
|
||||
command: name.into(),
|
||||
@@ -164,9 +161,7 @@ fn create_result(name: &str, mean: f64) -> BenchmarkResult {
|
||||
measurements: vec![Measurement {
|
||||
time_wall_clock: Time::from_seconds(mean),
|
||||
time_user: Time::from_seconds(mean),
|
||||
time_system: Time::zero(),
|
||||
peak_memory_usage: Information::from_bytes(1024),
|
||||
exit_status: ExitStatus::default(),
|
||||
..Default::default()
|
||||
}],
|
||||
},
|
||||
parameters: BTreeMap::new(),
|
||||
|
||||
+2
-2
@@ -82,14 +82,14 @@ pub fn execute_and_measure(mut command: Command) -> Result<Measurement> {
|
||||
discard(output);
|
||||
}
|
||||
|
||||
let (exit_status, time_user, time_system, memory_usage_byte) = cpu_timer.stop(child)?;
|
||||
let (time_user, time_system, peak_memory_usage, exit_status) = cpu_timer.stop(child)?;
|
||||
let time_wall_clock = wallclock_timer.stop();
|
||||
|
||||
Ok(Measurement {
|
||||
time_wall_clock,
|
||||
time_user,
|
||||
time_system,
|
||||
peak_memory_usage: memory_usage_byte,
|
||||
peak_memory_usage,
|
||||
exit_status,
|
||||
})
|
||||
}
|
||||
@@ -70,13 +70,13 @@ impl CPUTimer {
|
||||
Self {}
|
||||
}
|
||||
|
||||
pub fn stop(&self, child: Child) -> Result<(ExitStatus, Time, Time, Information)> {
|
||||
pub fn stop(&self, child: Child) -> Result<(Time, Time, Information, ExitStatus)> {
|
||||
let (status, usage) = wait4(child)?;
|
||||
Ok((
|
||||
status,
|
||||
usage.time_user,
|
||||
usage.time_system,
|
||||
usage.memory_usage,
|
||||
status,
|
||||
))
|
||||
}
|
||||
}
|
||||
@@ -84,7 +84,7 @@ impl CPUTimer {
|
||||
Self { job_object }
|
||||
}
|
||||
|
||||
pub fn stop(&self, mut child: Child) -> Result<(ExitStatus, Time, Time, Information)> {
|
||||
pub fn stop(&self, mut child: Child) -> Result<(Time, Time, Information, ExitStatus)> {
|
||||
let status = child.wait()?;
|
||||
|
||||
let mut job_object_info =
|
||||
@@ -116,9 +116,9 @@ impl CPUTimer {
|
||||
let system_time =
|
||||
Time::from_nanoseconds((job_object_info.TotalKernelTime as f64) * 100.0);
|
||||
|
||||
Ok((status, user_time, system_time, Information::zero()))
|
||||
Ok((user_time, system_time, Information::zero(), status))
|
||||
} else {
|
||||
Ok((status, Time::zero(), Time::zero(), Information::zero()))
|
||||
Ok((Time::zero(), Time::zero(), Information::zero(), status))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user