mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-03-31 17:55:36 +00:00
udp: simplify statistics code further
This commit is contained in:
parent
953ede7f67
commit
c192ee6e5a
2 changed files with 42 additions and 66 deletions
|
|
@ -35,7 +35,7 @@ impl StatisticsCollector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn collect_from_shared(&mut self) -> FormattedStatistics {
|
pub fn collect_from_shared(&mut self) -> CollectedStatistics {
|
||||||
let requests_received = Self::fetch_and_reset(&self.shared.requests_received);
|
let requests_received = Self::fetch_and_reset(&self.shared.requests_received);
|
||||||
let responses_sent_connect = Self::fetch_and_reset(&self.shared.responses_sent_connect);
|
let responses_sent_connect = Self::fetch_and_reset(&self.shared.responses_sent_connect);
|
||||||
let responses_sent_announce = Self::fetch_and_reset(&self.shared.responses_sent_announce);
|
let responses_sent_announce = Self::fetch_and_reset(&self.shared.responses_sent_announce);
|
||||||
|
|
@ -43,29 +43,50 @@ impl StatisticsCollector {
|
||||||
let responses_sent_error = Self::fetch_and_reset(&self.shared.responses_sent_error);
|
let responses_sent_error = Self::fetch_and_reset(&self.shared.responses_sent_error);
|
||||||
let bytes_received = Self::fetch_and_reset(&self.shared.bytes_received);
|
let bytes_received = Self::fetch_and_reset(&self.shared.bytes_received);
|
||||||
let bytes_sent = Self::fetch_and_reset(&self.shared.bytes_sent);
|
let bytes_sent = Self::fetch_and_reset(&self.shared.bytes_sent);
|
||||||
|
|
||||||
let num_torrents = Self::sum_atomic_usizes(&self.shared.torrents);
|
let num_torrents = Self::sum_atomic_usizes(&self.shared.torrents);
|
||||||
let num_peers = Self::sum_atomic_usizes(&self.shared.peers);
|
let num_peers = Self::sum_atomic_usizes(&self.shared.peers);
|
||||||
|
|
||||||
let now = Instant::now();
|
let elapsed = {
|
||||||
|
let now = Instant::now();
|
||||||
|
|
||||||
let elapsed = (now - self.last_update).as_secs_f64();
|
let elapsed = (now - self.last_update).as_secs_f64();
|
||||||
|
|
||||||
self.last_update = now;
|
self.last_update = now;
|
||||||
|
|
||||||
let collected_statistics = CollectedStatistics {
|
elapsed
|
||||||
requests_per_second: requests_received / elapsed,
|
|
||||||
responses_per_second_connect: responses_sent_connect / elapsed,
|
|
||||||
responses_per_second_announce: responses_sent_announce / elapsed,
|
|
||||||
responses_per_second_scrape: responses_sent_scrape / elapsed,
|
|
||||||
responses_per_second_error: responses_sent_error / elapsed,
|
|
||||||
bytes_received_per_second: bytes_received / elapsed,
|
|
||||||
bytes_sent_per_second: bytes_sent / elapsed,
|
|
||||||
num_torrents,
|
|
||||||
num_peers,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
FormattedStatistics::new(collected_statistics, self.last_complete_histogram.clone())
|
let requests_per_second = requests_received / elapsed;
|
||||||
|
let responses_per_second_connect = responses_sent_connect / elapsed;
|
||||||
|
let responses_per_second_announce = responses_sent_announce / elapsed;
|
||||||
|
let responses_per_second_scrape = responses_sent_scrape / elapsed;
|
||||||
|
let responses_per_second_error = responses_sent_error / elapsed;
|
||||||
|
let bytes_received_per_second = bytes_received / elapsed;
|
||||||
|
let bytes_sent_per_second = bytes_sent / elapsed;
|
||||||
|
|
||||||
|
let responses_per_second_total = responses_per_second_connect
|
||||||
|
+ responses_per_second_announce
|
||||||
|
+ responses_per_second_scrape
|
||||||
|
+ responses_per_second_error;
|
||||||
|
|
||||||
|
CollectedStatistics {
|
||||||
|
requests_per_second: (requests_per_second as usize).to_formatted_string(&Locale::en),
|
||||||
|
responses_per_second_total: (responses_per_second_total as usize)
|
||||||
|
.to_formatted_string(&Locale::en),
|
||||||
|
responses_per_second_connect: (responses_per_second_connect as usize)
|
||||||
|
.to_formatted_string(&Locale::en),
|
||||||
|
responses_per_second_announce: (responses_per_second_announce as usize)
|
||||||
|
.to_formatted_string(&Locale::en),
|
||||||
|
responses_per_second_scrape: (responses_per_second_scrape as usize)
|
||||||
|
.to_formatted_string(&Locale::en),
|
||||||
|
responses_per_second_error: (responses_per_second_error as usize)
|
||||||
|
.to_formatted_string(&Locale::en),
|
||||||
|
rx_mbits: format!("{:.2}", bytes_received_per_second * 8.0 / 1_000_000.0),
|
||||||
|
tx_mbits: format!("{:.2}", bytes_sent_per_second * 8.0 / 1_000_000.0),
|
||||||
|
num_torrents: num_torrents.to_formatted_string(&Locale::en),
|
||||||
|
num_peers: num_peers.to_formatted_string(&Locale::en),
|
||||||
|
peer_histogram: self.last_complete_histogram.clone(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sum_atomic_usizes(values: &[AtomicUsize]) -> usize {
|
fn sum_atomic_usizes(values: &[AtomicUsize]) -> usize {
|
||||||
|
|
@ -77,21 +98,8 @@ impl StatisticsCollector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
struct CollectedStatistics {
|
|
||||||
requests_per_second: f64,
|
|
||||||
responses_per_second_connect: f64,
|
|
||||||
responses_per_second_announce: f64,
|
|
||||||
responses_per_second_scrape: f64,
|
|
||||||
responses_per_second_error: f64,
|
|
||||||
bytes_received_per_second: f64,
|
|
||||||
bytes_sent_per_second: f64,
|
|
||||||
num_torrents: usize,
|
|
||||||
num_peers: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize)]
|
#[derive(Clone, Debug, Serialize)]
|
||||||
pub struct FormattedStatistics {
|
pub struct CollectedStatistics {
|
||||||
pub requests_per_second: String,
|
pub requests_per_second: String,
|
||||||
pub responses_per_second_total: String,
|
pub responses_per_second_total: String,
|
||||||
pub responses_per_second_connect: String,
|
pub responses_per_second_connect: String,
|
||||||
|
|
@ -105,38 +113,6 @@ pub struct FormattedStatistics {
|
||||||
pub peer_histogram: PeerHistogramStatistics,
|
pub peer_histogram: PeerHistogramStatistics,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FormattedStatistics {
|
|
||||||
fn new(statistics: CollectedStatistics, peer_histogram: PeerHistogramStatistics) -> Self {
|
|
||||||
let rx_mbits = statistics.bytes_received_per_second * 8.0 / 1_000_000.0;
|
|
||||||
let tx_mbits = statistics.bytes_sent_per_second * 8.0 / 1_000_000.0;
|
|
||||||
|
|
||||||
let responses_per_second_total = statistics.responses_per_second_connect
|
|
||||||
+ statistics.responses_per_second_announce
|
|
||||||
+ statistics.responses_per_second_scrape
|
|
||||||
+ statistics.responses_per_second_error;
|
|
||||||
|
|
||||||
FormattedStatistics {
|
|
||||||
requests_per_second: (statistics.requests_per_second as usize)
|
|
||||||
.to_formatted_string(&Locale::en),
|
|
||||||
responses_per_second_total: (responses_per_second_total as usize)
|
|
||||||
.to_formatted_string(&Locale::en),
|
|
||||||
responses_per_second_connect: (statistics.responses_per_second_connect as usize)
|
|
||||||
.to_formatted_string(&Locale::en),
|
|
||||||
responses_per_second_announce: (statistics.responses_per_second_announce as usize)
|
|
||||||
.to_formatted_string(&Locale::en),
|
|
||||||
responses_per_second_scrape: (statistics.responses_per_second_scrape as usize)
|
|
||||||
.to_formatted_string(&Locale::en),
|
|
||||||
responses_per_second_error: (statistics.responses_per_second_error as usize)
|
|
||||||
.to_formatted_string(&Locale::en),
|
|
||||||
rx_mbits: format!("{:.2}", rx_mbits),
|
|
||||||
tx_mbits: format!("{:.2}", tx_mbits),
|
|
||||||
num_torrents: statistics.num_torrents.to_formatted_string(&Locale::en),
|
|
||||||
num_peers: statistics.num_peers.to_formatted_string(&Locale::en),
|
|
||||||
peer_histogram,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Default)]
|
#[derive(Clone, Debug, Serialize, Default)]
|
||||||
pub struct PeerHistogramStatistics {
|
pub struct PeerHistogramStatistics {
|
||||||
pub p0: u64,
|
pub p0: u64,
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ use time::format_description::well_known::Rfc2822;
|
||||||
use time::OffsetDateTime;
|
use time::OffsetDateTime;
|
||||||
use tinytemplate::TinyTemplate;
|
use tinytemplate::TinyTemplate;
|
||||||
|
|
||||||
use collector::{FormattedStatistics, StatisticsCollector};
|
use collector::{CollectedStatistics, StatisticsCollector};
|
||||||
|
|
||||||
use crate::common::*;
|
use crate::common::*;
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
|
@ -31,8 +31,8 @@ struct TemplateData {
|
||||||
ipv4_active: bool,
|
ipv4_active: bool,
|
||||||
ipv6_active: bool,
|
ipv6_active: bool,
|
||||||
extended_active: bool,
|
extended_active: bool,
|
||||||
ipv4: FormattedStatistics,
|
ipv4: CollectedStatistics,
|
||||||
ipv6: FormattedStatistics,
|
ipv6: CollectedStatistics,
|
||||||
last_updated: String,
|
last_updated: String,
|
||||||
peer_update_interval: String,
|
peer_update_interval: String,
|
||||||
}
|
}
|
||||||
|
|
@ -113,7 +113,7 @@ pub fn run_statistics_worker(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_to_stdout(config: &Config, statistics: &FormattedStatistics) {
|
fn print_to_stdout(config: &Config, statistics: &CollectedStatistics) {
|
||||||
println!(
|
println!(
|
||||||
" bandwidth: {:>7} Mbit/s in, {:7} Mbit/s out",
|
" bandwidth: {:>7} Mbit/s in, {:7} Mbit/s out",
|
||||||
statistics.rx_mbits, statistics.tx_mbits,
|
statistics.rx_mbits, statistics.tx_mbits,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue