pub struct Histogram<T: Counter> { /* private fields */ }
Expand description

Histogram is the core data structure in HdrSample. It records values, and performs analytics.

At its heart, it keeps the count for recorded samples in “buckets” of values. The resolution and distribution of these buckets is tuned based on the desired highest trackable value, as well as the user-specified number of significant decimal digits to preserve. The values for the buckets are kept in a way that resembles floats and doubles: there is a mantissa and an exponent, and each bucket represents a different exponent. The “sub-buckets” within a bucket represent different values for the mantissa.

To a first approximation, the sub-buckets of the first bucket would hold the values 0, 1, 2, 3, …, the sub-buckets of the second bucket would hold 0, 2, 4, 6, …, the third would hold 0, 4, 8, and so on. However, the low half of each bucket (except bucket 0) is unnecessary, since those values are already covered by the sub-buckets of all the preceeding buckets. Thus, Histogram keeps the top half of every such bucket.

For the purposes of explanation, consider a Histogram with 2048 sub-buckets for every bucket, and a lowest discernible value of 1:

The 0th bucket covers 0...2047 in multiples of 1, using all 2048 sub-buckets
The 1st bucket covers 2048..4097 in multiples of 2, using only the top 1024 sub-buckets
The 2nd bucket covers 4096..8191 in multiple of 4, using only the top 1024 sub-buckets
...

Bucket 0 is “special” here. It is the only one that has 2048 entries. All the rest have 1024 entries (because their bottom half overlaps with and is already covered by the all of the previous buckets put together). In other words, the k’th bucket could represent 0 * 2^k to 2048 * 2^k in 2048 buckets with 2^k precision, but the midpoint of 1024 * 2^k = 2048 * 2^(k-1), which is the k-1’th bucket’s end. So, we would use the previous bucket for those lower values as it has better precision.

Implementations

Get the current number of distinct values that can be represented in the histogram.

Get the lowest discernible value for the histogram in its current configuration.

Get the highest trackable value for the histogram in its current configuration.

Get the number of significant value digits kept by this histogram.

👎Deprecated since 6.0.0:

use len instead

Get the total number of samples recorded.

Get the total number of samples recorded.

Returns true if this histogram has no recorded values.

Get the number of buckets used by the histogram to cover the highest trackable value.

This method differs from .len() in that it does not count the sub buckets within each bucket.

This method is probably only useful for testing purposes.

Returns true if this histogram is currently able to auto-resize as new samples are recorded.

Get a copy of this histogram, corrected for coordinated omission.

To compensate for the loss of sampled values when a recorded value is larger than the expected interval between value samples, the new histogram will include an auto-generated additional series of decreasingly-smaller (down to the interval) value records for each count found in the current histogram that is larger than the interval.

Note: This is a post-correction method, as opposed to the at-recording correction method provided by record_correct. The two methods are mutually exclusive, and only one of the two should be be used on a given data set to correct for the same coordinated omission issue.

See notes in the description of the Histogram calls for an illustration of why this corrective behavior is important.

If interval is larger than 0, add auto-generated value records as appropriate if value is larger than interval.

Overwrite this histogram with the given histogram. All data and statistics in this histogram will be overwritten.

Overwrite this histogram with the given histogram while correcting for coordinated omission. All data and statistics in this histogram will be overwritten. See clone_correct for more detailed explanation about how correction is applied

Add the contents of another histogram to this one.

Returns an error if values in the other histogram cannot be stored; see AdditionError.

Add the contents of another histogram to this one, while correcting for coordinated omission.

To compensate for the loss of sampled values when a recorded value is larger than the expected interval between value samples, the values added will include an auto-generated additional series of decreasingly-smaller (down to the given interval) value records for each count found in the current histogram that is larger than interval.

Note: This is a post-recording correction method, as opposed to the at-recording correction method provided by record_correct. The two methods are mutually exclusive, and only one of the two should be be used on a given data set to correct for the same coordinated omission issue.

See notes in the description of the Histogram calls for an illustration of why this corrective behavior is important.

See RecordError for error conditions.

Subtract the contents of another histogram from this one.

See SubtractionError for error conditions.

Clear the contents of this histogram while preserving its statistics and configuration.

Reset the contents and statistics of this histogram, preserving only its configuration.

Control whether or not the histogram can auto-resize and auto-adjust it’s highest trackable value as high-valued samples are recorded.

Construct an auto-resizing Histogram with a lowest discernible value of 1 and an auto-adjusting highest trackable value. Can auto-resize up to track values up to (i64::max_value() / 2).

See new_with_bounds for info on sigfig.

