mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-10-12 02:22:35 +00:00
Further simplifications
This commit is contained in:
@@ -15,6 +15,7 @@ use super::measurement::{Measurement, Measurements};
|
||||
|
||||
use anyhow::{bail, Context, Result};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum BenchmarkIteration {
|
||||
NonBenchmarkRun,
|
||||
Warmup(u64),
|
||||
@@ -36,7 +37,7 @@ pub trait Executor {
|
||||
fn run_command_and_measure(
|
||||
&self,
|
||||
command: &Command<'_>,
|
||||
iteration: &BenchmarkIteration,
|
||||
iteration: BenchmarkIteration,
|
||||
command_failure_action: Option<CmdFailureAction>,
|
||||
output_policy: &CommandOutputPolicy,
|
||||
) -> Result<Measurement>;
|
||||
@@ -56,7 +57,7 @@ pub trait Executor {
|
||||
|
||||
fn run_command_and_measure_common(
|
||||
mut command: std::process::Command,
|
||||
iteration: &BenchmarkIteration,
|
||||
iteration: BenchmarkIteration,
|
||||
command_failure_action: CmdFailureAction,
|
||||
command_input_policy: &CommandInputPolicy,
|
||||
command_output_policy: &CommandOutputPolicy,
|
||||
@@ -132,7 +133,7 @@ impl Executor for RawExecutor<'_> {
|
||||
fn run_command_and_measure(
|
||||
&self,
|
||||
command: &Command<'_>,
|
||||
iteration: &BenchmarkIteration,
|
||||
iteration: BenchmarkIteration,
|
||||
command_failure_action: Option<CmdFailureAction>,
|
||||
output_policy: &CommandOutputPolicy,
|
||||
) -> Result<Measurement> {
|
||||
@@ -175,7 +176,7 @@ impl Executor for ShellExecutor<'_> {
|
||||
fn run_command_and_measure(
|
||||
&self,
|
||||
command: &Command<'_>,
|
||||
iteration: &BenchmarkIteration,
|
||||
iteration: BenchmarkIteration,
|
||||
command_failure_action: Option<CmdFailureAction>,
|
||||
output_policy: &CommandOutputPolicy,
|
||||
) -> Result<Measurement> {
|
||||
@@ -230,7 +231,7 @@ impl Executor for ShellExecutor<'_> {
|
||||
// Just run the shell without any command
|
||||
let res = self.run_command_and_measure(
|
||||
&Command::new(None, ""),
|
||||
&BenchmarkIteration::NonBenchmarkRun,
|
||||
BenchmarkIteration::NonBenchmarkRun,
|
||||
None,
|
||||
&CommandOutputPolicy::Null,
|
||||
);
|
||||
@@ -304,7 +305,7 @@ impl Executor for MockExecutor {
|
||||
fn run_command_and_measure(
|
||||
&self,
|
||||
command: &Command<'_>,
|
||||
_iteration: &BenchmarkIteration,
|
||||
_iteration: BenchmarkIteration,
|
||||
_command_failure_action: Option<CmdFailureAction>,
|
||||
_output_policy: &CommandOutputPolicy,
|
||||
) -> Result<Measurement> {
|
||||
|
||||
+28
-24
@@ -58,7 +58,7 @@ impl<'a> Benchmark<'a> {
|
||||
command: &Command<'_>,
|
||||
error_output: &'static str,
|
||||
output_policy: &CommandOutputPolicy,
|
||||
iteration: &executor::BenchmarkIteration,
|
||||
iteration: executor::BenchmarkIteration,
|
||||
) -> Result<Measurement> {
|
||||
self.executor
|
||||
.run_command_and_measure(
|
||||
@@ -75,7 +75,6 @@ impl<'a> Benchmark<'a> {
|
||||
&self,
|
||||
parameters: impl IntoIterator<Item = ParameterNameAndValue<'a>>,
|
||||
output_policy: &CommandOutputPolicy,
|
||||
iteration: executor::BenchmarkIteration,
|
||||
) -> Result<Measurement> {
|
||||
let command = self
|
||||
.options
|
||||
@@ -87,7 +86,14 @@ impl<'a> Benchmark<'a> {
|
||||
Append ' || true' to the command if you are sure that this can be ignored.";
|
||||
|
||||
Ok(command
|
||||
.map(|cmd| self.run_intermediate_command(&cmd, error_output, output_policy, &iteration))
|
||||
.map(|cmd| {
|
||||
self.run_intermediate_command(
|
||||
&cmd,
|
||||
error_output,
|
||||
output_policy,
|
||||
BenchmarkIteration::NonBenchmarkRun,
|
||||
)
|
||||
})
|
||||
.transpose()?
|
||||
.unwrap_or_default())
|
||||
}
|
||||
@@ -97,7 +103,6 @@ impl<'a> Benchmark<'a> {
|
||||
&self,
|
||||
parameters: impl IntoIterator<Item = ParameterNameAndValue<'a>>,
|
||||
output_policy: &CommandOutputPolicy,
|
||||
iteration: executor::BenchmarkIteration,
|
||||
) -> Result<Measurement> {
|
||||
let command = self
|
||||
.options
|
||||
@@ -109,7 +114,14 @@ impl<'a> Benchmark<'a> {
|
||||
Append ' || true' to the command if you are sure that this can be ignored.";
|
||||
|
||||
Ok(command
|
||||
.map(|cmd| self.run_intermediate_command(&cmd, error_output, output_policy, &iteration))
|
||||
.map(|cmd| {
|
||||
self.run_intermediate_command(
|
||||
&cmd,
|
||||
error_output,
|
||||
output_policy,
|
||||
BenchmarkIteration::NonBenchmarkRun,
|
||||
)
|
||||
})
|
||||
.transpose()?
|
||||
.unwrap_or_default())
|
||||
}
|
||||
@@ -119,7 +131,7 @@ impl<'a> Benchmark<'a> {
|
||||
&self,
|
||||
command: &Command<'_>,
|
||||
output_policy: &CommandOutputPolicy,
|
||||
iteration: &executor::BenchmarkIteration,
|
||||
iteration: executor::BenchmarkIteration,
|
||||
) -> Result<Measurement> {
|
||||
let error_output = "The preparation command terminated with a non-zero exit code. \
|
||||
Append ' || true' to the command if you are sure that this can be ignored.";
|
||||
@@ -137,7 +149,7 @@ impl<'a> Benchmark<'a> {
|
||||
let error_output = "The conclusion command terminated with a non-zero exit code. \
|
||||
Append ' || true' to the command if you are sure that this can be ignored.";
|
||||
|
||||
self.run_intermediate_command(command, error_output, output_policy, &iteration)
|
||||
self.run_intermediate_command(command, error_output, output_policy, iteration)
|
||||
}
|
||||
|
||||
/// Run the benchmark for a single command
|
||||
@@ -170,7 +182,7 @@ impl<'a> Benchmark<'a> {
|
||||
)
|
||||
});
|
||||
|
||||
let run_preparation_command = |iteration: &executor::BenchmarkIteration| {
|
||||
let run_preparation_command = |iteration: executor::BenchmarkIteration| {
|
||||
preparation_command
|
||||
.as_ref()
|
||||
.map(|cmd| self.run_preparation_command(cmd, output_policy, iteration))
|
||||
@@ -196,11 +208,7 @@ impl<'a> Benchmark<'a> {
|
||||
.transpose()
|
||||
};
|
||||
|
||||
self.run_setup_command(
|
||||
self.command.get_parameters().iter().cloned(),
|
||||
output_policy,
|
||||
executor::BenchmarkIteration::NonBenchmarkRun,
|
||||
)?;
|
||||
self.run_setup_command(self.command.get_parameters().iter().cloned(), output_policy)?;
|
||||
|
||||
// Warmup phase
|
||||
if self.options.warmup_count > 0 {
|
||||
@@ -216,10 +224,10 @@ impl<'a> Benchmark<'a> {
|
||||
|
||||
for i in 0..self.options.warmup_count {
|
||||
let warmup_iteration = BenchmarkIteration::Warmup(i);
|
||||
let _ = run_preparation_command(&warmup_iteration)?;
|
||||
let _ = run_preparation_command(warmup_iteration)?;
|
||||
let _ = self.executor.run_command_and_measure(
|
||||
self.command,
|
||||
&warmup_iteration,
|
||||
warmup_iteration,
|
||||
None,
|
||||
output_policy,
|
||||
)?;
|
||||
@@ -245,7 +253,7 @@ impl<'a> Benchmark<'a> {
|
||||
};
|
||||
|
||||
let benchmark_iteration = BenchmarkIteration::Benchmark(0);
|
||||
let preparation_result = run_preparation_command(&benchmark_iteration)?;
|
||||
let preparation_result = run_preparation_command(benchmark_iteration)?;
|
||||
let preparation_overhead = preparation_result.map_or(Time::zero(), |res| {
|
||||
res.time_wall_clock + self.executor.time_overhead()
|
||||
});
|
||||
@@ -253,7 +261,7 @@ impl<'a> Benchmark<'a> {
|
||||
// Initial timing run
|
||||
let res = self.executor.run_command_and_measure(
|
||||
self.command,
|
||||
&benchmark_iteration,
|
||||
benchmark_iteration,
|
||||
None,
|
||||
output_policy,
|
||||
)?;
|
||||
@@ -301,7 +309,7 @@ impl<'a> Benchmark<'a> {
|
||||
// Gather statistics (perform the actual benchmark)
|
||||
for i in 0..count_remaining {
|
||||
let benchmark_iteration = BenchmarkIteration::Benchmark(i + 1);
|
||||
run_preparation_command(&benchmark_iteration)?;
|
||||
run_preparation_command(benchmark_iteration)?;
|
||||
|
||||
let msg = {
|
||||
let t_wall_clock_mean = measurements.time_wall_clock_mean();
|
||||
@@ -319,7 +327,7 @@ impl<'a> Benchmark<'a> {
|
||||
|
||||
let res = self.executor.run_command_and_measure(
|
||||
self.command,
|
||||
&benchmark_iteration,
|
||||
benchmark_iteration,
|
||||
None,
|
||||
output_policy,
|
||||
)?;
|
||||
@@ -452,11 +460,7 @@ impl<'a> Benchmark<'a> {
|
||||
console_writeln!(io::stdout(), " ")?;
|
||||
}
|
||||
|
||||
self.run_cleanup_command(
|
||||
self.command.get_parameters().iter().cloned(),
|
||||
output_policy,
|
||||
executor::BenchmarkIteration::NonBenchmarkRun,
|
||||
)?;
|
||||
self.run_cleanup_command(self.command.get_parameters().iter().cloned(), output_policy)?;
|
||||
|
||||
Ok(BenchmarkResult {
|
||||
command: self.command.get_name(),
|
||||
|
||||
Reference in New Issue
Block a user