Add unit tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by David Peter · parent ef2bef5 · Browse files · 5 files·+47 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+47 added−8 removed
- other:+47 added−8 removed
Provenance
- Commit
241b89f- Tree
5f3eeba- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
ef2bef5
5 files changed+47 added−8 removed
modified:src/hyperfine/benchmark.rs+2 added-2 removed
6 unmodified lines7 8 use hyperfine::internal::{get_progress_bar, max, min, CmdFailureAction, HyperfineOptions, Second,9 Warnings, MIN_EXECUTION_TIME};10 use hyperfine::format::{format_duration, format_duration_unit};11-use hyperfine::cputime::{get_cpu_interval, get_cpu_times};11+use hyperfine::cputime::{cpu_time_interval, get_cpu_times};12 13 /// Results from timing a single shell command14 #[derive(Debug, Copy, Clone)]15 pub struct TimingResult {47 unmodified lines63 // Real time64 let mut time_real = duration.as_secs() as f64 + (duration.subsec_nanos() as f64) * 1e-9;65 66 // User and system time67- let cpu_interval = get_cpu_interval(&start_cpu, &end_cpu);67+ let cpu_interval = cpu_time_interval(&start_cpu, &end_cpu);68 let mut time_user = cpu_interval.user;69 let mut time_system = cpu_interval.system;70 71 // Correct for shell spawning time206 unmodified linesmodified:src/hyperfine/cputime.rs+31 added-6 removed
19 unmodified lines20 /// Total amount of time spent executing in kernel mode21 pub system: Second,22 }23 24-/// Read CPU execution times (user and system time) from kernel24+/// Read CPU execution times ('user' and 'system')25 pub fn get_cpu_times() -> CPUTimes {26 let result: rusage = unsafe {27 let mut buf = mem::zeroed();28 let success = getrusage(RUSAGE_CHILDREN, &mut buf);29 assert_eq!(0, success);30 buf31 };32 33- const USEC_PER_SEC: i64 = 1000 * 1000;33+ const MICROSEC_PER_SEC: i64 = 1000 * 1000;34 35 CPUTimes {36- user_usec: i64::from(result.ru_utime.tv_sec) * USEC_PER_SEC36+ user_usec: i64::from(result.ru_utime.tv_sec) * MICROSEC_PER_SEC37 + i64::from(result.ru_utime.tv_usec),38- system_usec: i64::from(result.ru_stime.tv_sec) * USEC_PER_SEC38+ system_usec: i64::from(result.ru_stime.tv_sec) * MICROSEC_PER_SEC39 + i64::from(result.ru_stime.tv_usec),40 }41 }42 43-/// Compute the time intervals in between two CPUTimes snapshots44-pub fn get_cpu_interval(start: &CPUTimes, end: &CPUTimes) -> CPUInterval {43+/// Compute the time intervals in between two `CPUTimes` snapshots44+pub fn cpu_time_interval(start: &CPUTimes, end: &CPUTimes) -> CPUInterval {45 CPUInterval {46 user: ((end.user_usec - start.user_usec) as f64) * 1e-6,47 system: ((end.system_usec - start.system_usec) as f64) * 1e-6,48 }49 }50+51+#[test]52+fn test_cpu_time_interval() {53+ let t_a = CPUTimes {54+ user_usec: 12345,55+ system_usec: 54321,56+ };57+58+ let t_b = CPUTimes {59+ user_usec: 20000,60+ system_usec: 70000,61+ };62+63+ let t_zero = cpu_time_interval(&t_a, &t_a);64+ assert_eq!(0.0, t_zero.user);65+ assert_eq!(0.0, t_zero.system);66+67+ let t_ab = cpu_time_interval(&t_a, &t_b);68+ assert_relative_eq!(0.007655, t_ab.user);69+ assert_relative_eq!(0.015679, t_ab.system);70+71+ let t_ba = cpu_time_interval(&t_b, &t_a);72+ assert_relative_eq!(-0.007655, t_ba.user);73+ assert_relative_eq!(-0.015679, t_ba.system);74+}modified:src/main.rs+4 added
3 unmodified lines4 extern crate indicatif;5 extern crate libc;6 extern crate statistical;7 8+#[cfg(test)]9+#[macro_use]10+extern crate approx;11+12 use std::cmp;13 use std::error::Error;14 use std::io;15 112 unmodified linesmodified:Cargo.lock+7 added
10 unmodified lines11 version = "0.10.2"12 source = "registry+https://github.com/rust-lang/crates.io-index"13 14 [[package]]15+name = "approx"16+version = "0.1.1"17+source = "registry+https://github.com/rust-lang/crates.io-index"18+19+[[package]]20 name = "atty"21 version = "0.2.6"22 source = "registry+https://github.com/rust-lang/crates.io-index"23 dependencies = [65 unmodified lines89 name = "hyperfine"90 version = "0.3.0"91 dependencies = [92 "ansi_term 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)",93+ "approx 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",94 "clap 2.29.2 (registry+https://github.com/rust-lang/crates.io-index)",95 "indicatif 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",96 "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",97 "statistical 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",304 unmodified lines402 403 [metadata]404 "checksum aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d6531d44de723825aa81398a6415283229725a00fa30713812ab9323faa82fc4"405 "checksum ansi_term 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6b3568b48b7cefa6b8ce125f9bb4989e52fbcc29ebea88df04cc7c5f12f70455"406+"checksum approx 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "08abcc3b4e9339e33a3d0a5ed15d84a687350c05689d825e0f6655eef9e76a94"407 "checksum atty 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "8352656fd42c30a0c3c89d26dea01e3b77c0ab2af18230835c15e2e13cd51859"408 "checksum bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b3c30d3802dfb7281680d6285f2ccdaa8c2d8fee41f93805dba5c4cf50dc23cf"409 "checksum clap 2.29.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4151c5790817c7d21bbdc6c3530811f798172915f93258244948b93ba19604a6"410 "checksum clicolors-control 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1f84dec9bc083ce2503908cd305af98bd363da6f54bf8d4bf0ac14ee749ad5d1"43 unmodified linesmodified:Cargo.toml+3 added
17 unmodified lines1819202122232417 unmodified lines[dependencies.clap]version = "2"default-features = falsefeatures = ["suggestions", "color", "wrap_help"]
[dev-dependencies]approx = "0.1.1"