diff --git a/.gitignore b/.gitignore index d3d74fe..ba6be5b 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,6 @@ Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk -.idea/ \ No newline at end of file +.idea/ + +*.svg diff --git a/Cargo.toml b/Cargo.toml index 512538f..c5bf148 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,3 +18,6 @@ maintenance = { status = "passively-maintained" } [dependencies] rand = "0.8" + +[target.'cfg(not(windows))'.dev-dependencies] +pprof = { version = "0.10", features = ["flamegraph"] } diff --git a/src/lib.rs b/src/lib.rs index 45c2aa2..27b1c79 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,19 @@ +//! +//! ```rust +//! use tinymt::{TinyMT64, TinyMT64Seed, TinyMT32}; +//! use rand::{Rng, SeedableRng}; +//! +//! // from nondeterministic seed +//! let mut random = TinyMT64::from_entropy(); +//! let rn = random.gen_range(0.0..1.0); +//! assert!((0.0..1.0).contains(&rn)); +//! +//! // from deterministic seed (reproduction of random number sequence is possible) +//! let mut random = TinyMT64::from_seed(TinyMT64Seed::from(0u64)); +//! let rn = random.gen_range(0.0..1.0); +//! assert!((0.0..1.0).contains(&rn)); +//! ``` +//! use std::cmp::min; use rand::{Error, RngCore, SeedableRng}; @@ -155,20 +171,31 @@ impl RngCore for TinyMT32 { #[cfg(test)] mod test { - use rand::Rng; - - use super::*; #[test] - fn tinymt_usage() { - // from nondeterministic seed + #[cfg(not(target_os = "windows"))] + fn profiling() { + use super::*; + use pprof; + use rand::Rng; + let guard = pprof::ProfilerGuardBuilder::default() + .frequency(1000) + .blocklist(&["libc", "libgcc", "pthread"]) + .build() + .unwrap(); + let mut random = TinyMT64::from_entropy(); - let rn = random.gen_range(0.0..1.0); - assert!((0.0..1.0).contains(&rn)); + for _i in 0..10000 { + let rn = random.gen_range(0.0..1.0); + assert!((0.0..1.0).contains(&rn)); + } - // from deterministic seed (reproduction of random number sequence is possible) - let mut random = TinyMT64::from_seed(TinyMT64Seed::from(0u64)); - let rn = random.gen_range(0.0..1.0); - assert!((0.0..1.0).contains(&rn)); + if let Ok(report) = guard.report().build() { + println!("report: {:?}", &report); + }; + if let Ok(report) = guard.report().build() { + let file = std::fs::File::create("flamegraph.svg").unwrap(); + report.flamegraph(file).unwrap(); + }; } }