Skip to content

Commit

Permalink
fix: previous lognormal crate was requiring java. Implemented in pure…
Browse files Browse the repository at this point in the history
… Rust now
  • Loading branch information
beltram committed Jun 13, 2023
1 parent 7c1e315 commit 29089c9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ rand = "0.8"
regex-syntax = { version = "0.6", default-features = false }
rand_regex = { version = "0.15", default-features = false }
thiserror = "1.0"
jandom = "0.3"
rand_distr = "0.4"

isahc = { version = "1.7", optional = true, default-features = false }
reqwest = { version = "0.11", optional = true, default-features = false }
Expand Down
9 changes: 6 additions & 3 deletions lib/src/wiremock/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ pub struct LognormalDelay {
impl LognormalDelay {
/// see [https://github.com/wiremock/wiremock/blob/60e9e858068548786af4a1a434b52fd1376c4d43/src/main/java/com/github/tomakehurst/wiremock/http/LogNormal.java#L52]
pub fn new_sample(&self) -> core::time::Duration {
let seed = rand::random::<i64>();
let rand = jandom::Random::new(seed).next_gaussian();
let milli = ((rand * self.sigma).exp() * self.median as f64).round() as u64;
let mean = self.median as f64;
// TODO: error handling
let normal = rand_distr::LogNormal::new(mean.ln(), self.sigma).unwrap();

use rand_distr::Distribution as _;
let milli = normal.sample(&mut rand::thread_rng()) as u64;
core::time::Duration::from_millis(milli)
}
}
Expand Down

0 comments on commit 29089c9

Please sign in to comment.