Print download speed

This commit is contained in:
Igor Katson 2021-10-02 09:58:57 +01:00
parent 1bcc16c327
commit f2c509359f
3 changed files with 31 additions and 18 deletions

View file

@ -201,6 +201,7 @@ pub struct StatsSnapshot {
pub uploaded_bytes: u64, pub uploaded_bytes: u64,
pub initially_needed_bytes: u64, pub initially_needed_bytes: u64,
pub remaining_bytes: u64, pub remaining_bytes: u64,
pub total_bytes: u64,
pub live_peers: u32, pub live_peers: u32,
pub seen_peers: u32, pub seen_peers: u32,
pub connecting_peers: u32, pub connecting_peers: u32,
@ -234,6 +235,7 @@ pub struct TorrentState {
peer_id: Id20, peer_id: Id20,
lengths: Lengths, lengths: Lengths,
needed: u64, needed: u64,
have_plus_needed: u64,
stats: AtomicStats, stats: AtomicStats,
options: TorrentStateOptions, options: TorrentStateOptions,
@ -271,6 +273,7 @@ impl TorrentState {
..Default::default() ..Default::default()
}, },
needed: needed_bytes, needed: needed_bytes,
have_plus_needed: needed_bytes + have_bytes,
lengths, lengths,
options, options,
@ -565,6 +568,7 @@ impl TorrentState {
downloaded_and_checked_pieces: self.stats.downloaded_pieces.load(Relaxed), downloaded_and_checked_pieces: self.stats.downloaded_pieces.load(Relaxed),
fetched_bytes: self.stats.fetched_bytes.load(Relaxed), fetched_bytes: self.stats.fetched_bytes.load(Relaxed),
uploaded_bytes: self.stats.uploaded.load(Relaxed), uploaded_bytes: self.stats.uploaded.load(Relaxed),
total_bytes: self.have_plus_needed,
live_peers: peer_stats.live as u32, live_peers: peer_stats.live as u32,
seen_peers: g.peers.seen.len() as u32, seen_peers: g.peers.seen.len() as u32,
connecting_peers: peer_stats.connecting as u32, connecting_peers: peer_stats.connecting as u32,

View file

@ -6,6 +6,7 @@ use std::{
use parking_lot::Mutex; use parking_lot::Mutex;
#[derive(Clone, Copy)]
struct ProgressSnapshot { struct ProgressSnapshot {
downloaded_bytes: u64, downloaded_bytes: u64,
instant: Instant, instant: Instant,
@ -44,15 +45,26 @@ impl SpeedEstimator {
} }
pub fn add_snapshot(&self, downloaded_bytes: u64, remaining_bytes: u64, instant: Instant) { pub fn add_snapshot(&self, downloaded_bytes: u64, remaining_bytes: u64, instant: Instant) {
let mut g = self.latest_per_second_snapshots.lock(); let first = {
if g.len() < g.capacity() { let mut g = self.latest_per_second_snapshots.lock();
g.push_back(ProgressSnapshot {
let current = ProgressSnapshot {
downloaded_bytes, downloaded_bytes,
instant, instant,
}); };
return;
} if g.is_empty() {
let first = g.pop_front().unwrap(); g.push_back(current);
return;
} else if g.len() < g.capacity() {
g.push_back(current);
g.front().copied().unwrap()
} else {
let first = g.pop_front().unwrap();
g.push_back(current);
first
}
};
let downloaded_bytes_diff = downloaded_bytes - first.downloaded_bytes; let downloaded_bytes_diff = downloaded_bytes - first.downloaded_bytes;
let elapsed = instant - first.instant; let elapsed = instant - first.instant;
@ -68,10 +80,5 @@ impl SpeedEstimator {
.store(time_remaining_millis_rounded, Ordering::Relaxed); .store(time_remaining_millis_rounded, Ordering::Relaxed);
self.download_bytes_per_second self.download_bytes_per_second
.store(bps as u64, Ordering::Relaxed); .store(bps as u64, Ordering::Relaxed);
g.push_back(ProgressSnapshot {
downloaded_bytes,
instant,
});
} }
} }

View file

@ -376,21 +376,23 @@ async fn main_torrent_info(
loop { loop {
let peer_stats = handle.torrent_state().peer_stats_snapshot(); let peer_stats = handle.torrent_state().peer_stats_snapshot();
let stats = handle.torrent_state().stats_snapshot(); let stats = handle.torrent_state().stats_snapshot();
let needed = stats.initially_needed_bytes; let speed = handle.speed_estimator();
let total = stats.total_bytes;
let progress = stats.total_bytes - stats.remaining_bytes;
let downloaded_pct = if stats.remaining_bytes == 0 { let downloaded_pct = if stats.remaining_bytes == 0 {
100f64 100f64
} else { } else {
(stats.downloaded_and_checked_bytes as f64 / needed as f64) * 100f64 (progress as f64 / total as f64) * 100f64
}; };
info!( info!(
"Stats: downloaded {:.2}% ({:.2}), fetched {}, remaining {:.2} out of {:.2}, uploaded {:.2}, have {:.2}, peers: {{live: {}, connecting: {}, queued: {}, seen: {}}}", "Stats: {:.2}% ({:.2}), down speed {:.2} Mbps, fetched {}, remaining {:.2} of {:.2}, uploaded {:.2}, peers: {{live: {}, connecting: {}, queued: {}, seen: {}}}",
downloaded_pct, downloaded_pct,
SF::new(stats.downloaded_and_checked_bytes), SF::new(progress),
speed.download_mbps(),
SF::new(stats.fetched_bytes), SF::new(stats.fetched_bytes),
SF::new(stats.remaining_bytes), SF::new(stats.remaining_bytes),
SF::new(needed), SF::new(total),
SF::new(stats.uploaded_bytes), SF::new(stats.uploaded_bytes),
SF::new(stats.have_bytes),
peer_stats.live, peer_stats.live,
peer_stats.connecting, peer_stats.connecting,
peer_stats.queued, peer_stats.queued,