-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
77d993c
refactor: progress.tick support attaching message
weihanglo d58aa8a
feat: show transfer rate and total bytes received
weihanglo 7e3f7d6
fix: remove unnecessary progress tick rate limiting
weihanglo 507d7e4
fix: remove total bytes received from progress message
weihanglo f2172db
fix: message of progress when resolving deltas
weihanglo a813ba0
feat: add metrics counter utility
weihanglo de84f35
refactor: use metrics counter as an abstaction
weihanglo a89b1e8
refactor: pass Instant to MetricsCounter
weihanglo 9df531b
fix: bytes per second should not prefix with `i`
weihanglo e4d4347
comment about caveat of current transfer rate refresh
weihanglo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,67 @@ | ||
use std::time::Instant; | ||
|
||
/// A metrics counter storing only latest `N` records. | ||
pub struct MetricsCounter<const N: usize> { | ||
/// Slots to store metrics. | ||
slots: [(usize, Instant); N], | ||
/// The slot of the oldest record. | ||
/// Also the next slot to store the new record. | ||
index: usize, | ||
} | ||
|
||
impl<const N: usize> MetricsCounter<N> { | ||
/// Creates a new counter with an initial value. | ||
pub fn new(init: usize, init_at: Instant) -> Self { | ||
debug_assert!(N > 0, "number of slots must be greater than zero"); | ||
Self { | ||
slots: [(init, init_at); N], | ||
index: 0, | ||
} | ||
} | ||
|
||
/// Adds record to the counter. | ||
pub fn add(&mut self, data: usize, added_at: Instant) { | ||
self.slots[self.index] = (data, added_at); | ||
self.index = (self.index + 1) % N; | ||
} | ||
|
||
/// Calculates per-second average rate of all slots. | ||
pub fn rate(&self) -> f32 { | ||
let latest = self.slots[self.index.checked_sub(1).unwrap_or(N - 1)]; | ||
let oldest = self.slots[self.index]; | ||
let duration = (latest.1 - oldest.1).as_secs_f32(); | ||
let avg = (latest.0 - oldest.0) as f32 / duration; | ||
if f32::is_nan(avg) { | ||
0f32 | ||
} else { | ||
avg | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::MetricsCounter; | ||
use std::time::{Duration, Instant}; | ||
|
||
#[test] | ||
fn counter() { | ||
let now = Instant::now(); | ||
let mut counter = MetricsCounter::<3>::new(0, now); | ||
assert_eq!(counter.rate(), 0f32); | ||
counter.add(1, now + Duration::from_secs(1)); | ||
assert_eq!(counter.rate(), 1f32); | ||
counter.add(4, now + Duration::from_secs(2)); | ||
assert_eq!(counter.rate(), 2f32); | ||
counter.add(7, now + Duration::from_secs(3)); | ||
assert_eq!(counter.rate(), 3f32); | ||
counter.add(12, now + Duration::from_secs(4)); | ||
assert_eq!(counter.rate(), 4f32); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "number of slots must be greater than zero")] | ||
fn counter_zero_slot() { | ||
let _counter = MetricsCounter::<0>::new(0, Instant::now()); | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As one thought I had while reading this, do you know if libgit2 is guaranteed to call this callback perioidically? If we receive data and then don't receive anything else for 10s, does that mean the transfer rate is "constant" for that 10s even though we don't actually receive anything?
(basically I think for the transfer rate to be accurate we would have to guarantee that this
transfer_progress
callback is called periodically even if the network is idle.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that libgit2 does not periodically call the callback. So yes it maybe be stuck. Git CLI has a similar issue too. IIRC all Git operations share the same timeout config from the same curl handle, so its nearly impossible to reuse it. Therefore we have some options:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think our options are pretty limited here. One option is to remove the download rate (boo) and the other one would be to add some sort of threading that calls the update on a tick. I don't think those are really worth it for now though. Perhaps you can leave a comment here to this effect and we can tackle this in the future if it's actually an issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment added. Also fixed an unit display issue 😂
Thanks for your suggestion!