Fix download speed (#14291) (#14295)

(cherry picked from commit 7b49c85aa7)

Co-authored-by: sakridge <sakridge@gmail.com>
This commit is contained in:
mergify[bot]
2020-12-28 02:21:40 +00:00
committed by GitHub
parent 1c91376f78
commit b87e606626

View File

@ -82,6 +82,7 @@ pub fn download_file(
response: R, response: R,
last_print: Instant, last_print: Instant,
current_bytes: usize, current_bytes: usize,
last_print_bytes: usize,
download_size: f32, download_size: f32,
use_progress_bar: bool, use_progress_bar: bool,
} }
@ -94,14 +95,16 @@ pub fn download_file(
} else { } else {
self.current_bytes += n; self.current_bytes += n;
if self.last_print.elapsed().as_secs() > 5 { if self.last_print.elapsed().as_secs() > 5 {
let bytes_f32 = self.current_bytes as f32; let total_bytes_f32 = self.current_bytes as f32;
let diff_bytes_f32 = (self.current_bytes - self.last_print_bytes) as f32;
info!( info!(
"downloaded {} bytes {:.1}% {:.1} bytes/s", "downloaded {} bytes {:.1}% {:.1} bytes/s",
self.current_bytes, self.current_bytes,
100f32 * (bytes_f32 / self.download_size), 100f32 * (total_bytes_f32 / self.download_size),
bytes_f32 / self.last_print.elapsed().as_secs_f32(), diff_bytes_f32 / self.last_print.elapsed().as_secs_f32(),
); );
self.last_print = Instant::now(); self.last_print = Instant::now();
self.last_print_bytes = self.current_bytes;
} }
} }
n n
@ -114,6 +117,7 @@ pub fn download_file(
response, response,
last_print: Instant::now(), last_print: Instant::now(),
current_bytes: 0, current_bytes: 0,
last_print_bytes: 0,
download_size: (download_size as f32).max(1f32), download_size: (download_size as f32).max(1f32),
use_progress_bar, use_progress_bar,
}; };