4
4
5
5
use std:: time:: Instant ;
6
6
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 .
8
8
pub struct TransferStats {
9
9
accumulated_chunk_len : usize , // Total length of chunks transferred in the current period
10
10
accumulated_time : u128 , // Total time taken for the transfers in the current period
11
11
pub transfer_speed : u64 , // Calculated transfer speed in bytes per second
12
+ pub total_transferred : u64 , // Cumulative total of all transferred data
12
13
start_time : Instant , // Time when the current period started
13
14
granularity : u32 , // Time period (in milliseconds) over which the transfer speed is calculated
14
15
}
@@ -20,18 +21,20 @@ impl TransferStats {
20
21
accumulated_chunk_len : 0 ,
21
22
accumulated_time : 0 ,
22
23
transfer_speed : 0 ,
24
+ total_transferred : 0 ,
23
25
start_time : Instant :: now ( ) ,
24
26
granularity,
25
27
}
26
28
}
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 .
28
30
pub fn record_chunk_transfer ( & mut self , chunk_len : usize ) {
29
31
let now = Instant :: now ( ) ;
30
32
let it_took = now. duration_since ( self . start_time ) . as_millis ( ) ;
31
33
self . accumulated_chunk_len += chunk_len;
34
+ self . total_transferred += chunk_len as u64 ;
32
35
self . accumulated_time += it_took;
33
36
34
- // If the accumulated time exceeds the granularity, calculate the transfer speed .
37
+ // Calculate transfer speed if accumulated time exceeds granularity.
35
38
if self . accumulated_time >= self . granularity as u128 {
36
39
self . transfer_speed =
37
40
( self . accumulated_chunk_len as u128 / self . accumulated_time * 1024 ) as u64 ;
@@ -47,6 +50,6 @@ impl TransferStats {
47
50
// Provides a default implementation for TransferStats with a granularity of 500 milliseconds.
48
51
impl Default for TransferStats {
49
52
fn default ( ) -> Self {
50
- Self :: start ( 500 ) // Default granularity is 500
53
+ Self :: start ( 500 ) // Default granularity is 500 ms
51
54
}
52
55
}
0 commit comments