Mercurial > lbo > hg > time_test
changeset 0:95fcaa3adf3b
Initial commit
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sat, 02 Sep 2017 19:35:18 +0200 |
parents | |
children | d4a954e8992a |
files | .hgignore Cargo.toml LICENSE README.md example/Cargo.toml example/src/example.rs example/src/lib.rs src/lib.rs |
diffstat | 8 files changed, 118 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Sat Sep 02 19:35:18 2017 +0200 @@ -0,0 +1,3 @@ +target/ +Cargo.lock +.*[.]rs[.]bk$
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Cargo.toml Sat Sep 02 19:35:18 2017 +0200 @@ -0,0 +1,7 @@ +[package] +name = "time_test" +version = "0.1.0" +authors = ["Lewin Bormann <lewin@lewin-bormann.info>"] + +[dependencies] +time = "0.1"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LICENSE Sat Sep 02 19:35:18 2017 +0200 @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2017 Lewin Bormann <lewin@lewin-bormann.info> + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to +deal in the Software without restriction, including without limitation the +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +IN THE SOFTWARE.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Sat Sep 02 19:35:18 2017 +0200 @@ -0,0 +1,38 @@ +# `time_test` + +`time_test` is a super simple crate that you can use to easily obtain how long +your tests took to run. Using it is simple (see `example/` for an example use +case): + +```rust +#[macro_use] +extern crate time_test; + +#[cfg(test)] +mod tests { + #[test] + fn my_test() { + time_test!(); + + println!("hello world"); + assert!(true); + assert_eq!(1, 1); + } +} +``` + +Adding the `time_test!()` macro to the line in your test from which you want to +measure the test duration will result in the duration that the test has taken +being shown in the test result line: + +``` +$ # 1 test thread so that the output is not garbled. +$ cargo test -- --test-threads=1 + + Finished dev [unoptimized + debuginfo] target(s) in 0.78 secs + Running target/debug/deps/example-a84426a5de188514 + +running 1 test +test example::tests::my_test ... (took PT0.000004178S) ok +``` +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/Cargo.toml Sat Sep 02 19:35:18 2017 +0200 @@ -0,0 +1,7 @@ +[package] +name = "example" +version = "0.1.0" +authors = ["Lewin Bormann <lewin@lewin-bormann.info>"] + +[dependencies] +time_test = { path = "../" }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/src/example.rs Sat Sep 02 19:35:18 2017 +0200 @@ -0,0 +1,10 @@ + +#[cfg(test)] +mod tests { + #[test] + fn my_test() { + time_test!(); + assert!(true); + assert_eq!(1, 1); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/src/lib.rs Sat Sep 02 19:35:18 2017 +0200 @@ -0,0 +1,6 @@ +#![allow(unused_imports)] + +#[macro_use] +extern crate time_test; + +mod example;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lib.rs Sat Sep 02 19:35:18 2017 +0200 @@ -0,0 +1,26 @@ +use std::io::{self, Write}; + +extern crate time; + +/// TestTimer allows to easily time tests. It's recommended to use the time_test!() macro. +pub struct TestTimer(time::Timespec); + +impl TestTimer { + pub fn new() -> TestTimer { + TestTimer(time::get_time()) + } +} + +impl Drop for TestTimer { + fn drop(&mut self) { + write!(io::stderr(), "(took {}) ", time::get_time() - self.0).is_ok(); + } +} + +#[macro_export] +macro_rules! time_test { + () => { + use time_test::TestTimer; + let _tt = TestTimer::new(); + } +}