| 1 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/::atomicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/::{AtomicU32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicU32#, AtomicU64rust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicU64#}; |
| 2 | |
| 3 | use craterust-analyzer cargo histogram 1.5.0 crate/::configrust-analyzer cargo histogram 1.5.0 config/::Configrust-analyzer cargo histogram 1.5.0 config/Config#; |
| 4 | use craterust-analyzer cargo histogram 1.5.0 crate/::{AtomicCountrust-analyzer cargo histogram 1.5.0 count/AtomicCount#, Countrust-analyzer cargo histogram 1.5.0 count/Count#, Errorrust-analyzer cargo histogram 1.5.0 errors/Error#, Histogramrust-analyzer cargo histogram 1.5.0 standard/Histogram#, Histogram32rust-analyzer cargo histogram 1.5.0 standard/Histogram32#}; |
| 5 | |
| 6 | macro_rules! define_atomic_histogramrust-analyzer cargo histogram 1.5.0 atomic/define_atomic_histogram! { |
| 7 | ($name:ident, $count:ty, $atomic:ty, $hist:ident) => { |
| 8 | /// A histogram that uses atomic counters for each bucket. |
| 9 | /// |
| 10 | /// Unlike the non-atomic variant, it cannot be used directly to report |
| 11 | /// percentiles. Instead, a snapshot must be taken which captures the |
| 12 | /// state of the histogram at a point in time. |
| 13 | pub struct $name { |
| 14 | pub(crate) config: Config, |
| 15 | pub(crate) buckets: Box<[$atomic]>, |
| 16 | } |
| 17 | |
| 18 | impl $name { |
| 19 | /// Construct a new atomic histogram from the provided parameters. |
| 20 | /// See [`crate::Config`] for the meaning of the parameters. |
| 21 | pub fn new(grouping_power: u8, max_value_power: u8) -> Result<Self, Error> { |
| 22 | let config = Config::new(grouping_power, max_value_power)?; |
| 23 | Ok(Self::with_config(&config)) |
| 24 | } |
| 25 | |
| 26 | /// Creates a new atomic histogram using a provided [`crate::Config`]. |
| 27 | pub fn with_config(config: &Config) -> Self { |
| 28 | let mut buckets = Vec::with_capacity(config.total_buckets()); |
| 29 | buckets.resize_with(config.total_buckets(), || { |
| 30 | <$atomic as AtomicCount>::new(<$count as Count>::ZERO) |
| 31 | }); |
| 32 | Self { |
| 33 | config: *config, |
| 34 | buckets: buckets.into(), |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /// Increment the bucket that contains `value` by one. |
| 39 | pub fn increment(&self, value: u64) -> Result<(), Error> { |
| 40 | self.add(value, <$count as Count>::ONE) |
| 41 | } |
| 42 | |
| 43 | /// Add `count` to the bucket that contains `value`. |
| 44 | pub fn add(&self, value: u64, count: $count) -> Result<(), Error> { |
| 45 | let index = self.config.value_to_index(value)?; |
| 46 | self.buckets[index].fetch_add_relaxed(count); |
| 47 | Ok(()) |
| 48 | } |
| 49 | |
| 50 | /// Returns the bucket configuration of the histogram. |
| 51 | pub fn config(&self) -> Config { |
| 52 | self.config |
| 53 | } |
| 54 | |
| 55 | /// Read the bucket values into a new non-atomic histogram snapshot. |
| 56 | pub fn load(&self) -> $hist { |
| 57 | let buckets: Vec<$count> = self.buckets.iter().map(|b| b.load_relaxed()).collect(); |
| 58 | $hist { |
| 59 | config: self.config, |
| 60 | buckets: buckets.into(), |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | impl std::fmt::Debug for $name { |
| 66 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 67 | f.debug_struct(stringify!($name)) |
| 68 | .field("config", &self.config) |
| 69 | .finish() |
| 70 | } |
| 71 | } |
| 72 | }; |
| 73 | } |
| 74 | |
| 75 | define_atomic_histogramrust-analyzer cargo histogram 1.5.0 atomic/define_atomic_histogram!!(AtomicHistogramrust-analyzer cargo histogram 1.5.0 atomic/AtomicHistogram#, u64, AtomicU64rust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicU64#, Histogramrust-analyzer cargo histogram 1.5.0 standard/Histogram#); |
| 76 | define_atomic_histogramrust-analyzer cargo histogram 1.5.0 atomic/define_atomic_histogram!!(AtomicHistogram32rust-analyzer cargo histogram 1.5.0 atomic/AtomicHistogram32#, u32, AtomicU32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicU32#, Histogram32rust-analyzer cargo histogram 1.5.0 standard/Histogram32#); |
| 77 | |
| 78 | // NOTE: once stabilized, `target_has_atomic_load_store` is more correct. |
| 79 | // https://github.com/rust-lang/rust/issues/94039 |
| 80 | #[cfg(target_has_atomic = "64")] |
| 81 | impl AtomicHistogramrust-analyzer cargo histogram 1.5.0 atomic/AtomicHistogram# { |
| 82 | /// Drains the bucket values into a new `Histogram`. |
| 83 | /// |
| 84 | /// Unlike [`load`](AtomicHistogram::load), this method resets all bucket |
| 85 | /// values to zero. Uses [`AtomicU64::swap`] under the hood and is |
| 86 | /// available only on platforms that support 64-bit atomics. |
| 87 | pub fn drainrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram]drain().(&self) -> Histogramrust-analyzer cargo histogram 1.5.0 standard/Histogram# { |
| 88 | let buckets: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u64> = self.bucketsrust-analyzer cargo histogram 1.5.0 atomic/AtomicHistogram#buckets..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|b| b.swap_relaxedrust-analyzer cargo histogram 1.5.0 count/impl#[AtomicU64][AtomicCount]swap_relaxed().(0)).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 89 | Histogramrust-analyzer cargo histogram 1.5.0 standard/Histogram# { |
| 90 | configrust-analyzer cargo histogram 1.5.0 standard/Histogram#config.: self.configrust-analyzer cargo histogram 1.5.0 atomic/AtomicHistogram#config., |
| 91 | bucketsrust-analyzer cargo histogram 1.5.0 standard/Histogram#buckets.: buckets.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | #[cfg(target_has_atomic = "32")] |
| 97 | impl AtomicHistogram32rust-analyzer cargo histogram 1.5.0 atomic/AtomicHistogram32# { |
| 98 | /// Drains the bucket values into a new `Histogram32`. Resets all |
| 99 | /// buckets to zero. Available only on platforms that support 32-bit |
| 100 | /// atomics (more widely supported than 64-bit). |
| 101 | pub fn drainrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram32]drain().(&self) -> Histogram32rust-analyzer cargo histogram 1.5.0 standard/Histogram32# { |
| 102 | let buckets: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u32> = self.bucketsrust-analyzer cargo histogram 1.5.0 atomic/AtomicHistogram32#buckets..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|b| b.swap_relaxedrust-analyzer cargo histogram 1.5.0 count/impl#[AtomicU32][AtomicCount]swap_relaxed().(0)).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 103 | Histogram32rust-analyzer cargo histogram 1.5.0 standard/Histogram32# { |
| 104 | configrust-analyzer cargo histogram 1.5.0 standard/Histogram32#config.: self.configrust-analyzer cargo histogram 1.5.0 atomic/AtomicHistogram32#config., |
| 105 | bucketsrust-analyzer cargo histogram 1.5.0 standard/Histogram32#buckets.: buckets.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | #[cfg(test)] |
| 111 | mod testsrust-analyzer cargo histogram 1.5.0 atomic/tests/ { |
| 112 | use craterust-analyzer cargo histogram 1.5.0 crate/::*; |
| 113 | |
| 114 | #[cfg(target_pointer_width = "64")] |
| 115 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 116 | fn sizerust-analyzer cargo histogram 1.5.0 atomic/tests/size().() { |
| 117 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<AtomicHistogramrust-analyzer cargo histogram 1.5.0 atomic/AtomicHistogram#>(), 48); |
| 118 | } |
| 119 | |
| 120 | #[cfg(target_has_atomic = "64")] |
| 121 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 122 | /// Tests that drain properly resets buckets to 0. |
| 123 | fn drainrust-analyzer cargo histogram 1.5.0 atomic/tests/drain().() { |
| 124 | let histogram = AtomicHistogramrust-analyzer cargo histogram 1.5.0 atomic/AtomicHistogram#::newrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram]new().(7, 64).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 125 | for i in 0..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#100 { |
| 126 | let _ = histogram.incrementrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram]increment().(i); |
| 127 | } |
| 128 | let snapshot = histogram.drainrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram]drain().(); |
| 129 | let result = snapshot.quantilerust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantile().(0.50).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 130 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 131 | result.getrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]get().(&Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(0.50).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()), |
| 132 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&Bucketrust-analyzer cargo histogram 1.5.0 bucket/Bucket# { |
| 133 | countrust-analyzer cargo histogram 1.5.0 bucket/Bucket#count.: 1, |
| 134 | rangerust-analyzer cargo histogram 1.5.0 bucket/Bucket#range.: 50..=50, |
| 135 | }) |
| 136 | ); |
| 137 | histogram.incrementrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram]increment().(1000).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 138 | let snapshot = histogram.drainrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram]drain().(); |
| 139 | let result = snapshot.quantilerust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantile().(0.50).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 140 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 141 | result.getrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]get().(&Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(0.50).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()), |
| 142 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&Bucketrust-analyzer cargo histogram 1.5.0 bucket/Bucket# { |
| 143 | countrust-analyzer cargo histogram 1.5.0 bucket/Bucket#count.: 1, |
| 144 | rangerust-analyzer cargo histogram 1.5.0 bucket/Bucket#range.: 1000..=1003, |
| 145 | }) |
| 146 | ); |
| 147 | } |
| 148 | |
| 149 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 150 | fn quantilesrust-analyzer cargo histogram 1.5.0 atomic/tests/quantiles().() { |
| 151 | let histogram = AtomicHistogramrust-analyzer cargo histogram 1.5.0 atomic/AtomicHistogram#::newrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram]new().(7, 64).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 152 | let qs = [0.25, 0.50, 0.75, 0.90, 0.99]; |
| 153 | |
| 154 | // check empty |
| 155 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(histogram.loadrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram]load().().quantilesrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantiles().(&qs).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); |
| 156 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(histogram.loadrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram]load().().quantilerust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantile().(0.5).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); |
| 157 | |
| 158 | // populate and check min/max |
| 159 | for i in 0..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#100 { |
| 160 | let _ = histogram.incrementrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram]increment().(i); |
| 161 | let result = histogram.loadrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram]load().().quantilerust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantile().(0.0).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 162 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 163 | result.getrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]get().(&Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(0.0).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()), |
| 164 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&Bucketrust-analyzer cargo histogram 1.5.0 bucket/Bucket# { |
| 165 | countrust-analyzer cargo histogram 1.5.0 bucket/Bucket#count.: 1, |
| 166 | rangerust-analyzer cargo histogram 1.5.0 bucket/Bucket#range.: 0..=0, |
| 167 | }) |
| 168 | ); |
| 169 | let result = histogram.loadrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram]load().().quantilerust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantile().(1.0).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 170 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 171 | result.getrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]get().(&Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(1.0).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()), |
| 172 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&Bucketrust-analyzer cargo histogram 1.5.0 bucket/Bucket# { |
| 173 | countrust-analyzer cargo histogram 1.5.0 bucket/Bucket#count.: 1, |
| 174 | rangerust-analyzer cargo histogram 1.5.0 bucket/Bucket#range.: i..=i, |
| 175 | }) |
| 176 | ); |
| 177 | } |
| 178 | |
| 179 | for q in qs { |
| 180 | let result = histogram.loadrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram]load().().quantilerust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantile().(q).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 181 | let bucket = result.getrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]get().(&Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(q).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 182 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bucket.endrust-analyzer cargo histogram 1.5.0 bucket/impl#[Bucket]end().(), (q *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). 100.0) as u64); |
| 183 | } |
| 184 | |
| 185 | let result = histogram.loadrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram]load().().quantilerust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantile().(0.999).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 186 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 187 | result.getrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]get().(&Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(0.999).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().endrust-analyzer cargo histogram 1.5.0 bucket/impl#[Bucket]end().(), |
| 188 | 100 |
| 189 | ); |
| 190 | |
| 191 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 192 | histogram.loadrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram]load().().quantilesrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantiles().(&[-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1.0]), |
| 193 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo histogram 1.5.0 errors/Error#::InvalidQuantilerust-analyzer cargo histogram 1.5.0 errors/Error#InvalidQuantile#) |
| 194 | ); |
| 195 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 196 | histogram.loadrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram]load().().quantilesrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantiles().(&[1.01]), |
| 197 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo histogram 1.5.0 errors/Error#::InvalidQuantilerust-analyzer cargo histogram 1.5.0 errors/Error#InvalidQuantile#) |
| 198 | ); |
| 199 | |
| 200 | let result = histogram |
| 201 | .loadrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram]load().() |
| 202 | .quantilesrust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantiles().(&[0.5, 0.9, 0.99, 0.999]) |
| 203 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() |
| 204 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 205 | let values: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<(f64, u64)> = result |
| 206 | .entriesrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]entries().() |
| 207 | .iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]iter().() |
| 208 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(q, b)| (q.as_f64rust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]as_f64().(), b.endrust-analyzer cargo histogram 1.5.0 bucket/impl#[Bucket]end().())) |
| 209 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 210 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(values, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![(0.5, 50), (0.9, 90), (0.99, 99), (0.999, 100)]); |
| 211 | |
| 212 | let _ = histogram.incrementrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram]increment().(1024); |
| 213 | let result = histogram.loadrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram]load().().quantilerust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram]quantile().(0.999).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 214 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 215 | result.getrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]get().(&Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(0.999).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()), |
| 216 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&Bucketrust-analyzer cargo histogram 1.5.0 bucket/Bucket# { |
| 217 | countrust-analyzer cargo histogram 1.5.0 bucket/Bucket#count.: 1, |
| 218 | rangerust-analyzer cargo histogram 1.5.0 bucket/Bucket#range.: 1024..=1031, |
| 219 | }) |
| 220 | ); |
| 221 | } |
| 222 | |
| 223 | #[cfg(target_pointer_width = "64")] |
| 224 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 225 | fn size_u32rust-analyzer cargo histogram 1.5.0 atomic/tests/size_u32().() { |
| 226 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<AtomicHistogram32rust-analyzer cargo histogram 1.5.0 atomic/AtomicHistogram32#>(), 48); |
| 227 | } |
| 228 | |
| 229 | #[cfg(target_has_atomic = "32")] |
| 230 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 231 | fn drain_u32rust-analyzer cargo histogram 1.5.0 atomic/tests/drain_u32().() { |
| 232 | let h = AtomicHistogram32rust-analyzer cargo histogram 1.5.0 atomic/AtomicHistogram32#::newrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram32]new().(7, 64).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 233 | for v in 0..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#100u64 { |
| 234 | h.incrementrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram32]increment().(v).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 235 | } |
| 236 | let snap = h.drainrust-analyzer cargo histogram 1.5.0 atomic/impl#[AtomicHistogram32]drain().(); |
| 237 | let result = snap.quantilerust-analyzer cargo histogram 1.5.0 standard/impl#[Histogram32]quantile().(0.5).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 238 | let q = Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#::newrust-analyzer cargo histogram 1.5.0 quantile/impl#[Quantile]new().(0.5).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 239 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(result.getrust-analyzer cargo histogram 1.5.0 quantile/impl#[QuantilesResult]get().(&q).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().endrust-analyzer cargo histogram 1.5.0 bucket/impl#[Bucket]end().(), 50); |
| 240 | } |
| 241 | } |