move plot_pareto from aquatic_udp_bench crate into own crate

This commit is contained in:
Joakim Frostegård 2020-05-25 19:37:11 +02:00
parent 2f13e1e1a8
commit 3addab07f0
14 changed files with 53 additions and 35 deletions

54
plot_pareto/src/main.rs Normal file
View file

@ -0,0 +1,54 @@
use plotly::{Plot, Scatter, Layout};
use plotly::common::Title;
use plotly::layout::Axis;
use rand::{thread_rng, rngs::SmallRng, SeedableRng, Rng};
use rand_distr::Pareto;
fn main(){
let mut plot = Plot::new();
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
const LEN: usize = 1_000;
const MAX_VAL: usize = LEN - 1;
for pareto_shape in [0.1, 0.2, 0.3, 0.4, 0.5].iter() {
let pareto = Pareto::new(1.0, *pareto_shape).unwrap();
let mut y_axis = [0; LEN];
for _ in 1..1_000_000 {
let index = pareto_usize(&mut rng, pareto, MAX_VAL);
y_axis[index] += 1;
}
let x_axis: Vec<usize> = (0..MAX_VAL).into_iter().collect();
let trace = Scatter::new(x_axis, y_axis.to_vec())
.name(&format!("pareto shape = {}", pareto_shape));
plot.add_trace(trace);
}
let layout = Layout::new()
.title(Title::new("Pareto distribution"))
.xaxis(Axis::new().title(Title::new("Info hash index")))
.yaxis(Axis::new().title(Title::new("Num requests")));
plot.set_layout(layout);
plot.show();
}
pub fn pareto_usize(
rng: &mut impl Rng,
pareto: Pareto<f64>,
max: usize,
) -> usize {
let p: f64 = rng.sample(pareto);
let p = (p.min(101.0f64) - 1.0) / 100.0;
(p * max as f64) as usize
}