Skip to content

Commit

Permalink
metrics: Fix bug where HistogramBuilder did not properly compute num_…
Browse files Browse the repository at this point in the history
…buckets
  • Loading branch information
rcoh committed Nov 7, 2024
1 parent 8897885 commit 1fdb71e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
14 changes: 12 additions & 2 deletions tokio/src/runtime/metrics/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,14 @@ impl HistogramBuilder {
}
None => self.histogram_type,
};
let num_buckets = self.histogram_type.num_buckets();
let num_buckets = histogram_type.num_buckets();

Histogram {
buckets: (0..num_buckets)
.map(|_| MetricAtomicU64::new(0))
.collect::<Vec<_>>()
.into_boxed_slice(),
histogram_type: histogram_type,
histogram_type,
}
}
}
Expand Down Expand Up @@ -303,6 +303,13 @@ mod test {
.build()
}

#[test]
fn test_legacy_builder() {
let mut builder = HistogramBuilder::new();
builder.legacy_mut(|b| b.num_buckets = 20);
assert_eq!(builder.build().num_buckets(), 20);
}

#[test]
fn log_scale_resolution_1() {
let h = HistogramBuilder {
Expand Down Expand Up @@ -355,6 +362,9 @@ mod test {

b.measure(4096, 1);
assert_bucket_eq!(b, 9, 1);

b.measure(u64::MAX, 1);
assert_bucket_eq!(b, 9, 2);
}

#[test]
Expand Down
17 changes: 16 additions & 1 deletion tokio/tests/rt_unstable_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::task::Poll;
use std::thread;
use tokio::macros::support::poll_fn;

use tokio::runtime::{HistogramConfiguration, LogHistogram, Runtime};
use tokio::runtime::{HistogramConfiguration, HistogramScale, LogHistogram, Runtime};
use tokio::task::consume_budget;
use tokio::time::{self, Duration};

Expand Down Expand Up @@ -424,6 +424,21 @@ fn log_histogram() {
assert_eq!(N, n);
}

#[test]
#[allow(deprecated)]
fn legacy_log_histogram() {
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.enable_metrics_poll_time_histogram()
.metrics_poll_count_histogram_scale(HistogramScale::Log)
.metrics_poll_count_histogram_resolution(Duration::from_micros(50))
.metrics_poll_count_histogram_buckets(20)
.build()
.unwrap();
let num_buckets = rt.metrics().poll_time_histogram_num_buckets();
assert_eq!(num_buckets, 20);
}

#[test]
fn log_histogram_default_configuration() {
let rt = tokio::runtime::Builder::new_current_thread()
Expand Down

0 comments on commit 1fdb71e

Please sign in to comment.