HdrHistogram Python Wrapper

0.10.3 · active · verified Fri Apr 17

HdrHistogram is a native Python wrapper for the High Dynamic Range Histogram C library, designed for recording and analyzing sampled data (like latencies) with high precision over a wide dynamic range. It's currently at version 0.10.3 and typically sees maintenance releases as needed, with major changes often aligning with the upstream C library or Python version support.

Common errors

Warnings

Install

Imports

Quickstart

Initialize an HdrHistogram, record sample values, and retrieve various statistics including min, max, mean, standard deviation, and specific percentiles. The example also shows how the histogram automatically adjusts to values exceeding its initial maximum.

from hdrhistogram import HdrHistogram

# Create a histogram with a precision of 3 significant digits,
# a max value of 1,000,000 (1 second), and a min value of 1 (1 microsecond).
# The internal C library will auto-adjust max value if exceeded.
h = HdrHistogram(1, 1_000_000, 3)

# Record some values (e.g., latencies in microseconds)
h.record_value(100)
h.record_value(150)
h.record_value(1000)
h.record_value(1000)
h.record_value(1000)
h.record_value(5000)
h.record_value(100_000) # This will exceed the initial max, but the histogram self-adjusts

print(f"Total count: {h.total_count}")
print(f"Min value: {h.min_value}")
print(f"Max value: {h.max_value}")
print(f"Mean value: {h.mean_value:.2f}")
print(f"Std Deviation: {h.std_deviation:.2f}")

print("\nPercentiles:")
print(f"50th percentile: {h.get_value_at_percentile(50):.2f}")
print(f"90th percentile: {h.get_value_at_percentile(90):.2f}")
print(f"99th percentile: {h.get_value_at_percentile(99):.2f}")
print(f"99.9th percentile: {h.get_value_at_percentile(99.9):.2f}")

# You can also iterate through percentiles
print("\nPercentile distribution:")
for p, v in h.get_percentile_to_value_list():
    if p % 10 == 0 or p > 99: # print every 10th percentile and high percentiles
        print(f"{p:>6.2f}th percentile: {v:>10.2f}")

# Reset the histogram
h.reset()
print(f"\nAfter reset, total count: {h.total_count}")

view raw JSON →