mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-02 10:45:30 +00:00
Run rustfmt, clean up aquatic_http_protocol/Cargo.toml
This commit is contained in:
parent
0cc312a78d
commit
d0e716f80b
65 changed files with 1754 additions and 2590 deletions
|
|
@ -1,7 +1,7 @@
|
|||
use std::net::SocketAddr;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use crossbeam_channel::{Sender, Receiver};
|
||||
use crossbeam_channel::{Receiver, Sender};
|
||||
use indicatif::ProgressIterator;
|
||||
use rand::Rng;
|
||||
use rand_distr::Pareto;
|
||||
|
|
@ -12,7 +12,6 @@ use aquatic_udp::config::Config;
|
|||
use crate::common::*;
|
||||
use crate::config::BenchConfig;
|
||||
|
||||
|
||||
pub fn bench_announce_handler(
|
||||
state: &State,
|
||||
bench_config: &BenchConfig,
|
||||
|
|
@ -22,12 +21,7 @@ pub fn bench_announce_handler(
|
|||
rng: &mut impl Rng,
|
||||
info_hashes: &[InfoHash],
|
||||
) -> (usize, Duration) {
|
||||
let requests = create_requests(
|
||||
state,
|
||||
rng,
|
||||
info_hashes,
|
||||
bench_config.num_announce_requests
|
||||
);
|
||||
let requests = create_requests(state, rng, info_hashes, bench_config.num_announce_requests);
|
||||
|
||||
let p = aquatic_config.handlers.max_requests_per_iter * bench_config.num_threads;
|
||||
let mut num_responses = 0usize;
|
||||
|
|
@ -40,8 +34,8 @@ pub fn bench_announce_handler(
|
|||
|
||||
let before = Instant::now();
|
||||
|
||||
for round in (0..bench_config.num_rounds).progress_with(pb){
|
||||
for request_chunk in requests.chunks(p){
|
||||
for round in (0..bench_config.num_rounds).progress_with(pb) {
|
||||
for request_chunk in requests.chunks(p) {
|
||||
for (request, src) in request_chunk {
|
||||
request_sender.send((request.clone().into(), *src)).unwrap();
|
||||
}
|
||||
|
|
@ -49,7 +43,7 @@ pub fn bench_announce_handler(
|
|||
while let Ok((Response::Announce(r), _)) = response_receiver.try_recv() {
|
||||
num_responses += 1;
|
||||
|
||||
if let Some(last_peer) = r.peers.last(){
|
||||
if let Some(last_peer) = r.peers.last() {
|
||||
dummy ^= last_peer.port.0;
|
||||
}
|
||||
}
|
||||
|
|
@ -61,7 +55,7 @@ pub fn bench_announce_handler(
|
|||
if let Ok((Response::Announce(r), _)) = response_receiver.recv() {
|
||||
num_responses += 1;
|
||||
|
||||
if let Some(last_peer) = r.peers.last(){
|
||||
if let Some(last_peer) = r.peers.last() {
|
||||
dummy ^= last_peer.port.0;
|
||||
}
|
||||
}
|
||||
|
|
@ -77,7 +71,6 @@ pub fn bench_announce_handler(
|
|||
(num_responses, elapsed)
|
||||
}
|
||||
|
||||
|
||||
pub fn create_requests(
|
||||
state: &State,
|
||||
rng: &mut impl Rng,
|
||||
|
|
@ -92,12 +85,9 @@ pub fn create_requests(
|
|||
|
||||
let connections = state.connections.lock();
|
||||
|
||||
let connection_keys: Vec<ConnectionKey> = connections.keys()
|
||||
.take(number)
|
||||
.cloned()
|
||||
.collect();
|
||||
let connection_keys: Vec<ConnectionKey> = connections.keys().take(number).cloned().collect();
|
||||
|
||||
for connection_key in connection_keys.into_iter(){
|
||||
for connection_key in connection_keys.into_iter() {
|
||||
let info_hash_index = pareto_usize(rng, pareto, max_index);
|
||||
|
||||
let request = AnnounceRequest {
|
||||
|
|
@ -109,14 +99,14 @@ pub fn create_requests(
|
|||
bytes_uploaded: NumberOfBytes(rng.gen()),
|
||||
bytes_left: NumberOfBytes(rng.gen()),
|
||||
event: AnnounceEvent::Started,
|
||||
ip_address: None,
|
||||
ip_address: None,
|
||||
key: PeerKey(rng.gen()),
|
||||
peers_wanted: NumberOfPeers(rng.gen()),
|
||||
port: Port(rng.gen())
|
||||
port: Port(rng.gen()),
|
||||
};
|
||||
|
||||
requests.push((request, connection_key.socket_addr));
|
||||
}
|
||||
|
||||
requests
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,9 @@ use indicatif::{ProgressBar, ProgressStyle};
|
|||
use rand::Rng;
|
||||
use rand_distr::Pareto;
|
||||
|
||||
|
||||
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 {
|
||||
let t = format!("{:<8} {}", name, "{wide_bar} {pos:>2}/{len:>2}");
|
||||
let style = ProgressStyle::default_bar().template(&t);
|
||||
|
|
@ -14,14 +12,9 @@ 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 {
|
||||
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,5 +1,4 @@
|
|||
use serde::{Serialize, Deserialize};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct BenchConfig {
|
||||
|
|
@ -11,7 +10,6 @@ pub struct BenchConfig {
|
|||
pub num_hashes_per_scrape_request: usize,
|
||||
}
|
||||
|
||||
|
||||
impl Default for BenchConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
|
|
@ -25,5 +23,4 @@ impl Default for BenchConfig {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
impl aquatic_cli_helpers::Config for BenchConfig {}
|
||||
impl aquatic_cli_helpers::Config for BenchConfig {}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
use std::time::{Duration, Instant};
|
||||
|
||||
use crossbeam_channel::{Sender, Receiver};
|
||||
use crossbeam_channel::{Receiver, Sender};
|
||||
use indicatif::ProgressIterator;
|
||||
use rand::{Rng, SeedableRng, thread_rng, rngs::SmallRng};
|
||||
use rand::{rngs::SmallRng, thread_rng, Rng, SeedableRng};
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use aquatic_udp::common::*;
|
||||
|
|
@ -11,16 +11,13 @@ use aquatic_udp::config::Config;
|
|||
use crate::common::*;
|
||||
use crate::config::BenchConfig;
|
||||
|
||||
|
||||
pub fn bench_connect_handler(
|
||||
bench_config: &BenchConfig,
|
||||
aquatic_config: &Config,
|
||||
request_sender: &Sender<(Request, SocketAddr)>,
|
||||
response_receiver: &Receiver<(Response, SocketAddr)>,
|
||||
) -> (usize, Duration) {
|
||||
let requests = create_requests(
|
||||
bench_config.num_connect_requests
|
||||
);
|
||||
let requests = create_requests(bench_config.num_connect_requests);
|
||||
|
||||
let p = aquatic_config.handlers.max_requests_per_iter * bench_config.num_threads;
|
||||
let mut num_responses = 0usize;
|
||||
|
|
@ -33,8 +30,8 @@ pub fn bench_connect_handler(
|
|||
|
||||
let before = Instant::now();
|
||||
|
||||
for round in (0..bench_config.num_rounds).progress_with(pb){
|
||||
for request_chunk in requests.chunks(p){
|
||||
for round in (0..bench_config.num_rounds).progress_with(pb) {
|
||||
for request_chunk in requests.chunks(p) {
|
||||
for (request, src) in request_chunk {
|
||||
request_sender.send((request.clone().into(), *src)).unwrap();
|
||||
}
|
||||
|
|
@ -48,7 +45,7 @@ pub fn bench_connect_handler(
|
|||
let total = bench_config.num_connect_requests * (round + 1);
|
||||
|
||||
while num_responses < total {
|
||||
if let Ok((Response::Connect(r), _)) = response_receiver.recv(){
|
||||
if let Ok((Response::Connect(r), _)) = response_receiver.recv() {
|
||||
num_responses += 1;
|
||||
dummy ^= r.connection_id.0;
|
||||
}
|
||||
|
|
@ -64,7 +61,6 @@ pub fn bench_connect_handler(
|
|||
(num_responses, elapsed)
|
||||
}
|
||||
|
||||
|
||||
pub fn create_requests(number: usize) -> Vec<(ConnectRequest, SocketAddr)> {
|
||||
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
//! Benchmark announce and scrape handlers
|
||||
//!
|
||||
//!
|
||||
//! Example outputs:
|
||||
//! ```
|
||||
//! # Results over 20 rounds with 1 threads
|
||||
|
|
@ -15,14 +15,14 @@
|
|||
//! ```
|
||||
|
||||
use crossbeam_channel::unbounded;
|
||||
use std::time::Duration;
|
||||
use num_format::{Locale, ToFormattedString};
|
||||
use rand::{Rng, thread_rng, rngs::SmallRng, SeedableRng};
|
||||
use rand::{rngs::SmallRng, thread_rng, Rng, SeedableRng};
|
||||
use std::time::Duration;
|
||||
|
||||
use aquatic_cli_helpers::run_app_with_cli_and_config;
|
||||
use aquatic_udp::common::*;
|
||||
use aquatic_udp::config::Config;
|
||||
use aquatic_udp::handlers;
|
||||
use aquatic_cli_helpers::run_app_with_cli_and_config;
|
||||
|
||||
use config::BenchConfig;
|
||||
|
||||
|
|
@ -32,20 +32,17 @@ mod config;
|
|||
mod connect;
|
||||
mod scrape;
|
||||
|
||||
|
||||
#[global_allocator]
|
||||
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
||||
|
||||
|
||||
fn main(){
|
||||
fn main() {
|
||||
run_app_with_cli_and_config::<BenchConfig>(
|
||||
"aquatic_udp_bench: Run aquatic_udp benchmarks",
|
||||
run,
|
||||
None
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
pub fn run(bench_config: BenchConfig) -> ::anyhow::Result<()> {
|
||||
// Setup common state, spawn request handlers
|
||||
|
||||
|
|
@ -62,12 +59,7 @@ pub fn run(bench_config: BenchConfig) -> ::anyhow::Result<()> {
|
|||
let response_sender = response_sender.clone();
|
||||
|
||||
::std::thread::spawn(move || {
|
||||
handlers::run_request_worker(
|
||||
state,
|
||||
config,
|
||||
request_receiver,
|
||||
response_sender
|
||||
)
|
||||
handlers::run_request_worker(state, config, request_receiver, response_sender)
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -90,7 +82,7 @@ pub fn run(bench_config: BenchConfig) -> ::anyhow::Result<()> {
|
|||
&request_sender,
|
||||
&response_receiver,
|
||||
&mut rng,
|
||||
&info_hashes
|
||||
&info_hashes,
|
||||
);
|
||||
|
||||
let s = scrape::bench_scrape_handler(
|
||||
|
|
@ -100,13 +92,12 @@ pub fn run(bench_config: BenchConfig) -> ::anyhow::Result<()> {
|
|||
&request_sender,
|
||||
&response_receiver,
|
||||
&mut rng,
|
||||
&info_hashes
|
||||
&info_hashes,
|
||||
);
|
||||
|
||||
println!(
|
||||
"\n# Results over {} rounds with {} threads",
|
||||
bench_config.num_rounds,
|
||||
bench_config.num_threads,
|
||||
bench_config.num_rounds, bench_config.num_threads,
|
||||
);
|
||||
|
||||
print_results("Connect: ", c.0, c.1);
|
||||
|
|
@ -116,28 +107,18 @@ pub fn run(bench_config: BenchConfig) -> ::anyhow::Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
|
||||
pub fn print_results(
|
||||
request_type: &str,
|
||||
num_responses: usize,
|
||||
duration: Duration,
|
||||
) {
|
||||
let per_second = (
|
||||
(num_responses as f64 / (duration.as_micros() as f64 / 1000000.0)
|
||||
) as usize).to_formatted_string(&Locale::se);
|
||||
pub fn print_results(request_type: &str, num_responses: usize, duration: Duration) {
|
||||
let per_second = ((num_responses as f64 / (duration.as_micros() as f64 / 1000000.0)) as usize)
|
||||
.to_formatted_string(&Locale::se);
|
||||
|
||||
let time_per_request = duration.as_nanos() as f64 / (num_responses as f64);
|
||||
|
||||
println!(
|
||||
"{} {:>10} requests/second, {:>8.2} ns/request",
|
||||
request_type,
|
||||
per_second,
|
||||
time_per_request,
|
||||
request_type, per_second, time_per_request,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
fn create_info_hashes(rng: &mut impl Rng) -> Vec<InfoHash> {
|
||||
let mut info_hashes = Vec::new();
|
||||
|
||||
|
|
@ -146,4 +127,4 @@ fn create_info_hashes(rng: &mut impl Rng) -> Vec<InfoHash> {
|
|||
}
|
||||
|
||||
info_hashes
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use std::net::SocketAddr;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use crossbeam_channel::{Sender, Receiver};
|
||||
use crossbeam_channel::{Receiver, Sender};
|
||||
use indicatif::ProgressIterator;
|
||||
use rand::Rng;
|
||||
use rand_distr::Pareto;
|
||||
|
|
@ -12,7 +12,6 @@ use aquatic_udp::config::Config;
|
|||
use crate::common::*;
|
||||
use crate::config::BenchConfig;
|
||||
|
||||
|
||||
pub fn bench_scrape_handler(
|
||||
state: &State,
|
||||
bench_config: &BenchConfig,
|
||||
|
|
@ -41,8 +40,8 @@ pub fn bench_scrape_handler(
|
|||
|
||||
let before = Instant::now();
|
||||
|
||||
for round in (0..bench_config.num_rounds).progress_with(pb){
|
||||
for request_chunk in requests.chunks(p){
|
||||
for round in (0..bench_config.num_rounds).progress_with(pb) {
|
||||
for request_chunk in requests.chunks(p) {
|
||||
for (request, src) in request_chunk {
|
||||
request_sender.send((request.clone().into(), *src)).unwrap();
|
||||
}
|
||||
|
|
@ -50,7 +49,7 @@ pub fn bench_scrape_handler(
|
|||
while let Ok((Response::Scrape(r), _)) = response_receiver.try_recv() {
|
||||
num_responses += 1;
|
||||
|
||||
if let Some(stat) = r.torrent_stats.last(){
|
||||
if let Some(stat) = r.torrent_stats.last() {
|
||||
dummy ^= stat.leechers.0;
|
||||
}
|
||||
}
|
||||
|
|
@ -59,10 +58,10 @@ pub fn bench_scrape_handler(
|
|||
let total = bench_config.num_scrape_requests * (round + 1);
|
||||
|
||||
while num_responses < total {
|
||||
if let Ok((Response::Scrape(r), _)) = response_receiver.recv(){
|
||||
if let Ok((Response::Scrape(r), _)) = response_receiver.recv() {
|
||||
num_responses += 1;
|
||||
|
||||
if let Some(stat) = r.torrent_stats.last(){
|
||||
if let Some(stat) = r.torrent_stats.last() {
|
||||
dummy ^= stat.leechers.0;
|
||||
}
|
||||
}
|
||||
|
|
@ -78,8 +77,6 @@ pub fn bench_scrape_handler(
|
|||
(num_responses, elapsed)
|
||||
}
|
||||
|
||||
|
||||
|
||||
pub fn create_requests(
|
||||
state: &State,
|
||||
rng: &mut impl Rng,
|
||||
|
|
@ -93,14 +90,11 @@ pub fn create_requests(
|
|||
|
||||
let connections = state.connections.lock();
|
||||
|
||||
let connection_keys: Vec<ConnectionKey> = connections.keys()
|
||||
.take(number)
|
||||
.cloned()
|
||||
.collect();
|
||||
let connection_keys: Vec<ConnectionKey> = connections.keys().take(number).cloned().collect();
|
||||
|
||||
let mut requests = Vec::new();
|
||||
|
||||
for connection_key in connection_keys.into_iter(){
|
||||
for connection_key in connection_keys.into_iter() {
|
||||
let mut request_info_hashes = Vec::new();
|
||||
|
||||
for _ in 0..hashes_per_request {
|
||||
|
|
@ -118,4 +112,4 @@ pub fn create_requests(
|
|||
}
|
||||
|
||||
requests
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue