buckethead.observability.profiling¶
Optional profiling hooks — I/O counters, memray, pyinstrument.
buckethead.observability.profiling
¶
Optional profiling hooks for BucketHead.
Three opt-in layers:
- I/O counters — in-house, zero external deps. Tracks bytes and op counts through BucketClient. Cheap enough to always enable in production. Answers the 'how many R2 Class-A ops am I generating' question that drives R2 cost.
- Memory — memray, lazy-imported when config.memory is True. Wraps each flush in a memray.Tracker; emits a .bin per cycle.
- CPU — pyinstrument, lazy-imported when config.cpu is True. Wraps each flush; emits an HTML flame graph per cycle.
Only I/O counters are implemented fully in v1 — the memray and pyinstrument hooks are ready for wiring but ship as no-ops unless the optional 'profiling' extra is installed (pip install buckethead[profiling]).
ProfilingConfig
¶
Bases: BaseModel
Which profiling layers to enable and where their output goes.
IOCounters
¶
Tracks bucket operation counts, byte totals, and wall-time totals.
Thread-safe for the patterns BucketClient uses (single writer per operation; no lock needed because Python's GIL + atomic dict operations cover us). If multiple threads share a client and collide on the same op name, the count is still correct.
Source code in src/buckethead/observability/profiling.py
write_summary
¶
write_summary(
config: ProfilingConfig,
counters: IOCounters,
*,
duration_s: float,
) -> Path
Write an io-summary JSON to the configured output directory.
Returns the path written. Creates the directory if it doesn't exist.
Source code in src/buckethead/observability/profiling.py
write_flush_record
¶
write_flush_record(
config: ProfilingConfig,
*,
flush_index: int,
duration_s: float,
bytes_written: int,
counters: IOCounters,
) -> Path
Append a per-flush record to flushes.jsonl in the output directory.
Each line is a self-contained JSON object so the stress report can reconstruct a time series without re-instrumenting anything.
Source code in src/buckethead/observability/profiling.py
memory_tracker
¶
memory_tracker(config: ProfilingConfig, flush_index: int)
Context manager that writes a memray snapshot for one flush.
Source code in src/buckethead/observability/profiling.py
cpu_profiler
¶
cpu_profiler(config: ProfilingConfig, flush_index: int)
Context manager that writes a pyinstrument HTML for one flush.