Construct a Histogram given a known maximum value to be tracked, and a number of significant decimal digits. The histogram will be constructed to implicitly track (distinguish from 0) values as low as 1. Auto-resizing will be disabled.

See new_with_bounds for info on high and sigfig.

Construct a Histogram with known upper and lower bounds for recorded sample values.

low is the lowest value that can be discerned (distinguished from 0) by the histogram, and must be a positive integer that is >= 1. It may be internally rounded down to nearest power of 2. Providing a lowest discernible value (low) is useful is situations where the units used for the histogram’s values are much smaller that the minimal accuracy required. E.g. when tracking time values stated in nanosecond units, where the minimal accuracy required is a microsecond, the proper value for low would be 1000. If you’re not sure, use 1.

high is the highest value to be tracked by the histogram, and must be a positive integer that is >= (2 * low). If you’re not sure, use u64::max_value().

sigfig Specifies the number of significant figures to maintain. This is the number of significant decimal digits to which the histogram will maintain value resolution and separation. Must be in the range [0, 5]. If you’re not sure, use 3. As sigfig increases, memory usage grows exponentially, so choose carefully if there will be many histograms in memory at once or if storage is otherwise a concern.

Returns an error if the provided parameters are invalid; see CreationError.

Construct a Histogram with the same range settings as a given source histogram, duplicating the source’s start/end timestamps (but NOT its contents).

Record value in the histogram.

Returns an error if value exceeds the highest trackable value and auto-resize is disabled.

Record value in the histogram, clamped to the range of the histogram.

This method cannot fail, as any values that are too small or too large to be tracked will automatically be clamed to be in range. Be aware that this will hide extreme outliers from the resulting histogram without warning. Since the values are clamped, the histogram will also not be resized to accomodate the value, even if auto-resize is enabled.

Record multiple samples for a value in the histogram, adding to the value’s current count.

count is the number of occurrences of this value to record.

Returns an error if value cannot be recorded; see RecordError.

Record multiple samples for a value in the histogram, each one clamped to the histogram’s range.

count is the number of occurrences of this value to record.

This method cannot fail, as values that are too small or too large to be recorded will automatically be clamed to be in range. Be aware that this will hide extreme outliers from the resulting histogram without warning. Since the values are clamped, the histogram will also not be resized to accomodate the value, even if auto-resize is enabled.

Record a value in the histogram while correcting for coordinated omission.

See record_n_correct for further documentation.

Record multiple values in the histogram while correcting for coordinated omission.

To compensate for the loss of sampled values when a recorded value is larger than the expected interval between value samples, this method will auto-generate and record an additional series of decreasingly-smaller (down to interval) value records.

Note: This is a at-recording correction method, as opposed to the post-recording correction method provided by correct_clone. The two methods are mutually exclusive, and only one of the two should be be used on a given data set to correct for the same coordinated omission issue.

Returns an error if value exceeds the highest trackable value and auto-resize is disabled.

Iterate through histogram values by quantile levels.

The iteration mechanic for this iterator may appear somewhat confusing, but it yields fairly pleasing output. The iterator starts with a quantile step size of 1/halving_period. For every iteration, it yields a value whose quantile is that much greater than the previously emitted quantile (i.e., initially 0, 0.1, 0.2, etc.). Once halving_period values have been emitted, the quantile step size is halved, and the iteration continues.

ticks_per_half_distance must be at least 1.

The iterator yields an iterators::IterationValue struct.

One subtlety of this iterator is that you can reach a value whose cumulative count yields a quantile of 1.0 far sooner than the quantile iteration would reach 1.0. Consider a histogram with count 1 at value 1, and count 1000000 at value 1000. At any quantile iteration above 1/1000001 = 0.000000999, iteration will have necessarily proceeded to the index for value 1000, which has all the remaining counts, and therefore quantile (for the value) of 1.0. This is why IterationValue has both quantile() and quantile_iterated_to(). Additionally, to avoid a bunch of unhelpful iterations once iteration has reached the last value with non-zero count, quantile iteration will skip straight to 1.0 as well.

use hdrhistogram::Histogram;
use hdrhistogram::iterators::IterationValue;
let mut hist = Histogram::<u64>::new_with_max(10000, 4).unwrap();
for i in 0..10000 {
    hist += i;
}

let mut perc = hist.iter_quantiles(1);

println!("{:?}", hist.iter_quantiles(1).collect::<Vec<_>>());

