From 84f420e2c5f0626904e094ff93b3ce322780901e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Wed, 26 Oct 2022 20:03:59 +0200 Subject: [PATCH] udp bench: use gamma distribution for torrent selection --- aquatic_udp_bench/src/announce.rs | 6 +++--- aquatic_udp_bench/src/common.rs | 10 ++++++---- aquatic_udp_bench/src/scrape.rs | 6 +++--- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/aquatic_udp_bench/src/announce.rs b/aquatic_udp_bench/src/announce.rs index e048bf4..9302764 100644 --- a/aquatic_udp_bench/src/announce.rs +++ b/aquatic_udp_bench/src/announce.rs @@ -5,7 +5,7 @@ use aquatic_common::CanonicalSocketAddr; use crossbeam_channel::{Receiver, Sender}; use indicatif::ProgressIterator; use rand::Rng; -use rand_distr::Pareto; +use rand_distr::Gamma; use aquatic_udp::common::*; use aquatic_udp_protocol::*; @@ -81,14 +81,14 @@ pub fn create_requests( info_hashes: &[InfoHash], number: usize, ) -> Vec<(AnnounceRequest, CanonicalSocketAddr)> { - let pareto = Pareto::new(1., PARETO_SHAPE).unwrap(); + let gamma = Gamma::new(GAMMA_SHAPE, GAMMA_SCALE).unwrap(); let max_index = info_hashes.len() - 1; let mut requests = Vec::new(); for _ in 0..number { - let info_hash_index = pareto_usize(rng, pareto, max_index); + let info_hash_index = gamma_usize(rng, gamma, max_index); let request = AnnounceRequest { connection_id: ConnectionId(0), diff --git a/aquatic_udp_bench/src/common.rs b/aquatic_udp_bench/src/common.rs index f444166..e3885d0 100644 --- a/aquatic_udp_bench/src/common.rs +++ b/aquatic_udp_bench/src/common.rs @@ -1,8 +1,10 @@ use indicatif::{ProgressBar, ProgressStyle}; use rand::Rng; -use rand_distr::Pareto; +use rand_distr::Gamma; + +pub const GAMMA_SHAPE: f64 = 0.2; +pub const GAMMA_SCALE: f64 = 100.0; -pub const PARETO_SHAPE: f64 = 0.1; pub const NUM_INFO_HASHES: usize = 10_000; pub fn create_progress_bar(name: &str, iterations: u64) -> ProgressBar { @@ -12,8 +14,8 @@ 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, max: usize) -> usize { - let p: f64 = rng.sample(pareto); +pub fn gamma_usize(rng: &mut impl Rng, gamma: Gamma, max: usize) -> usize { + let p: f64 = rng.sample(gamma); let p = (p.min(101.0f64) - 1.0) / 100.0; (p * max as f64) as usize diff --git a/aquatic_udp_bench/src/scrape.rs b/aquatic_udp_bench/src/scrape.rs index e233779..0a1229f 100644 --- a/aquatic_udp_bench/src/scrape.rs +++ b/aquatic_udp_bench/src/scrape.rs @@ -5,7 +5,7 @@ use aquatic_common::CanonicalSocketAddr; use crossbeam_channel::{Receiver, Sender}; use indicatif::ProgressIterator; use rand::Rng; -use rand_distr::Pareto; +use rand_distr::Gamma; use aquatic_udp::common::*; use aquatic_udp_protocol::*; @@ -93,7 +93,7 @@ pub fn create_requests( number: usize, hashes_per_request: usize, ) -> Vec<(ScrapeRequest, CanonicalSocketAddr)> { - let pareto = Pareto::new(1., PARETO_SHAPE).unwrap(); + let gamma = Gamma::new(GAMMA_SHAPE, GAMMA_SCALE).unwrap(); let max_index = info_hashes.len() - 1; @@ -103,7 +103,7 @@ pub fn create_requests( let mut request_info_hashes = Vec::new(); for _ in 0..hashes_per_request { - let info_hash_index = pareto_usize(rng, pareto, max_index); + let info_hash_index = gamma_usize(rng, gamma, max_index); request_info_hashes.push(info_hashes[info_hash_index]) }