mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-01 10:15:31 +00:00
move plot_pareto from aquatic_udp_bench crate into own crate
This commit is contained in:
parent
2f13e1e1a8
commit
3addab07f0
14 changed files with 53 additions and 35 deletions
|
|
@ -9,8 +9,6 @@ use rand_distr::Pareto;
|
|||
use aquatic_udp::common::*;
|
||||
use aquatic_udp::config::Config;
|
||||
|
||||
use aquatic_udp_bench::pareto_usize;
|
||||
|
||||
use crate::common::*;
|
||||
use crate::config::BenchConfig;
|
||||
|
||||
|
|
@ -1,44 +0,0 @@
|
|||
use plotly::{Plot, Scatter, Layout};
|
||||
use plotly::common::Title;
|
||||
use plotly::layout::Axis;
|
||||
use rand::{thread_rng, rngs::SmallRng, SeedableRng};
|
||||
use rand_distr::Pareto;
|
||||
|
||||
use aquatic_udp_bench::pareto_usize;
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
use rand::Rng;
|
||||
use rand_distr::Pareto;
|
||||
|
||||
|
||||
pub const PARETO_SHAPE: f64 = 0.1;
|
||||
|
|
@ -12,3 +13,15 @@ pub fn create_progress_bar(name: &str, iterations: u64) -> ProgressBar {
|
|||
|
||||
ProgressBar::new(iterations).with_style(style)
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
use rand::Rng;
|
||||
use rand_distr::Pareto;
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
|
|
@ -9,8 +9,6 @@ use rand_distr::Pareto;
|
|||
use aquatic_udp::common::*;
|
||||
use aquatic_udp::config::Config;
|
||||
|
||||
use aquatic_udp_bench::pareto_usize;
|
||||
|
||||
use crate::common::*;
|
||||
use crate::config::BenchConfig;
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue