44
55use std:: time:: Instant ;
66
7- // The TransferStats struct is used to track and calculate the transfer speed of data chunks over time .
7+ // The TransferStats struct tracks both transfer speed and cumulative transfer progress .
88pub struct TransferStats {
99 accumulated_chunk_len : usize , // Total length of chunks transferred in the current period
1010 accumulated_time : u128 , // Total time taken for the transfers in the current period
1111 pub transfer_speed : u64 , // Calculated transfer speed in bytes per second
12+ pub total_transferred : u64 , // Cumulative total of all transferred data
1213 start_time : Instant , // Time when the current period started
1314 granularity : u32 , // Time period (in milliseconds) over which the transfer speed is calculated
1415}
@@ -20,18 +21,20 @@ impl TransferStats {
2021 accumulated_chunk_len : 0 ,
2122 accumulated_time : 0 ,
2223 transfer_speed : 0 ,
24+ total_transferred : 0 ,
2325 start_time : Instant :: now ( ) ,
2426 granularity,
2527 }
2628 }
27- // Records the transfer of a data chunk and updates the transfer speed if the granularity period has elapsed .
29+ // Records the transfer of a data chunk and updates both transfer speed and total progress .
2830 pub fn record_chunk_transfer ( & mut self , chunk_len : usize ) {
2931 let now = Instant :: now ( ) ;
3032 let it_took = now. duration_since ( self . start_time ) . as_millis ( ) ;
3133 self . accumulated_chunk_len += chunk_len;
34+ self . total_transferred += chunk_len as u64 ;
3235 self . accumulated_time += it_took;
3336
34- // If the accumulated time exceeds the granularity, calculate the transfer speed .
37+ // Calculate transfer speed if accumulated time exceeds granularity.
3538 if self . accumulated_time >= self . granularity as u128 {
3639 self . transfer_speed =
3740 ( self . accumulated_chunk_len as u128 / self . accumulated_time * 1024 ) as u64 ;
@@ -47,6 +50,6 @@ impl TransferStats {
4750// Provides a default implementation for TransferStats with a granularity of 500 milliseconds.
4851impl Default for TransferStats {
4952 fn default ( ) -> Self {
50- Self :: start ( 500 ) // Default granularity is 500
53+ Self :: start ( 500 ) // Default granularity is 500 ms
5154 }
5255}
0 commit comments