From 241b89f12b401c8ab81503fe6e63e7352c5005bc Mon Sep 17 00:00:00 2001 From: sharkdp Date: Sat, 20 Jan 2018 12:55:16 +0100 Subject: [PATCH] Add unit tests --- Cargo.lock | 7 +++++++ Cargo.toml | 3 +++ src/hyperfine/benchmark.rs | 4 ++-- src/hyperfine/cputime.rs | 37 +++++++++++++++++++++++++++++++------ src/main.rs | 4 ++++ 5 files changed, 47 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7ea6756..baabc5c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,11 @@ name = "ansi_term" version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "approx" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "atty" version = "0.2.6" @@ -85,6 +90,7 @@ name = "hyperfine" version = "0.3.0" dependencies = [ "ansi_term 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", + "approx 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.29.2 (registry+https://github.com/rust-lang/crates.io-index)", "indicatif 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", @@ -397,6 +403,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d6531d44de723825aa81398a6415283229725a00fa30713812ab9323faa82fc4" "checksum ansi_term 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6b3568b48b7cefa6b8ce125f9bb4989e52fbcc29ebea88df04cc7c5f12f70455" +"checksum approx 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "08abcc3b4e9339e33a3d0a5ed15d84a687350c05689d825e0f6655eef9e76a94" "checksum atty 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "8352656fd42c30a0c3c89d26dea01e3b77c0ab2af18230835c15e2e13cd51859" "checksum bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b3c30d3802dfb7281680d6285f2ccdaa8c2d8fee41f93805dba5c4cf50dc23cf" "checksum clap 2.29.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4151c5790817c7d21bbdc6c3530811f798172915f93258244948b93ba19604a6" diff --git a/Cargo.toml b/Cargo.toml index c65dcd1..b06ddcf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,3 +19,6 @@ libc = "0.2" version = "2" default-features = false features = ["suggestions", "color", "wrap_help"] + +[dev-dependencies] +approx = "0.1.1" diff --git a/src/hyperfine/benchmark.rs b/src/hyperfine/benchmark.rs index 6189744..33d7e07 100644 --- a/src/hyperfine/benchmark.rs +++ b/src/hyperfine/benchmark.rs @@ -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; diff --git a/src/hyperfine/cputime.rs b/src/hyperfine/cputime.rs index b5c3d12..74c6a48 100644 --- a/src/hyperfine/cputime.rs +++ b/src/hyperfine/cputime.rs @@ -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); +} diff --git a/src/main.rs b/src/main.rs index 9e9df14..3b3306b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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;