3
3
4
4
use crate :: core:: GitReference ;
5
5
use crate :: util:: errors:: CargoResult ;
6
- use crate :: util:: { network, Config , IntoUrl , Progress } ;
6
+ use crate :: util:: { network, Config , IntoUrl , MetricsCounter , Progress } ;
7
7
use anyhow:: { anyhow, Context as _} ;
8
8
use cargo_util:: { paths, ProcessBuilder } ;
9
9
use curl:: easy:: List ;
@@ -695,9 +695,9 @@ pub fn with_fetch_options(
695
695
let mut progress = Progress :: new ( "Fetch" , config) ;
696
696
network:: with_retry ( config, || {
697
697
with_authentication ( url, git_config, |f| {
698
- let mut last_recv = 0 ; // in Byte
699
698
let mut last_update = Instant :: now ( ) ;
700
699
let mut rcb = git2:: RemoteCallbacks :: new ( ) ;
700
+ let mut counter = MetricsCounter :: < 10 > :: new ( 0 ) ;
701
701
rcb. credentials ( f) ;
702
702
rcb. transfer_progress ( |stats| {
703
703
let indexed_deltas = stats. indexed_deltas ( ) ;
@@ -711,18 +711,16 @@ pub fn with_fetch_options(
711
711
} else {
712
712
// Receiving objects.
713
713
let duration = last_update. elapsed ( ) ;
714
- let recv = stats. received_bytes ( ) ;
715
- let rate = ( recv - last_recv) as f32 / duration. as_secs_f32 ( ) ;
716
- if duration > Duration :: from_secs ( 3 ) {
717
- last_recv = recv;
714
+ if duration > Duration :: from_millis ( 300 ) {
715
+ counter. add ( stats. received_bytes ( ) ) ;
718
716
last_update = Instant :: now ( ) ;
719
717
}
720
718
fn format_bytes ( bytes : f32 ) -> ( & ' static str , f32 ) {
721
719
static UNITS : [ & str ; 5 ] = [ "" , "K" , "M" , "G" , "T" ] ;
722
720
let i = ( bytes. log2 ( ) / 10.0 ) . min ( 4.0 ) as usize ;
723
721
( UNITS [ i] , bytes / 1024_f32 . powi ( i as i32 ) )
724
722
}
725
- let ( unit, rate) = format_bytes ( rate) ;
723
+ let ( unit, rate) = format_bytes ( counter . rate ( ) ) ;
726
724
format ! ( ", {:.2}{}iB/s" , rate, unit)
727
725
} ;
728
726
progress
0 commit comments