assert_eq!(
    perc.next(),
    Some(IterationValue::new(hist.value_at_quantile(0.0001), 0.0001, 0.0, 1, 1))
);
// step size = 50
assert_eq!(
    perc.next(),
    Some(IterationValue::new(hist.value_at_quantile(0.5), 0.5, 0.5, 1, 5000 - 1))
);
// step size = 25
assert_eq!(
    perc.next(),
    Some(IterationValue::new(hist.value_at_quantile(0.75), 0.75, 0.75, 1, 2500))
);
// step size = 12.5
assert_eq!(
    perc.next(),
    Some(IterationValue::new(hist.value_at_quantile(0.875), 0.875, 0.875, 1, 1250))
);
// step size = 6.25
assert_eq!(
    perc.next(),
    Some(IterationValue::new(hist.value_at_quantile(0.9375), 0.9375, 0.9375, 1, 625))
);
// step size = 3.125
assert_eq!(
    perc.next(),
    Some(IterationValue::new(hist.value_at_quantile(0.9688), 0.9688, 0.96875, 1, 313))
);
// etc...

Iterates through histogram values using linear value steps. The iteration is performed in steps of size step, each one yielding the count for all values in the preceeding value range of size step. The iterator terminates when all recorded histogram values are exhausted.

The iterator yields an iterators::IterationValue struct.

use hdrhistogram::Histogram;
use hdrhistogram::iterators::IterationValue;
let mut hist = Histogram::<u64>::new_with_max(1000, 3).unwrap();
hist += 100;
hist += 500;
hist += 800;
hist += 850;

let mut perc = hist.iter_linear(100);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(99, hist.quantile_below(99), hist.quantile_below(99), 0, 0))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(199, hist.quantile_below(199), hist.quantile_below(199), 0, 1))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(299, hist.quantile_below(299), hist.quantile_below(299), 0, 0))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(399, hist.quantile_below(399), hist.quantile_below(399), 0, 0))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(499, hist.quantile_below(499), hist.quantile_below(499), 0, 0))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(599, hist.quantile_below(599), hist.quantile_below(599), 0, 1))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(699, hist.quantile_below(699), hist.quantile_below(699), 0, 0))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(799, hist.quantile_below(799), hist.quantile_below(799), 0, 0))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(899, hist.quantile_below(899), hist.quantile_below(899), 0, 2))
);
assert_eq!(perc.next(), None);

Iterates through histogram values at logarithmically increasing levels. The iteration is performed in steps that start at start and increase exponentially according to exp. The iterator terminates when all recorded histogram values are exhausted.

The iterator yields an iterators::IterationValue struct.

use hdrhistogram::Histogram;
use hdrhistogram::iterators::IterationValue;
let mut hist = Histogram::<u64>::new_with_max(1000, 3).unwrap();
hist += 100;
hist += 500;
hist += 800;
hist += 850;

let mut perc = hist.iter_log(1, 10.0);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(0, hist.quantile_below(0), hist.quantile_below(0), 0, 0))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(9, hist.quantile_below(9), hist.quantile_below(9), 0, 0))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(99, hist.quantile_below(99), hist.quantile_below(99), 0, 0))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(999, hist.quantile_below(999), hist.quantile_below(999), 0, 4))
);
assert_eq!(perc.next(), None);

Iterates through all recorded histogram values using the finest granularity steps supported by the underlying representation. The iteration steps through all non-zero recorded value counts, and terminates when all recorded histogram values are exhausted.

The iterator yields an iterators::IterationValue struct.

use hdrhistogram::Histogram;
use hdrhistogram::iterators::IterationValue;
let mut hist = Histogram::<u64>::new_with_max(1000, 3).unwrap();
hist += 100;
hist += 500;
hist += 800;
hist += 850;

let mut perc = hist.iter_recorded();
assert_eq!(
    perc.next(),
    Some(IterationValue::new(100, hist.quantile_below(100), hist.quantile_below(100), 1, 1))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(500, hist.quantile_below(500), hist.quantile_below(500), 1, 1))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(800, hist.quantile_below(800), hist.quantile_below(800), 1, 1))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(850, hist.quantile_below(850), hist.quantile_below(850), 1, 1))
);
assert_eq!(perc.next(), None);

Iterates through all histogram values using the finest granularity steps supported by the underlying representation. The iteration steps through all possible unit value levels, regardless of whether or not there were recorded values for that value level, and terminates when all recorded histogram values are exhausted.

The iterator yields an iterators::IterationValue struct.

use hdrhistogram::Histogram;
use hdrhistogram::iterators::IterationValue;
let mut hist = Histogram::<u64>::new_with_max(10, 1).unwrap();
hist += 1;
hist += 5;
hist += 8;

