@@ -200,6 +200,36 @@ On most systems, attaching to another process requires appropriate permissions.
200200See :ref: `profiling-permissions ` for platform-specific requirements.
201201
202202
203+ .. _replay-command :
204+
205+ The ``replay `` command
206+ ----------------------
207+
208+ The ``replay `` command converts binary profile files to other output formats::
209+
210+ python -m profiling.sampling replay profile.bin
211+ python -m profiling.sampling replay --flamegraph -o profile.html profile.bin
212+
213+ This command is useful when you have captured profiling data in binary format
214+ and want to analyze it later or convert it to a visualization format. Binary
215+ profiles can be replayed multiple times to different formats without
216+ re-profiling.
217+
218+ ::
219+
220+ # Convert binary to pstats (default, prints to stdout)
221+ python -m profiling.sampling replay profile.bin
222+
223+ # Convert binary to flame graph
224+ python -m profiling.sampling replay --flamegraph -o output.html profile.bin
225+
226+ # Convert binary to gecko format for Firefox Profiler
227+ python -m profiling.sampling replay --gecko -o profile.json profile.bin
228+
229+ # Convert binary to heatmap
230+ python -m profiling.sampling replay --heatmap -o my_heatmap profile.bin
231+
232+
203233Profiling in production
204234-----------------------
205235
@@ -1087,6 +1117,59 @@ intuitive view that shows exactly where time is spent without requiring
10871117interpretation of hierarchical visualizations.
10881118
10891119
1120+ Binary format
1121+ -------------
1122+
1123+ Binary format (:option: `--binary `) produces a compact binary file for efficient
1124+ storage of profiling data::
1125+
1126+ python -m profiling.sampling run --binary -o profile.bin script.py
1127+ python -m profiling.sampling attach --binary -o profile.bin 12345
1128+
1129+ The :option: `--compression ` option controls data compression:
1130+
1131+ - ``auto `` (default): Use zstd compression if available, otherwise no
1132+ compression
1133+ - ``zstd ``: Force zstd compression (requires :mod: `compression.zstd ` support)
1134+ - ``none ``: Disable compression
1135+
1136+ ::
1137+
1138+ python -m profiling.sampling run --binary --compression=zstd -o profile.bin script.py
1139+
1140+ To analyze binary profiles, use the :ref: `replay-command ` to convert them to
1141+ other formats like flame graphs or pstats output.
1142+
1143+
1144+ Record and replay workflow
1145+ ==========================
1146+
1147+ The binary format combined with the replay command enables a record-and-replay
1148+ workflow that separates data capture from analysis. Rather than generating
1149+ visualizations during profiling, you capture raw data to a compact binary file
1150+ and convert it to different formats later.
1151+
1152+ This approach has three main benefits:
1153+
1154+ - Sampling runs faster because the work of building data structures for
1155+ visualization is deferred until replay.
1156+ - A single binary capture can be converted to multiple output formats
1157+ without re-profiling: pstats for a quick overview, flame graph for visual
1158+ exploration, heatmap for line-level detail.
1159+ - Binary files are compact and easy to share with colleagues who can convert
1160+ them to their preferred format.
1161+
1162+ A typical workflow::
1163+
1164+ # Capture profile in production or during tests
1165+ python -m profiling.sampling attach --binary -o profile.bin 12345
1166+
1167+ # Later, analyze with different formats
1168+ python -m profiling.sampling replay profile.bin
1169+ python -m profiling.sampling replay --flamegraph -o profile.html profile.bin
1170+ python -m profiling.sampling replay --heatmap -o heatmap profile.bin
1171+
1172+
10901173Live mode
10911174=========
10921175
@@ -1298,6 +1381,10 @@ Global options
12981381
12991382 Attach to and profile a running process by PID.
13001383
1384+ .. option :: replay
1385+
1386+ Convert a binary profile file to another output format.
1387+
13011388
13021389Sampling options
13031390----------------
@@ -1388,12 +1475,22 @@ Output options
13881475
13891476 Generate HTML heatmap with line-level sample counts.
13901477
1478+ .. option :: --binary
1479+
1480+ Generate high-performance binary format for later conversion with the
1481+ ``replay `` command.
1482+
1483+ .. option :: --compression <type >
1484+
1485+ Compression for binary format: ``auto `` (use zstd if available, default),
1486+ ``zstd ``, or ``none ``.
1487+
13911488.. option :: -o <path >, --output <path >
13921489
13931490 Output file or directory path. Default behavior varies by format:
1394- `` --pstats `` writes to stdout, `` --flamegraph `` and `` --gecko `` generate
1395- files like `` flamegraph. PID.html ``, and `` --heatmap `` creates a directory
1396- named ``heatmap_PID ``.
1491+ :option: ` --pstats ` writes to stdout, while other formats generate a file
1492+ named `` <format>_< PID>.<ext> `` (for example, `` flamegraph_12345.html ``).
1493+ :option: ` --heatmap ` creates a directory named ``heatmap_<PID> ``.
13971494
13981495
13991496pstats display options
0 commit comments