Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show transfer rate when fetching/updating registry index #9395

Merged
merged 10 commits into from
Apr 30, 2021
35 changes: 32 additions & 3 deletions src/cargo/sources/git/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::env;
use std::fmt;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{Duration, Instant};
use url::Url;

fn serialize_str<T, S>(t: &T, s: S) -> Result<S::Ok, S::Error>
Expand Down Expand Up @@ -677,7 +678,7 @@ fn reset(repo: &git2::Repository, obj: &git2::Object<'_>, config: &Config) -> Ca
let mut pb = Progress::new("Checkout", config);
let mut opts = git2::build::CheckoutBuilder::new();
opts.progress(|_, cur, max| {
drop(pb.tick(cur, max));
drop(pb.tick(cur, max, ""));
});
debug!("doing reset");
repo.reset(obj, git2::ResetType::Hard, Some(&mut opts))?;
Expand All @@ -694,12 +695,40 @@ pub fn with_fetch_options(
let mut progress = Progress::new("Fetch", config);
network::with_retry(config, || {
with_authentication(url, git_config, |f| {
let mut last_recv = 0.0; // in Byte
let mut last_rate = 0.0; // in Byte/s
let mut last_update = Instant::now();
let mut rcb = git2::RemoteCallbacks::new();
rcb.credentials(f);

rcb.transfer_progress(|stats| {
let indexed_deltas = stats.indexed_deltas();
let msg = if indexed_deltas > 0 {
// Resolving deltas.
format!(" ({}/{})", indexed_deltas, stats.total_deltas())
alexcrichton marked this conversation as resolved.
Show resolved Hide resolved
} else {
// Receiving objects.
let duration = last_update.elapsed();
let (recv, rate) = if duration > Duration::from_secs(1) {
alexcrichton marked this conversation as resolved.
Show resolved Hide resolved
let recv = stats.received_bytes() as f32;
let rate = (recv - last_recv) / duration.as_secs_f32();
last_recv = recv;
last_rate = rate;
last_update = Instant::now();
(recv, rate)
} else {
(last_recv, last_rate)
};
fn format_bytes(bytes: f32) -> (&'static str, f32) {
static UNITS: [&str; 5] = ["", "K", "M", "G", "T"];
let i = (bytes.log2() / 10.0).min(4.0) as usize;
(UNITS[i], bytes / 1024_f32.powi(i as i32))
}
let (rate_unit, rate) = format_bytes(rate);
let (unit, recv) = format_bytes(recv);
format!(" | {:.2}{}iB | {:.2}{}iB/s", recv, unit, rate, rate_unit)
alexcrichton marked this conversation as resolved.
Show resolved Hide resolved
};
progress
.tick(stats.indexed_objects(), stats.total_objects())
.tick(stats.indexed_objects(), stats.total_objects(), &msg)
.is_ok()
});

Expand Down
4 changes: 2 additions & 2 deletions src/cargo/util/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl<'cfg> Progress<'cfg> {
Self::with_style(name, ProgressStyle::Percentage, cfg)
}

pub fn tick(&mut self, cur: usize, max: usize) -> CargoResult<()> {
pub fn tick(&mut self, cur: usize, max: usize, msg: &str) -> CargoResult<()> {
let s = match &mut self.state {
Some(s) => s,
None => return Ok(()),
Expand All @@ -118,7 +118,7 @@ impl<'cfg> Progress<'cfg> {
return Ok(());
}

s.tick(cur, max, "")
s.tick(cur, max, msg)
}

pub fn tick_now(&mut self, cur: usize, max: usize, msg: &str) -> CargoResult<()> {
Expand Down