mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-11-02 01:00:31 +00:00
Add unit tests
This commit is contained in:
1 parent
ef2bef588c
commit
241b89f12b
5 files changed
+47
-8
No files matched your search
@@ -8,7 +8,7 @@ use statistical::{mean, standard_deviation};
|
||||
use hyperfine::internal::{get_progress_bar, max, min, CmdFailureAction, HyperfineOptions, Second,
|
||||
Warnings, MIN_EXECUTION_TIME};
|
||||
use hyperfine::format::{format_duration, format_duration_unit};
|
||||
use hyperfine::cputime::{get_cpu_interval, get_cpu_times};
|
||||
use hyperfine::cputime::{cpu_time_interval, get_cpu_times};
|
||||
|
||||
/// Results from timing a single shell command
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
@@ -64,7 +64,7 @@ pub fn time_shell_command(
|
||||
let mut time_real = duration.as_secs() as f64 + (duration.subsec_nanos() as f64) * 1e-9;
|
||||
|
||||
// User and system time
|
||||
let cpu_interval = get_cpu_interval(&start_cpu, &end_cpu);
|
||||
let cpu_interval = cpu_time_interval(&start_cpu, &end_cpu);
|
||||
let mut time_user = cpu_interval.user;
|
||||
let mut time_system = cpu_interval.system;
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ pub struct CPUInterval {
|
||||
pub system: Second,
|
||||
}
|
||||
|
||||
/// Read CPU execution times (user and system time) from kernel
|
||||
/// Read CPU execution times ('user' and 'system')
|
||||
pub fn get_cpu_times() -> CPUTimes {
|
||||
let result: rusage = unsafe {
|
||||
let mut buf = mem::zeroed();
|
||||
@@ -30,20 +30,45 @@ pub fn get_cpu_times() -> CPUTimes {
|
||||
buf
|
||||
};
|
||||
|
||||
const USEC_PER_SEC: i64 = 1000 * 1000;
|
||||
const MICROSEC_PER_SEC: i64 = 1000 * 1000;
|
||||
|
||||
CPUTimes {
|
||||
user_usec: i64::from(result.ru_utime.tv_sec) * USEC_PER_SEC
|
||||
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) * USEC_PER_SEC
|
||||
system_usec: i64::from(result.ru_stime.tv_sec) * MICROSEC_PER_SEC
|
||||
+ i64::from(result.ru_stime.tv_usec),
|
||||
}
|
||||
}
|
||||
|
||||
/// Compute the time intervals in between two CPUTimes snapshots
|
||||
pub fn get_cpu_interval(start: &CPUTimes, end: &CPUTimes) -> CPUInterval {
|
||||
/// Compute the time intervals in between two `CPUTimes` snapshots
|
||||
pub fn cpu_time_interval(start: &CPUTimes, end: &CPUTimes) -> CPUInterval {
|
||||
CPUInterval {
|
||||
user: ((end.user_usec - start.user_usec) as f64) * 1e-6,
|
||||
system: ((end.system_usec - start.system_usec) as f64) * 1e-6,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cpu_time_interval() {
|
||||
let t_a = CPUTimes {
|
||||
user_usec: 12345,
|
||||
system_usec: 54321,
|
||||
};
|
||||
|
||||
let t_b = CPUTimes {
|
||||
user_usec: 20000,
|
||||
system_usec: 70000,
|
||||
};
|
||||
|
||||
let t_zero = cpu_time_interval(&t_a, &t_a);
|
||||
assert_eq!(0.0, t_zero.user);
|
||||
assert_eq!(0.0, t_zero.system);
|
||||
|
||||
let t_ab = cpu_time_interval(&t_a, &t_b);
|
||||
assert_relative_eq!(0.007655, t_ab.user);
|
||||
assert_relative_eq!(0.015679, t_ab.system);
|
||||
|
||||
let t_ba = cpu_time_interval(&t_b, &t_a);
|
||||
assert_relative_eq!(-0.007655, t_ba.user);
|
||||
assert_relative_eq!(-0.015679, t_ba.system);
|
||||
}
|
||||
@@ -5,6 +5,10 @@ extern crate indicatif;
|
||||
extern crate libc;
|
||||
extern crate statistical;
|
||||
|
||||
#[cfg(test)]
|
||||
#[macro_use]
|
||||
extern crate approx;
|
||||
|
||||
use std::cmp;
|
||||
use std::error::Error;
|
||||
use std::io;
|
||||
|
||||
Reference in new issue
Block a user