-
Notifications
You must be signed in to change notification settings - Fork 106
feat: add range based exponential buckets in histogram #233
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
Changes from 2 commits
4881cd5
cc60d05
fdd9860
5107c4c
6a141cc
cdd3b03
153a931
2a1343a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -122,6 +122,27 @@ pub fn exponential_buckets(start: f64, factor: f64, length: u16) -> impl Iterato | |
| .take(length.into()) | ||
| } | ||
|
|
||
| /// Exponential bucket distribution within a range | ||
| /// /// Creates `length` buckets, where the lowest bucket is `min` and the highest bucket is `max`. | ||
| /// /// The final +Inf bucket is not counted and not included in the returned iterator. | ||
| /// /// The function panics if `length` is 0 or negative, or if `min` is 0 or negative. | ||
| fn exponential_buckets_range(min: f64, max: f64, length: u16) -> impl Iterator<Item = f64> { | ||
| if length < 1 { | ||
| panic!("ExponentialBucketsRange length needs a positive length"); | ||
| } | ||
|
||
| if min <= 0.0 { | ||
| panic!("ExponentialBucketsRange min needs to be greater than 0"); | ||
| } | ||
|
||
|
|
||
| // We know max/min and highest bucket. Solve for growth_factor. | ||
| let growth_factor = (max / min).powf(1.0 / (length as f64 - 1.0)); | ||
|
|
||
| iter::repeat(()) | ||
| .enumerate() | ||
| .map(move |(i, _)| min * growth_factor.powf(i as f64)) | ||
| .take(length.into()) | ||
| } | ||
|
|
||
| /// Linear bucket distribution. | ||
| pub fn linear_buckets(start: f64, width: f64, length: u16) -> impl Iterator<Item = f64> { | ||
| iter::repeat(()) | ||
|
|
@@ -166,4 +187,12 @@ mod tests { | |
| linear_buckets(0.0, 1.0, 10).collect::<Vec<_>>() | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn exponential_range() { | ||
| assert_eq!( | ||
| vec![1.0, 2.0, 4.0, 8.0, 16.0, 32.0], | ||
| exponential_buckets_range(1.0, 32.0, 6).collect::<Vec<_>>() | ||
| ); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.