Skip to content

Commit

Permalink
add pprof-rs profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
torao committed Jul 13, 2022
1 parent 4fc2aac commit 3fc4a9c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk

.idea/
.idea/

*.svg
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ maintenance = { status = "passively-maintained" }

[dependencies]
rand = "0.8"

[target.'cfg(not(windows))'.dev-dependencies]
pprof = { version = "0.10", features = ["flamegraph"] }
49 changes: 38 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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();
};
}
}

0 comments on commit 3fc4a9c

Please sign in to comment.