let mut perc = hist.iter_all();
assert_eq!(perc.next(), Some(IterationValue::new(0, 0.0, 0.0, 0, 0)));
assert_eq!(
    perc.next(),
    Some(IterationValue::new(1, hist.quantile_below(1), hist.quantile_below(1), 1, 1))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(2, hist.quantile_below(2), hist.quantile_below(2), 0, 0))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(3, hist.quantile_below(3), hist.quantile_below(3), 0, 0))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(4, hist.quantile_below(4), hist.quantile_below(4), 0, 0))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(5, hist.quantile_below(5), hist.quantile_below(5), 1, 1))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(6, hist.quantile_below(6), hist.quantile_below(6), 0, 0))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(7, hist.quantile_below(7), hist.quantile_below(7), 0, 0))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(8, hist.quantile_below(8), hist.quantile_below(8), 1, 1))
);
assert_eq!(
    perc.next(),
    Some(IterationValue::new(9, hist.quantile_below(9), hist.quantile_below(9), 0, 0))
);
assert_eq!(perc.next(), Some(IterationValue::new(10, 1.0, 1.0, 0, 0)));

Get the lowest recorded value level in the histogram. If the histogram has no recorded values, the value returned will be 0.

Get the highest recorded value level in the histogram. If the histogram has no recorded values, the value returned is undefined.

Get the lowest recorded non-zero value level in the histogram. If the histogram has no recorded values, the value returned is u64::max_value().

Determine if two values are equivalent with the histogram’s resolution. Equivalent here means that value samples recorded for any two equivalent values are counted in a common total count.

Get the computed mean value of all recorded values in the histogram.

Get the computed standard deviation of all recorded values in the histogram

Get the value at a given percentile.

This is simply value_at_quantile multiplied by 100.0. For best floating-point precision, use value_at_quantile directly.

Get the value at a given quantile.

When the given quantile is > 0.0, the value returned is the value that the given percentage of the overall recorded value entries in the histogram are either smaller than or equivalent to. When the given quantile is 0.0, the value returned is the value that all value entries in the histogram are either larger than or equivalent to.

Two values are considered “equivalent” if self.equivalent would return true.

If the total count of the histogram has exceeded u64::max_value(), this will return inaccurate results.

Get the percentile of samples at and below a given value.

This is simply quantile_below* multiplied by 100.0. For best floating-point precision, use quantile_below` directly.

Get the quantile of samples at or below a given value.

The value returned is the quantile of values recorded in the histogram that are smaller than or equivalent to the given value.

Two values are considered “equivalent” if self.equivalent would return true.

If the value is larger than the maximum representable value, it will be clamped to the max representable value.

If the total count of the histogram has reached u64::max_value(), this will return inaccurate results.

Get the count of recorded values within a range of value levels (inclusive to within the histogram’s resolution).

low gives the lower value bound on the range for which to provide the recorded count. Will be rounded down with lowest_equivalent. Similarly, high gives the higher value bound on the range, and will be rounded up with highest_equivalent. The function returns the total count of values recorded in the histogram within the value range that is >= lowest_equivalent(low) and <= highest_equivalent(high).

If either value is larger than the maximum representable value, it will be clamped to the max representable value.

The count will saturate at u64::max_value().

Get the count of recorded values at a specific value (to within the histogram resolution at the value level).

The count is computed across values recorded in the histogram that are within the value range that is >= lowest_equivalent(value) and <= highest_equivalent(value).

If the value is larger than the maximum representable value, it will be clamped to the max representable value.

Get the lowest value that is equivalent to the given value within the histogram’s resolution. Equivalent here means that value samples recorded for any two equivalent values are counted in a common total count.

Get the highest value that is equivalent to the given value within the histogram’s resolution. Equivalent here means that value samples recorded for any two equivalent values are counted in a common total count.

Note that the return value is capped at u64::max_value().

Get a value that lies in the middle (rounded up) of the range of values equivalent the given value. Equivalent here means that value samples recorded for any two equivalent values are counted in a common total count.

Note that the return value is capped at u64::max_value().

Get the next value that is not equivalent to the given value within the histogram’s resolution. Equivalent means that value samples recorded for any two equivalent values are counted in a common total count.

Note that the return value is capped at u64::max_value().

Get the size (in value units) of the range of values that are equivalent to the given value within the histogram’s resolution. Equivalent here means that value samples recorded for any two equivalent values are counted in a common total count.

Turn this histogram into a SyncHistogram.

Trait Implementations

The resulting type after applying the + operator.
Performs the + operation. Read more
The resulting type after applying the + operator.
Performs the + operation. Read more
Performs the += operation. Read more
Performs the += operation. Read more
Performs the += operation. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more
Performs the -= operation. Read more
Performs the -= operation. Read more
Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.