Skip to content

Commit a89b1e8

Browse files
committed
refactor: pass Instant to MetricsCounter
1 parent de84f35 commit a89b1e8

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

src/cargo/sources/git/utils.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,9 @@ pub fn with_fetch_options(
697697
with_authentication(url, git_config, |f| {
698698
let mut last_update = Instant::now();
699699
let mut rcb = git2::RemoteCallbacks::new();
700-
let mut counter = MetricsCounter::<10>::new(0);
700+
// We choose `N=10` here to make a `300ms * 10slots ~= 3000ms`
701+
// sliding window for tracking the data transfer rate (in bytes/s).
702+
let mut counter = MetricsCounter::<10>::new(0, last_update);
701703
rcb.credentials(f);
702704
rcb.transfer_progress(|stats| {
703705
let indexed_deltas = stats.indexed_deltas();
@@ -710,10 +712,11 @@ pub fn with_fetch_options(
710712
)
711713
} else {
712714
// Receiving objects.
713-
let duration = last_update.elapsed();
714-
if duration > Duration::from_millis(300) {
715-
counter.add(stats.received_bytes());
716-
last_update = Instant::now();
715+
let now = Instant::now();
716+
// Scrape a `received_bytes` to the counter every 300ms.
717+
if now - last_update > Duration::from_millis(300) {
718+
counter.add(stats.received_bytes(), now);
719+
last_update = now;
717720
}
718721
fn format_bytes(bytes: f32) -> (&'static str, f32) {
719722
static UNITS: [&str; 5] = ["", "K", "M", "G", "T"];

src/cargo/util/counter.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ pub struct MetricsCounter<const N: usize> {
1111

1212
impl<const N: usize> MetricsCounter<N> {
1313
/// Creates a new counter with an initial value.
14-
pub fn new(init: usize) -> Self {
14+
pub fn new(init: usize, init_at: Instant) -> Self {
1515
debug_assert!(N > 0, "number of slots must be greater than zero");
1616
Self {
17-
slots: [(init, Instant::now()); N],
17+
slots: [(init, init_at); N],
1818
index: 0,
1919
}
2020
}
2121

2222
/// Adds record to the counter.
23-
pub fn add(&mut self, data: usize) {
24-
self.slots[self.index] = (data, Instant::now());
23+
pub fn add(&mut self, data: usize, added_at: Instant) {
24+
self.slots[self.index] = (data, added_at);
2525
self.index = (self.index + 1) % N;
2626
}
2727

@@ -42,20 +42,26 @@ impl<const N: usize> MetricsCounter<N> {
4242
#[cfg(test)]
4343
mod tests {
4444
use super::MetricsCounter;
45+
use std::time::{Duration, Instant};
4546

4647
#[test]
4748
fn counter() {
48-
let mut counter = MetricsCounter::<3>::new(0);
49+
let now = Instant::now();
50+
let mut counter = MetricsCounter::<3>::new(0, now);
4951
assert_eq!(counter.rate(), 0f32);
50-
for i in 1..=5 {
51-
counter.add(i);
52-
assert!(counter.rate() > 0f32);
53-
}
52+
counter.add(1, now + Duration::from_secs(1));
53+
assert_eq!(counter.rate(), 1f32);
54+
counter.add(4, now + Duration::from_secs(2));
55+
assert_eq!(counter.rate(), 2f32);
56+
counter.add(7, now + Duration::from_secs(3));
57+
assert_eq!(counter.rate(), 3f32);
58+
counter.add(12, now + Duration::from_secs(4));
59+
assert_eq!(counter.rate(), 4f32);
5460
}
5561

5662
#[test]
5763
#[should_panic(expected = "number of slots must be greater than zero")]
5864
fn counter_zero_slot() {
59-
let _counter = MetricsCounter::<0>::new(0);
65+
let _counter = MetricsCounter::<0>::new(0, Instant::now());
6066
}
6167
}

0 commit comments

Comments
 (0)