-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(upload): Add transfer_speed for downloading and uploading files (#…
…1797) Co-authored-by: Victor Aremu <[email protected]> Co-authored-by: Fabian-Lars <[email protected]>
- Loading branch information
1 parent
e0d2e2c
commit 87cc585
Showing
4 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"upload": "minor" | ||
"upload-js": "minor" | ||
--- | ||
|
||
Added feature for calculating `transfer_speed` during file uploads and downloads |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
use std::time::Instant; | ||
|
||
// The TransferStats struct is used to track and calculate the transfer speed of data chunks over time. | ||
pub struct TransferStats { | ||
accumulated_chunk_len: usize, // Total length of chunks transferred in the current period | ||
accumulated_time: u128, // Total time taken for the transfers in the current period | ||
pub transfer_speed: u64, // Calculated transfer speed in bytes per second | ||
start_time: Instant, // Time when the current period started | ||
granularity: u32, // Time period (in milliseconds) over which the transfer speed is calculated | ||
} | ||
|
||
impl TransferStats { | ||
// Initializes a new TransferStats instance with the specified granularity. | ||
pub fn start(granularity: u32) -> Self { | ||
Self { | ||
accumulated_chunk_len: 0, | ||
accumulated_time: 0, | ||
transfer_speed: 0, | ||
start_time: Instant::now(), | ||
granularity, | ||
} | ||
} | ||
// Records the transfer of a data chunk and updates the transfer speed if the granularity period has elapsed. | ||
pub fn record_chunk_transfer(&mut self, chunk_len: usize) { | ||
let now = Instant::now(); | ||
let it_took = now.duration_since(self.start_time).as_millis(); | ||
self.accumulated_chunk_len += chunk_len; | ||
self.accumulated_time += it_took; | ||
|
||
// If the accumulated time exceeds the granularity, calculate the transfer speed. | ||
if self.accumulated_time >= self.granularity as u128 { | ||
self.transfer_speed = | ||
(self.accumulated_chunk_len as u128 / self.accumulated_time * 1024) as u64; | ||
self.accumulated_chunk_len = 0; | ||
self.accumulated_time = 0; | ||
} | ||
|
||
// Reset the start time for the next period. | ||
self.start_time = now; | ||
} | ||
} | ||
|
||
// Provides a default implementation for TransferStats with a granularity of 500 milliseconds. | ||
impl Default for TransferStats { | ||
fn default() -> Self { | ||
Self::start(500) // Default granularity is 500 | ||
} | ||
} |