Skip to content
Myr

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

5 files+47 added−8 removed

modified:src/hyperfine/benchmark.rs+2 added-2 removed
6 unmodified lines
78use hyperfine::internal::{get_progress_bar, max, min, CmdFailureAction, HyperfineOptions, Second,9 Warnings, MIN_EXECUTION_TIME};10use hyperfine::format::{format_duration, format_duration_unit};11use hyperfine::cputime::{get_cpu_interval, get_cpu_times};11use hyperfine::cputime::{cpu_time_interval, get_cpu_times};1213/// Results from timing a single shell command14#[derive(Debug, Copy, Clone)]15pub struct TimingResult {
47 unmodified lines
63 // Real time64 let mut time_real = duration.as_secs() as f64 + (duration.subsec_nanos() as f64) * 1e-9;6566 // 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;7071 // Correct for shell spawning time
206 unmodified lines
modified:src/hyperfine/cputime.rs+31 added-6 removed
19 unmodified lines
20 /// Total amount of time spent executing in kernel mode21 pub system: Second,22}2324/// Read CPU execution times (user and system time) from kernel24/// Read CPU execution times ('user' and 'system')25pub 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 };3233 const USEC_PER_SEC: i64 = 1000 * 1000;33 const MICROSEC_PER_SEC: i64 = 1000 * 1000;3435 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}4243/// Compute the time intervals in between two CPUTimes snapshots44pub fn get_cpu_interval(start: &CPUTimes, end: &CPUTimes) -> CPUInterval {43/// Compute the time intervals in between two `CPUTimes` snapshots44pub 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}5051#[test]52fn test_cpu_time_interval() {53 let t_a = CPUTimes {54 user_usec: 12345,55 system_usec: 54321,56 };5758 let t_b = CPUTimes {59 user_usec: 20000,60 system_usec: 70000,61 };6263 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);6667 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);7071 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 lines
4extern crate indicatif;5extern crate libc;6extern crate statistical;78#[cfg(test)]9#[macro_use]10extern crate approx;1112use std::cmp;13use std::error::Error;14use std::io;15
112 unmodified lines
modified:Cargo.lock+7 added
10 unmodified lines
11version = "0.10.2"12source = "registry+https://github.com/rust-lang/crates.io-index"1314[[package]]15name = "approx"16version = "0.1.1"17source = "registry+https://github.com/rust-lang/crates.io-index"1819[[package]]20name = "atty"21version = "0.2.6"22source = "registry+https://github.com/rust-lang/crates.io-index"23dependencies = [
65 unmodified lines
89name = "hyperfine"90version = "0.3.0"91dependencies = [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 lines
402403[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 lines
modified:Cargo.toml+3 added