mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-11-26 09:59:57 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b4fb20f85b |
No files matched your search
+5
-3
@@ -19,6 +19,8 @@ impl Exporter for CsvExporter {
|
|||||||
_unit: Option<TimeUnit>,
|
_unit: Option<TimeUnit>,
|
||||||
_sort_order: SortOrder,
|
_sort_order: SortOrder,
|
||||||
) -> Result<Vec<u8>> {
|
) -> Result<Vec<u8>> {
|
||||||
|
const CSV_PRECISION: usize = 6;
|
||||||
|
|
||||||
let mut writer = WriterBuilder::new().from_writer(vec![]);
|
let mut writer = WriterBuilder::new().from_writer(vec![]);
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -48,7 +50,7 @@ impl Exporter for CsvExporter {
|
|||||||
res.min,
|
res.min,
|
||||||
res.max,
|
res.max,
|
||||||
] {
|
] {
|
||||||
fields.push(Cow::Owned(f.to_string().into_bytes()))
|
fields.push(Cow::Owned(format!("{f:.CSV_PRECISION$}").into_bytes()))
|
||||||
}
|
}
|
||||||
for v in res.parameters.values() {
|
for v in res.parameters.values() {
|
||||||
fields.push(Cow::Borrowed(v.as_bytes()))
|
fields.push(Cow::Borrowed(v.as_bytes()))
|
||||||
@@ -117,7 +119,7 @@ fn test_csv() {
|
|||||||
|
|
||||||
insta::assert_snapshot!(actual, @r#"
|
insta::assert_snapshot!(actual, @r#"
|
||||||
command,mean,stddev,median,user,system,min,max,parameter_bar,parameter_foo
|
command,mean,stddev,median,user,system,min,max,parameter_bar,parameter_foo
|
||||||
command_a,1,2,1,3,4,5,6,two,one
|
command_a,1.000000,2.000000,1.000000,3.000000,4.000000,5.000000,6.000000,two,one
|
||||||
command_b,11,12,11,13,14,15,16.5,seven,one
|
command_b,11.000000,12.000000,11.000000,13.000000,14.000000,15.000000,16.500000,seven,one
|
||||||
"#);
|
"#);
|
||||||
}
|
}
|
||||||
+1
-3
@@ -3,8 +3,6 @@ use std::marker::PhantomData;
|
|||||||
pub use uom::num_traits::Zero;
|
pub use uom::num_traits::Zero;
|
||||||
pub use uom::si::f64::{Information, Ratio, Time};
|
pub use uom::si::f64::{Information, Ratio, Time};
|
||||||
pub use uom::si::information::byte;
|
pub use uom::si::information::byte;
|
||||||
#[cfg(any(not(windows), test))]
|
|
||||||
pub use uom::si::information::kibibyte;
|
|
||||||
pub use uom::si::ratio::ratio;
|
pub use uom::si::ratio::ratio;
|
||||||
pub use uom::si::time::{microsecond, nanosecond, second};
|
pub use uom::si::time::{microsecond, nanosecond, second};
|
||||||
|
|
||||||
@@ -55,7 +53,7 @@ pub fn format_duration_value(duration: f64, unit: Option<TimeUnit>) -> (String,
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
use uom::si::time::millisecond;
|
use uom::si::{information::kibibyte, time::millisecond};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_time() {
|
fn test_time() {
|
||||||
|
|||||||
+13
-6
@@ -24,6 +24,19 @@ use std::process::{ChildStdout, Command};
|
|||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
struct CPUTimes {
|
||||||
|
/// Total amount of time spent executing in user mode
|
||||||
|
pub user_usec: i64,
|
||||||
|
|
||||||
|
/// Total amount of time spent executing in kernel mode
|
||||||
|
pub system_usec: i64,
|
||||||
|
|
||||||
|
/// Maximum amount of memory used by the process, in bytes
|
||||||
|
pub memory_usage_byte: u64,
|
||||||
|
}
|
||||||
|
|
||||||
/// Discard the output of a child process.
|
/// Discard the output of a child process.
|
||||||
fn discard(output: ChildStdout) {
|
fn discard(output: ChildStdout) {
|
||||||
const CHUNK_SIZE: usize = 64 << 10;
|
const CHUNK_SIZE: usize = 64 << 10;
|
||||||
@@ -82,15 +95,9 @@ pub fn execute_and_measure(mut command: Command) -> Result<Measurement> {
|
|||||||
discard(output);
|
discard(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(windows))]
|
|
||||||
let (time_user, time_system, peak_memory_usage, exit_status) = cpu_timer.stop(child)?;
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
let exit_status = child.wait()?;
|
let exit_status = child.wait()?;
|
||||||
|
|
||||||
let time_wall_clock = wallclock_timer.stop();
|
let time_wall_clock = wallclock_timer.stop();
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
let (time_user, time_system, peak_memory_usage) = cpu_timer.stop();
|
let (time_user, time_system, peak_memory_usage) = cpu_timer.stop();
|
||||||
|
|
||||||
Ok(Measurement {
|
Ok(Measurement {
|
||||||
|
|||||||
+90
-71
@@ -1,89 +1,108 @@
|
|||||||
#![cfg(not(windows))]
|
#![cfg(not(windows))]
|
||||||
|
|
||||||
use std::io;
|
use std::convert::TryFrom;
|
||||||
use std::mem::MaybeUninit;
|
use std::mem;
|
||||||
use std::os::unix::process::ExitStatusExt;
|
|
||||||
use std::process::{Child, ExitStatus};
|
|
||||||
|
|
||||||
use anyhow::Result;
|
use crate::quantity::{byte, microsecond, Information, Time};
|
||||||
|
use crate::timer::CPUTimes;
|
||||||
use crate::quantity::{byte, kibibyte, microsecond, second, Information, Time};
|
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
struct ResourceUsage {
|
pub struct CPUInterval {
|
||||||
/// Total amount of time spent executing in user mode
|
/// Total amount of time spent executing in user mode
|
||||||
pub time_user: Time,
|
pub user: Time,
|
||||||
|
|
||||||
/// Total amount of time spent executing in kernel mode
|
/// Total amount of time spent executing in kernel mode
|
||||||
pub time_system: Time,
|
pub system: Time,
|
||||||
|
|
||||||
/// Maximum amount of memory used by the process, in bytes
|
|
||||||
pub memory_usage: Information,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::useless_conversion)]
|
pub struct CPUTimer {
|
||||||
fn convert_timeval(tv: libc::timeval) -> Time {
|
start_cpu: CPUTimes,
|
||||||
let sec = tv.tv_sec as f64;
|
|
||||||
let usec = tv.tv_usec as f64;
|
|
||||||
|
|
||||||
Time::new::<second>(sec) + Time::new::<microsecond>(usec)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::useless_conversion)]
|
|
||||||
fn wait4(mut child: Child) -> io::Result<(ExitStatus, ResourceUsage)> {
|
|
||||||
drop(child.stdin.take());
|
|
||||||
|
|
||||||
let pid = child.id() as i32;
|
|
||||||
let mut status = 0;
|
|
||||||
let mut rusage = MaybeUninit::zeroed();
|
|
||||||
|
|
||||||
loop {
|
|
||||||
let result = unsafe { libc::wait4(pid, &mut status, 0, rusage.as_mut_ptr()) };
|
|
||||||
if result >= 0 {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
let error = io::Error::last_os_error();
|
|
||||||
if error.kind() != io::ErrorKind::Interrupted {
|
|
||||||
return Err(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let rusage = unsafe { rusage.assume_init() };
|
|
||||||
|
|
||||||
let memory_usage_byte = if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
|
|
||||||
// Linux and *BSD return the value in KibiBytes, Darwin flavors in bytes
|
|
||||||
// A f64 can represent integers up to 2^53 exactly, so we can represent
|
|
||||||
// all values up to 2^53 bytes = 8 PiB exactly. Beyond that, our precision
|
|
||||||
// is larger than 1 byte, but that's considered acceptable.
|
|
||||||
Information::new::<byte>(rusage.ru_maxrss as f64)
|
|
||||||
} else {
|
|
||||||
Information::new::<kibibyte>(rusage.ru_maxrss as f64)
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok((
|
|
||||||
ExitStatus::from_raw(status),
|
|
||||||
ResourceUsage {
|
|
||||||
time_user: convert_timeval(rusage.ru_utime),
|
|
||||||
time_system: convert_timeval(rusage.ru_stime),
|
|
||||||
memory_usage: memory_usage_byte.into(),
|
|
||||||
},
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct CPUTimer {}
|
|
||||||
|
|
||||||
impl CPUTimer {
|
impl CPUTimer {
|
||||||
pub fn start() -> Self {
|
pub fn start() -> Self {
|
||||||
Self {}
|
CPUTimer {
|
||||||
|
start_cpu: get_cpu_times(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stop(&self, child: Child) -> Result<(Time, Time, Information, ExitStatus)> {
|
pub fn stop(&self) -> (Time, Time, Information) {
|
||||||
let (status, usage) = wait4(child)?;
|
let end_cpu = get_cpu_times();
|
||||||
Ok((
|
let cpu_interval = cpu_time_interval(&self.start_cpu, &end_cpu);
|
||||||
usage.time_user,
|
(
|
||||||
usage.time_system,
|
cpu_interval.user,
|
||||||
usage.memory_usage,
|
cpu_interval.system,
|
||||||
status,
|
Information::new::<byte>(end_cpu.memory_usage_byte as f64),
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Read CPU execution times ('user' and 'system')
|
||||||
|
fn get_cpu_times() -> CPUTimes {
|
||||||
|
use libc::{getrusage, rusage, RUSAGE_CHILDREN};
|
||||||
|
|
||||||
|
let result: rusage = unsafe {
|
||||||
|
let mut buf = mem::zeroed();
|
||||||
|
let success = getrusage(RUSAGE_CHILDREN, &mut buf);
|
||||||
|
assert_eq!(0, success);
|
||||||
|
buf
|
||||||
|
};
|
||||||
|
|
||||||
|
const MICROSEC_PER_SEC: i64 = 1000 * 1000;
|
||||||
|
|
||||||
|
// Linux and *BSD return the value in KibiBytes, Darwin flavors in bytes
|
||||||
|
let max_rss_byte = if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
|
||||||
|
result.ru_maxrss
|
||||||
|
} else {
|
||||||
|
result.ru_maxrss * 1024
|
||||||
|
};
|
||||||
|
|
||||||
|
#[allow(clippy::useless_conversion)]
|
||||||
|
CPUTimes {
|
||||||
|
user_usec: i64::from(result.ru_utime.tv_sec) * MICROSEC_PER_SEC
|
||||||
|
+ i64::from(result.ru_utime.tv_usec),
|
||||||
|
system_usec: i64::from(result.ru_stime.tv_sec) * MICROSEC_PER_SEC
|
||||||
|
+ i64::from(result.ru_stime.tv_usec),
|
||||||
|
memory_usage_byte: u64::try_from(max_rss_byte).unwrap_or(0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Compute the time intervals in between two `CPUTimes` snapshots
|
||||||
|
fn cpu_time_interval(start: &CPUTimes, end: &CPUTimes) -> CPUInterval {
|
||||||
|
CPUInterval {
|
||||||
|
user: Time::new::<microsecond>((end.user_usec - start.user_usec) as f64),
|
||||||
|
system: Time::new::<microsecond>((end.system_usec - start.system_usec) as f64),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
use crate::quantity::second;
|
||||||
|
#[cfg(test)]
|
||||||
|
use approx::assert_relative_eq;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cpu_time_interval() {
|
||||||
|
let t_a = CPUTimes {
|
||||||
|
user_usec: 12345,
|
||||||
|
system_usec: 54321,
|
||||||
|
memory_usage_byte: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
let t_b = CPUTimes {
|
||||||
|
user_usec: 20000,
|
||||||
|
system_usec: 70000,
|
||||||
|
memory_usage_byte: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
let t_zero = cpu_time_interval(&t_a, &t_a);
|
||||||
|
assert!(t_zero.user.get::<second>().abs() < f64::EPSILON);
|
||||||
|
assert!(t_zero.system.get::<second>().abs() < f64::EPSILON);
|
||||||
|
|
||||||
|
let t_ab = cpu_time_interval(&t_a, &t_b);
|
||||||
|
assert_relative_eq!(0.007655, t_ab.user.get::<second>());
|
||||||
|
assert_relative_eq!(0.015679, t_ab.system.get::<second>());
|
||||||
|
|
||||||
|
let t_ba = cpu_time_interval(&t_b, &t_a);
|
||||||
|
assert_relative_eq!(-0.007655, t_ba.user.get::<second>());
|
||||||
|
assert_relative_eq!(-0.015679, t_ba.system.get::<second>());
|
||||||
|
}
|
||||||
Reference in new issue
Block a user