Display upload speed in Web UI
This commit is contained in:
parent
4784f3f14b
commit
80df2c1001
8 changed files with 89 additions and 55 deletions
|
|
@ -8,14 +8,14 @@ use parking_lot::Mutex;
|
|||
|
||||
#[derive(Clone, Copy)]
|
||||
struct ProgressSnapshot {
|
||||
downloaded_bytes: u64,
|
||||
progress_bytes: u64,
|
||||
instant: Instant,
|
||||
}
|
||||
|
||||
/// Estimates download speed in a sliding time window.
|
||||
/// Estimates download/upload speed in a sliding time window.
|
||||
pub struct SpeedEstimator {
|
||||
latest_per_second_snapshots: Mutex<VecDeque<ProgressSnapshot>>,
|
||||
download_bytes_per_second: AtomicU64,
|
||||
bytes_per_second: AtomicU64,
|
||||
time_remaining_millis: AtomicU64,
|
||||
}
|
||||
|
||||
|
|
@ -24,7 +24,7 @@ impl SpeedEstimator {
|
|||
assert!(window_seconds > 1);
|
||||
Self {
|
||||
latest_per_second_snapshots: Mutex::new(VecDeque::with_capacity(window_seconds)),
|
||||
download_bytes_per_second: Default::default(),
|
||||
bytes_per_second: Default::default(),
|
||||
time_remaining_millis: Default::default(),
|
||||
}
|
||||
}
|
||||
|
|
@ -37,20 +37,25 @@ impl SpeedEstimator {
|
|||
Some(Duration::from_millis(tr))
|
||||
}
|
||||
|
||||
pub fn download_bps(&self) -> u64 {
|
||||
self.download_bytes_per_second.load(Ordering::Relaxed)
|
||||
pub fn bps(&self) -> u64 {
|
||||
self.bytes_per_second.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
pub fn download_mbps(&self) -> f64 {
|
||||
self.download_bps() as f64 / 1024f64 / 1024f64
|
||||
pub fn mbps(&self) -> f64 {
|
||||
self.bps() as f64 / 1024f64 / 1024f64
|
||||
}
|
||||
|
||||
pub fn add_snapshot(&self, downloaded_bytes: u64, remaining_bytes: u64, instant: Instant) {
|
||||
pub fn add_snapshot(
|
||||
&self,
|
||||
progress_bytes: u64,
|
||||
remaining_bytes: Option<u64>,
|
||||
instant: Instant,
|
||||
) {
|
||||
let first = {
|
||||
let mut g = self.latest_per_second_snapshots.lock();
|
||||
|
||||
let current = ProgressSnapshot {
|
||||
downloaded_bytes,
|
||||
progress_bytes,
|
||||
instant,
|
||||
};
|
||||
|
||||
|
|
@ -67,19 +72,18 @@ impl SpeedEstimator {
|
|||
}
|
||||
};
|
||||
|
||||
let downloaded_bytes_diff = downloaded_bytes - first.downloaded_bytes;
|
||||
let downloaded_bytes_diff = progress_bytes - first.progress_bytes;
|
||||
let elapsed = instant - first.instant;
|
||||
let bps = downloaded_bytes_diff as f64 / elapsed.as_secs_f64();
|
||||
|
||||
let time_remaining_millis_rounded: u64 = if downloaded_bytes_diff > 0 {
|
||||
let time_remaining_secs = remaining_bytes as f64 / bps;
|
||||
let time_remaining_secs = remaining_bytes.unwrap_or_default() as f64 / bps;
|
||||
(time_remaining_secs * 1000f64) as u64
|
||||
} else {
|
||||
0
|
||||
};
|
||||
self.time_remaining_millis
|
||||
.store(time_remaining_millis_rounded, Ordering::Relaxed);
|
||||
self.download_bytes_per_second
|
||||
.store(bps as u64, Ordering::Relaxed);
|
||||
self.bytes_per_second.store(bps as u64, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue