Skip to content

Conversation

@dcw-data
Copy link

@dcw-data dcw-data commented Dec 4, 2025

pgbench: Add --percentile option for latency percentile reporting

Add support for computing and reporting latency percentiles (p50, p90, p99,
p99.9, p99.99) in pgbench using a memory-efficient histogram approach.

The existing stddev calculation uses a streaming algorithm that maintains
only sum and sum-of-squares, which cannot compute percentiles. This commit
adds an optional log-linear histogram that tracks the distribution of
latency values with bounded memory.

Histogram Design:

  • Fine buckets: 1000 buckets covering 0-100ms at 100μs resolution
  • Coarse buckets: 9900 buckets covering 100ms-10s at 1ms resolution
  • Total memory: ~88KB per histogram
  • Overflow handling: Latencies >10s tracked separately

Memory Efficiency:

  • Histograms are only allocated when --percentile flag is used
  • Each StatsData contains a LatencyHistogram pointer (NULL when disabled)
  • Typical overhead: 0KB without flag, ~350KB for 4-thread run with flag

Features:

  • Global percentile output for all transactions
  • Per-script percentile output when using multiple workloads
  • Thread-safe histogram merging for multi-threaded runs
  • Works with all existing pgbench features (--rate, --progress, etc.)

Usage:
pgbench --percentile -t 1000 -c 10 dbname

Output includes:
latency percentile 50 = X.XXX ms
latency percentile 90 = X.XXX ms
latency percentile 99 = X.XXX ms
latency percentile 99.9 = X.XXX ms
latency percentile 99.99 = X.XXX ms

TAP tests added to verify percentile output in single-threaded,
multi-threaded, and per-script scenarios.

This commit adds a new --percentile option to pgbench that reports
latency percentiles (p50, p90, p99, p99.9) when enabled.

The implementation uses a histogram-based approach with:
- Fine-grained buckets (0-100ms at 100μs resolution)
- Coarse buckets (100ms-10s at 1ms resolution)
- Bounded memory usage (~85KB per thread per script)

This provides accurate percentile calculations without storing
all individual latency measurements.

Changes:
- Added LatencyHistogram structure and related functions
- Added --percentile command-line option
- Updated help text and documentation
- Added comprehensive test coverage
@dcw-data dcw-data self-assigned this Dec 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants