mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-12-01 20:30:53 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ebb3182a6e |
No files matched your search
@@ -54,14 +54,13 @@ impl Measurements {
|
|||||||
mean(self.wall_clock_times())
|
mean(self.wall_clock_times())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The standard deviation of all wall clock times. Not available if only one run has been performed
|
/// The standard deviation of wall clock times, if at least two runs were measured.
|
||||||
pub fn stddev(&self) -> Option<Time> {
|
pub fn stddev(&self) -> Option<Time> {
|
||||||
let times: Vec<_> = self.wall_clock_times().collect(); // TODO: Avoid collecting
|
if self.len() < 2 {
|
||||||
|
|
||||||
if times.len() < 2 {
|
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(standard_deviation(times))
|
let times: Vec<_> = self.wall_clock_times().collect();
|
||||||
|
Some(standard_deviation(×, self.time_wall_clock_mean()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+7
-28
@@ -6,7 +6,6 @@ pub mod scheduler;
|
|||||||
|
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
use std::time::Instant;
|
|
||||||
|
|
||||||
use crate::benchmark::executor::BenchmarkIteration;
|
use crate::benchmark::executor::BenchmarkIteration;
|
||||||
use crate::command::Command;
|
use crate::command::Command;
|
||||||
@@ -15,9 +14,7 @@ use crate::options::{
|
|||||||
};
|
};
|
||||||
use crate::outlier_detection::OUTLIER_THRESHOLD;
|
use crate::outlier_detection::OUTLIER_THRESHOLD;
|
||||||
use crate::output::console_writeln;
|
use crate::output::console_writeln;
|
||||||
use crate::output::progress_bar::{
|
use crate::output::progress_bar::get_progress_bar;
|
||||||
finish_initial_measurement, get_progress_bar, start_initial_measurement,
|
|
||||||
};
|
|
||||||
use crate::output::warnings::{OutlierWarningOptions, Warnings};
|
use crate::output::warnings::{OutlierWarningOptions, Warnings};
|
||||||
use crate::parameter::ParameterNameAndValue;
|
use crate::parameter::ParameterNameAndValue;
|
||||||
use crate::quantity::{const_time_from_seconds, ratio, FormatQuantity, Time, Zero};
|
use crate::quantity::{const_time_from_seconds, ratio, FormatQuantity, Time, Zero};
|
||||||
@@ -247,11 +244,7 @@ impl<'a> Benchmark<'a> {
|
|||||||
let progress_bar = if self.options.output_style != OutputStyleOption::Disabled {
|
let progress_bar = if self.options.output_style != OutputStyleOption::Disabled {
|
||||||
Some(get_progress_bar(
|
Some(get_progress_bar(
|
||||||
self.options.run_bounds.min,
|
self.options.run_bounds.min,
|
||||||
if preparation_command.is_some() {
|
"Initial time measurement",
|
||||||
"Running preparation command"
|
|
||||||
} else {
|
|
||||||
"Initial run"
|
|
||||||
},
|
|
||||||
self.options.output_style,
|
self.options.output_style,
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
@@ -265,9 +258,6 @@ impl<'a> Benchmark<'a> {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Initial timing run
|
// Initial timing run
|
||||||
if let Some(bar) = progress_bar.as_ref() {
|
|
||||||
start_initial_measurement(bar, Instant::now());
|
|
||||||
}
|
|
||||||
let res = self.executor.run_command_and_measure(
|
let res = self.executor.run_command_and_measure(
|
||||||
self.command,
|
self.command,
|
||||||
benchmark_iteration,
|
benchmark_iteration,
|
||||||
@@ -276,18 +266,6 @@ impl<'a> Benchmark<'a> {
|
|||||||
)?;
|
)?;
|
||||||
let success = res.exit_status.success();
|
let success = res.exit_status.success();
|
||||||
|
|
||||||
if let Some(bar) = progress_bar.as_ref() {
|
|
||||||
let time_unit = self
|
|
||||||
.options
|
|
||||||
.time_unit
|
|
||||||
.unwrap_or(res.time_wall_clock.suitable_unit());
|
|
||||||
let estimate = res.time_wall_clock.format(time_unit);
|
|
||||||
finish_initial_measurement(
|
|
||||||
bar,
|
|
||||||
format!("Current estimate: {}", estimate.to_string().green()),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
let conclusion_result = run_conclusion_command(benchmark_iteration)?;
|
let conclusion_result = run_conclusion_command(benchmark_iteration)?;
|
||||||
let conclusion_overhead = conclusion_result.map_or(Time::zero(), |res| {
|
let conclusion_overhead = conclusion_result.map_or(Time::zero(), |res| {
|
||||||
res.time_wall_clock + self.executor.time_overhead()
|
res.time_wall_clock + self.executor.time_overhead()
|
||||||
@@ -321,13 +299,16 @@ impl<'a> Benchmark<'a> {
|
|||||||
|
|
||||||
// Re-configure the progress bar
|
// Re-configure the progress bar
|
||||||
if let Some(bar) = progress_bar.as_ref() {
|
if let Some(bar) = progress_bar.as_ref() {
|
||||||
bar.set_length(count);
|
bar.set_length(count)
|
||||||
bar.inc(1);
|
}
|
||||||
|
if let Some(bar) = progress_bar.as_ref() {
|
||||||
|
bar.inc(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gather statistics (perform the actual benchmark)
|
// Gather statistics (perform the actual benchmark)
|
||||||
for i in 0..count_remaining {
|
for i in 0..count_remaining {
|
||||||
let benchmark_iteration = BenchmarkIteration::Benchmark(i + 1);
|
let benchmark_iteration = BenchmarkIteration::Benchmark(i + 1);
|
||||||
|
run_preparation_command(benchmark_iteration)?;
|
||||||
|
|
||||||
let msg = {
|
let msg = {
|
||||||
let t_wall_clock_mean = measurements.time_wall_clock_mean();
|
let t_wall_clock_mean = measurements.time_wall_clock_mean();
|
||||||
@@ -343,8 +324,6 @@ impl<'a> Benchmark<'a> {
|
|||||||
bar.set_message(msg.to_owned())
|
bar.set_message(msg.to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
run_preparation_command(benchmark_iteration)?;
|
|
||||||
|
|
||||||
let res = self.executor.run_command_and_measure(
|
let res = self.executor.run_command_and_measure(
|
||||||
self.command,
|
self.command,
|
||||||
benchmark_iteration,
|
benchmark_iteration,
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
use indicatif::{FormattedDuration, ProgressBar, ProgressState, ProgressStyle};
|
use indicatif::{ProgressBar, ProgressStyle};
|
||||||
use std::fmt;
|
use std::time::Duration;
|
||||||
use std::time::{Duration, Instant};
|
|
||||||
|
|
||||||
use crate::options::OutputStyleOption;
|
use crate::options::OutputStyleOption;
|
||||||
|
|
||||||
@@ -10,44 +9,13 @@ const TICK_SETTINGS: (&str, u64) = ("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏ ", 80);
|
|||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
const TICK_SETTINGS: (&str, u64) = (r"+-x| ", 200);
|
const TICK_SETTINGS: (&str, u64) = (r"+-x| ", 200);
|
||||||
|
|
||||||
// Keep the message area 30 columns wide across preparation, the initial run,
|
|
||||||
// and estimates so that switching between them does not move the bar.
|
|
||||||
const DEFAULT_TEMPLATE: &str = " {spinner} {msg:<30} {wide_bar} ETA {eta_precise} ";
|
|
||||||
const INITIAL_TEMPLATE: &str =
|
|
||||||
" {spinner} Initial run: {initial_elapsed:<17} {wide_bar} ETA {eta_precise} ";
|
|
||||||
|
|
||||||
/// Show the time elapsed since the initial benchmark command started.
|
|
||||||
pub fn start_initial_measurement(bar: &ProgressBar, started: Instant) {
|
|
||||||
let style = bar
|
|
||||||
.style()
|
|
||||||
.template(INITIAL_TEMPLATE)
|
|
||||||
.expect("no template error")
|
|
||||||
.with_key(
|
|
||||||
"initial_elapsed",
|
|
||||||
move |_: &ProgressState, w: &mut dyn fmt::Write| {
|
|
||||||
write!(w, "{}", FormattedDuration(started.elapsed())).unwrap();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
bar.set_style(style);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Stop showing the initial timer and display the next phase's message.
|
|
||||||
pub fn finish_initial_measurement(bar: &ProgressBar, message: String) {
|
|
||||||
bar.set_message(message);
|
|
||||||
bar.set_style(
|
|
||||||
bar.style()
|
|
||||||
.template(DEFAULT_TEMPLATE)
|
|
||||||
.expect("no template error"),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return a pre-configured progress bar
|
/// Return a pre-configured progress bar
|
||||||
pub fn get_progress_bar(length: u64, msg: &str, option: OutputStyleOption) -> ProgressBar {
|
pub fn get_progress_bar(length: u64, msg: &str, option: OutputStyleOption) -> ProgressBar {
|
||||||
let progressbar_style = match option {
|
let progressbar_style = match option {
|
||||||
OutputStyleOption::Basic | OutputStyleOption::Color => ProgressStyle::default_bar(),
|
OutputStyleOption::Basic | OutputStyleOption::Color => ProgressStyle::default_bar(),
|
||||||
_ => ProgressStyle::default_spinner()
|
_ => ProgressStyle::default_spinner()
|
||||||
.tick_chars(TICK_SETTINGS.0)
|
.tick_chars(TICK_SETTINGS.0)
|
||||||
.template(DEFAULT_TEMPLATE)
|
.template(" {spinner} {msg:<30} {wide_bar} ETA {eta_precise} ")
|
||||||
.expect("no template error"),
|
.expect("no template error"),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+25
-30
@@ -1,12 +1,8 @@
|
|||||||
use std::ops::Add;
|
//! Statistics for non-empty samples of finite measurements.
|
||||||
use std::ops::AddAssign;
|
|
||||||
use std::ops::Div;
|
|
||||||
|
|
||||||
use uom::num_traits;
|
use std::ops::{Add, AddAssign, Div};
|
||||||
use uom::si::f64::Ratio;
|
|
||||||
use uom::si::ratio::ratio;
|
|
||||||
|
|
||||||
use super::{byte, second, Information, Time};
|
use crate::quantity::{byte, ratio, second, Information, Ratio, Time, Zero};
|
||||||
|
|
||||||
/// A min function that assumes no NaNs and at least one element
|
/// A min function that assumes no NaNs and at least one element
|
||||||
pub fn min<Q: PartialOrd>(values: impl IntoIterator<Item = Q>) -> Q {
|
pub fn min<Q: PartialOrd>(values: impl IntoIterator<Item = Q>) -> Q {
|
||||||
@@ -24,14 +20,14 @@ pub fn max<Q: PartialOrd>(values: impl IntoIterator<Item = Q>) -> Q {
|
|||||||
.expect("'max' requires at least one element")
|
.expect("'max' requires at least one element")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A mean function that assumes at least one element
|
/// Arithmetic mean, summing measurements in their original order.
|
||||||
pub fn mean<Q, P>(values: impl IntoIterator<Item = Q>) -> Q
|
pub fn mean<Q, P>(values: impl IntoIterator<Item = Q>) -> Q
|
||||||
where
|
where
|
||||||
Q: AddAssign + num_traits::Zero + Div<Ratio, Output = P>,
|
Q: AddAssign + Zero + Div<Ratio, Output = P>,
|
||||||
P: Into<Q>,
|
P: Into<Q>,
|
||||||
{
|
{
|
||||||
let mut sum = Q::zero();
|
let mut sum = Q::zero();
|
||||||
let mut count = 0;
|
let mut count = 0usize;
|
||||||
for value in values {
|
for value in values {
|
||||||
sum += value;
|
sum += value;
|
||||||
count += 1;
|
count += 1;
|
||||||
@@ -41,6 +37,7 @@ where
|
|||||||
(sum / count).into()
|
(sum / count).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Median without modifying the input sample.
|
||||||
pub fn median<Q, P>(values: impl IntoIterator<Item = Q>) -> Q
|
pub fn median<Q, P>(values: impl IntoIterator<Item = Q>) -> Q
|
||||||
where
|
where
|
||||||
Q: Copy + PartialOrd + Add<Output = Q> + Div<Ratio, Output = P>,
|
Q: Copy + PartialOrd + Add<Output = Q> + Div<Ratio, Output = P>,
|
||||||
@@ -51,14 +48,14 @@ where
|
|||||||
!values.is_empty(),
|
!values.is_empty(),
|
||||||
"median requires at least one measurement"
|
"median requires at least one measurement"
|
||||||
);
|
);
|
||||||
values.sort_by(|a, b| a.partial_cmp(b).expect("No NaN values"));
|
values.sort_unstable_by(|a, b| a.partial_cmp(b).expect("No NaN measurements"));
|
||||||
|
|
||||||
let len = values.len();
|
let len = values.len();
|
||||||
if len % 2 == 0 {
|
if len % 2 == 0 {
|
||||||
let mid = len / 2;
|
let mid = len / 2;
|
||||||
let a = &values[mid - 1];
|
let a = &values[mid - 1];
|
||||||
let b = &values[mid];
|
let b = &values[mid];
|
||||||
((*a + *b) / Ratio::new::<ratio>(2.)).into()
|
((*b + *a) / Ratio::new::<ratio>(2.)).into()
|
||||||
} else {
|
} else {
|
||||||
values[len / 2]
|
values[len / 2]
|
||||||
}
|
}
|
||||||
@@ -91,24 +88,22 @@ impl UnsafeRawValue for Information {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn standard_deviation_f64(values: &[f64]) -> f64 {
|
/// Sample standard deviation, using an already computed mean.
|
||||||
let mean_value = mean(values.iter().copied());
|
pub fn standard_deviation<Q: UnsafeRawValue>(values: &[Q], mean: Q) -> Q {
|
||||||
|
let mean = mean.unsafe_raw_value();
|
||||||
|
assert!(
|
||||||
|
values.len() > 1,
|
||||||
|
"standard deviation requires at least two measurements"
|
||||||
|
);
|
||||||
|
let sum = values
|
||||||
|
.iter()
|
||||||
|
.map(|value| (value.unsafe_raw_value() - mean) * (value.unsafe_raw_value() - mean))
|
||||||
|
.fold(0.0, |sum, deviation| sum + deviation);
|
||||||
|
assert!(sum >= 0.0, "invalid sum of squared deviations");
|
||||||
|
|
||||||
let mut squared_deviations = 0.;
|
// Preserve the operation order of statistical 1.0: dividing by n - 1
|
||||||
let mut n = 0;
|
// before taking the square root also preserves exported floating-point values.
|
||||||
for value in values {
|
Q::unsafe_from_raw_value((sum / (values.len() as f64 - 1.0)).sqrt())
|
||||||
let deviation = value - mean_value;
|
|
||||||
squared_deviations += deviation * deviation;
|
|
||||||
n += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
(1. / ((n - 1) as f64) * squared_deviations).sqrt()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn standard_deviation<Q: UnsafeRawValue>(values: impl IntoIterator<Item = Q> + Clone) -> Q {
|
|
||||||
let values: Vec<_> = values.into_iter().map(|q| q.unsafe_raw_value()).collect();
|
|
||||||
let result = standard_deviation_f64(&values);
|
|
||||||
Q::unsafe_from_raw_value(result)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compute modified Z-scores for a given sample. A (unmodified) Z-score is defined by
|
/// Compute modified Z-scores for a given sample. A (unmodified) Z-score is defined by
|
||||||
@@ -224,7 +219,7 @@ mod tests {
|
|||||||
Time::new::<millisecond>(200.0),
|
Time::new::<millisecond>(200.0),
|
||||||
Time::new::<microsecond>(300_000.0),
|
Time::new::<microsecond>(300_000.0),
|
||||||
];
|
];
|
||||||
let result = standard_deviation(values);
|
let result = standard_deviation(&values, mean(values));
|
||||||
assert_relative_eq!(result.get::<millisecond>(), 100.0);
|
assert_relative_eq!(result.get::<millisecond>(), 100.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -34,11 +34,11 @@ use crate::quantity::{nanosecond, Information, Time, Zero};
|
|||||||
|
|
||||||
#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]
|
#[cfg(not(feature = "windows_process_extensions_main_thread_handle"))]
|
||||||
#[allow(non_upper_case_globals)]
|
#[allow(non_upper_case_globals)]
|
||||||
static NtResumeProcess: Lazy<unsafe extern "system" fn(ProcessHandle: HANDLE) -> NTSTATUS> =
|
static NtResumeProcess: Lazy<unsafe extern "system" fn(process_handle: HANDLE) -> NTSTATUS> =
|
||||||
Lazy::new(|| {
|
Lazy::new(|| {
|
||||||
// SAFETY: Getting the module handle for ntdll.dll is safe
|
// SAFETY: Getting the module handle for ntdll.dll is safe
|
||||||
let ntdll = unsafe { GetModuleHandleW(w!("ntdll.dll")) };
|
let ntdll = unsafe { GetModuleHandleW(w!("ntdll.dll")) };
|
||||||
assert!(ntdll != std::ptr::null_mut(), "GetModuleHandleW failed");
|
assert!(!ntdll.is_null(), "GetModuleHandleW failed");
|
||||||
|
|
||||||
// SAFETY: The ntdll handle is valid
|
// SAFETY: The ntdll handle is valid
|
||||||
let nt_resume_process = unsafe { GetProcAddress(ntdll, s!("NtResumeProcess")) };
|
let nt_resume_process = unsafe { GetProcAddress(ntdll, s!("NtResumeProcess")) };
|
||||||
@@ -57,10 +57,7 @@ impl CPUTimer {
|
|||||||
|
|
||||||
// SAFETY: Creating a new job object is safe
|
// SAFETY: Creating a new job object is safe
|
||||||
let job_object = unsafe { CreateJobObjectW(ptr::null_mut(), ptr::null_mut()) };
|
let job_object = unsafe { CreateJobObjectW(ptr::null_mut(), ptr::null_mut()) };
|
||||||
assert!(
|
assert!(!job_object.is_null(), "CreateJobObjectW failed");
|
||||||
job_object != std::ptr::null_mut(),
|
|
||||||
"CreateJobObjectW failed"
|
|
||||||
);
|
|
||||||
|
|
||||||
// SAFETY: The job object handle is valid
|
// SAFETY: The job object handle is valid
|
||||||
let ret = unsafe { AssignProcessToJobObject(job_object, child_handle) };
|
let ret = unsafe { AssignProcessToJobObject(job_object, child_handle) };
|
||||||
|
|||||||
Reference in new issue
Block a user