Skip to content

Commit ddbdbc3

Browse files
committed
Misc cleanups of docs & tests
1 parent bf62038 commit ddbdbc3

3 files changed

Lines changed: 35 additions & 24 deletions

File tree

‎tokio/src/runtime/builder.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ impl Builder {
12551255
/// .enable_metrics_poll_time_histogram()
12561256
/// .metrics_poll_time_histogram_configuration(HistogramConfiguration::log(
12571257
/// LogHistogram::builder()
1258-
/// .max_value(Duration::from_millis(5))
1258+
/// .max_value(Duration::from_millis(4))
12591259
/// .min_value(Duration::from_micros(20))
12601260
/// // Set `precision_exact` to `0` to match `HistogramScale::Log`
12611261
/// .precision_exact(0)

‎tokio/src/runtime/metrics/histogram/h2_histogram.rs‎

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -164,25 +164,21 @@ impl LogHistogramBuilder {
164164
/// such that `2^-p` is less than `precision`. To set `p` directly, use
165165
/// [`LogHistogramBuilder::precision_exact`].
166166
///
167-
/// To match the behavior of the previous log histogram implementation, use
168-
/// `builder.precision_exact(0)`. This creates a histogram where each bucket is twice the size
169-
/// of the previous value.
167+
/// Precision controls the size of the "bucket groups" (consecutive buckets with identical
168+
/// ranges). When `p` is 0, each bucket will be twice the size of the previous bucket. To match
169+
/// the behavior of the legacy log histogram implementation, use `builder.precision_exact(0)`.
170170
///
171171
/// The default value is 25% (2^-2)
172172
///
173173
/// The highest supported precision is `0.0977%` `(2^-10)`. Provided values
174174
/// less than this will be truncated.
175175
///
176176
/// # Panics
177-
/// - `precision` < 0
178-
/// - `precision` > 1
177+
/// - `max_error` < 0
178+
/// - `max_error` > 1
179179
pub fn max_error(mut self, max_error: f64) -> Self {
180-
if max_error < 0.0 {
181-
panic!("precision must be >= 0");
182-
};
183-
if max_error > 1.0 {
184-
panic!("precision must be > 1");
185-
};
180+
assert!(max_error > 0.0, "max_error must be greater than 0");
181+
assert!(max_error < 1.0, "max_error must be less than 1");
186182
let mut p = 2;
187183
while 2_f64.powf(-1.0 * p as f64) > max_error && p <= MAX_PRECISION {
188184
p += 1;
@@ -195,16 +191,18 @@ impl LogHistogramBuilder {
195191
///
196192
/// The precision (meaning: the ratio `n/bucket_range(n)` for some given `n`) will be `2^-p`.
197193
///
198-
/// To match the behavior of the previous log histogram implementation, use
199-
/// `builder.precision_exact(0)`. This creates a histogram where each bucket is twice the size
200-
/// of the previous value.
194+
/// Precision controls the number consecutive buckets with identically sized ranges.
195+
/// When `p` is 0, each bucket will be twice the size of the previous bucket (bucket groups are
196+
/// only a single bucket wide).
197+
///
198+
/// To match the behavior of the legacy implementation ([`HistogramScale::Log`]), use `builder.precision_exact(0)`.
201199
///
202200
/// # Panics
203201
/// - `p` > 10
202+
///
203+
/// [`HistogramScale::Log`]: [crate::runtime::HistogramScale]
204204
pub fn precision_exact(mut self, p: u32) -> Self {
205-
if p > MAX_PRECISION {
206-
panic!("precision must be <= 10");
207-
};
205+
assert!(p <= MAX_PRECISION, "precision must be <= {MAX_PRECISION}");
208206
self.precision = Some(p);
209207
self
210208
}
@@ -307,7 +305,6 @@ mod test {
307305
use super::InvalidHistogramConfiguration;
308306
use crate::runtime::metrics::histogram::h2_histogram::LogHistogram;
309307
use crate::runtime::metrics::histogram::HistogramType;
310-
use std::time::Duration;
311308

312309
#[cfg(not(target_family = "wasm"))]
313310
mod proptests {
@@ -345,11 +342,20 @@ mod test {
345342
let first_bucket_end = Duration::from_nanos(histogram.bucket_range(0).end);
346343
let last_bucket_start = Duration::from_nanos(histogram.bucket_range(histogram.num_buckets - 1).start);
347344
let second_last_bucket_start = Duration::from_nanos(histogram.bucket_range(histogram.num_buckets - 2).start);
348-
prop_assert!(first_bucket_end <= min_value, "first bucket {first_bucket_end:?} must be less than {min_value:?}");
349-
prop_assert!(last_bucket_start > max_value, "last bucket start ({last_bucket_start:?} must be at least as big as `max_value` ({max_value:?})");
345+
prop_assert!(
346+
first_bucket_end <= min_value,
347+
"first bucket {first_bucket_end:?} must be less than {min_value:?}"
348+
);
349+
prop_assert!(
350+
last_bucket_start > max_value,
351+
"last bucket start ({last_bucket_start:?} must be at least as big as `max_value` ({max_value:?})"
352+
);
350353

351354
// We should have the exact right number of buckets. The second to last bucket should be strictly less than max value.
352-
prop_assert!(second_last_bucket_start < max_value, "second last bucket end ({second_last_bucket_start:?} must be at least as big as `max_value` ({max_value:?})");
355+
prop_assert!(
356+
second_last_bucket_start < max_value,
357+
"second last bucket end ({second_last_bucket_start:?} must be at least as big as `max_value` ({max_value:?})"
358+
);
353359
}
354360

355361
#[test]

‎tokio/tests/rt_unstable_metrics.rs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,15 @@ fn minimal_log_histogram() {
439439
.unwrap();
440440
let metrics = rt.metrics();
441441
let num_buckets = rt.metrics().poll_time_histogram_num_buckets();
442-
for b in 0..num_buckets {
442+
for b in 1..num_buckets - 1 {
443443
let range = metrics.poll_time_histogram_bucket_range(b);
444444
let size = range.end - range.start;
445-
println!("bucket {b}: {range:?} (size: {size:?})");
445+
// Assert the buckets continue doubling in size
446+
assert_eq!(
447+
size,
448+
Duration::from_nanos((1 << (b - 1)) * 16384),
449+
"incorrect range for {b}"
450+
);
446451
}
447452
assert_eq!(num_buckets, 10);
448453
}

0 commit comments

Comments
 (0)