- OCaml 100%
hdr #
An OCaml implementation of Gil Tene's
High Dynamic Range histogram. It records values
that span several orders of magnitude, such as latencies between a nanosecond
and a second, with the same relative precision at every magnitude. Recording
takes constant time and the memory is bounded. From the counts it reports
percentiles (p50, p99, p999), the minimum and maximum, the mean and the
standard deviation. Values are int; Hdr.Float does the same for float
values, like HdrHistogram's DoubleHistogram.
You choose the precision as a number s of significant decimal figures, from 1 to 5, with 3 as the default. The value range is cut into buckets, each twice as wide as the one before it, and each bucket into S linear sub-buckets, where S is the smallest power of two that is at least 2 × 10^s. For s = 3 that gives S = 2048. After the first bucket, only the upper half of a bucket's sub-buckets covers new values, so bucket b counts the values between 1024 × 2^b and 2048 × 2^b in 1024 slots of width 2^b. Every value is therefore counted to within 1 part in 1024, about 0.1%.
Hdr.record finds the bucket from the position of the value's highest set bit
and the slot from the bits below it, then increments one array cell. That is a
few shifts whatever the value. For B buckets the array holds (B + 1) × S/2
counts, with B = ⌈log2(highest / S)⌉ + 1. Covering 1 ns to 1 s at s = 3 needs
B = 20, which is 21,504 counts or 172,032 bytes of 8-byte integers.
This is what you need to report tail latency. The minimum, average and maximum say nothing about the tail. A histogram with fixed buckets loses resolution at one end of its range. An HDR histogram keeps enough resolution at every magnitude to give the 99.9th percentile without storing each sample.
The program that records the values also computes the percentiles, so hdr
cannot serve as the backend of a Prometheus client. A Prometheus histogram
exposes fixed le buckets and lets the server compute quantiles. A Prometheus
summary needs a sketch that targets chosen quantiles over arbitrary floats,
such as CKMS, the streaming algorithm of Cormode, Korn, Muthukrishnan and
Srivastava (ICDE 2005), available in
ckms. CKMS keeps a small
sample of the values it has seen, sized so that a quantile like p99 falls
within a stated rank error. Use hdr in benchmarks, load generators,
profiling runs and /debug endpoints.
let h = Hdr.v () (* auto-resizing; record latencies in ns *)
let () = List.iter (Hdr.record h) [ 1_200; 3_400; 900; 12_000 ]
let () = Format.printf "%a@." Hdr.pp h
let () = Format.printf "p99 = %d ns@." (Hdr.value_at_percentile h 99.)
Installation #
Install with opam:
$ opam install hdr
The package may not be in the public opam-repository yet. If opam cannot
find it, add the overlay repository first:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install hdr
Coordinated omission #
A load generator that sends requests at a fixed rate and waits for each reply
stops measuring while a response is stalled. The requests it should have sent
in that time are never recorded, and the tail looks better than it was.
record_corrected ~expected_interval adds the samples the stalled loop missed,
as wrk2 and HdrHistogram do:
let () = Hdr.record_corrected h 50_000 ~expected_interval:10_000
API #
v ?significant_figures ?highest () |
new histogram (default 3 sig figs = 0.1% error); omit highest to auto-resize |
record / record_n / record_corrected |
add a value (allocation-free unless a resize triggers) |
value_at_percentile / percentiles |
quantiles |
count / min / max / mean / stddev |
summary statistics |
add |
merge another histogram in |
iter |
fold over the non-empty buckets |
reset |
clear |
pp |
one-line p50/p90/p99/p999/max summary |
Hdr.Float |
the same API over float values (HdrHistogram's DoubleHistogram) |
License #
ISC. See LICENSE.md.