@@ -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
@@ -967,6 +997,57 @@ intuitive view that shows exactly where time is spent without requiring
967997interpretation of hierarchical visualizations.
968998
969999
1000+ Binary format
1001+ -------------
1002+
1003+ Binary format (:option: `--binary `) produces a compact binary file for efficient
1004+ storage of profiling data::
1005+
1006+ python -m profiling.sampling run --binary -o profile.bin script.py
1007+ python -m profiling.sampling attach --binary -o profile.bin 12345
1008+
1009+ The :option: `--compression ` option controls data compression:
1010+
1011+ - ``auto `` (default): Use zstd compression if available, otherwise no
1012+ compression
1013+ - ``zstd ``: Force zstd compression (requires zstd support)
1014+ - ``none ``: Disable compression
1015+
1016+ ::
1017+
1018+ python -m profiling.sampling run --binary --compression=zstd -o profile.bin script.py
1019+
1020+ To analyze binary profiles, use the :ref: `replay-command ` to convert them to
1021+ other formats like flame graphs or pstats output.
1022+
1023+
1024+ Record and replay workflow
1025+ ==========================
1026+
1027+ The binary format combined with the replay command enables a record-and-replay
1028+ workflow that separates data capture from analysis. Rather than generating
1029+ visualizations during profiling, you capture raw data to a compact binary file
1030+ and convert it to different formats later.
1031+
1032+ This approach has three main benefits. First, sampling runs faster because the
1033+ work of building data structures for visualization is deferred until replay.
1034+ Second, a single binary capture can be converted to multiple output formats
1035+ without re-profiling---pstats for a quick overview, flame graph for visual
1036+ exploration, heatmap for line-level detail. Third, binary files are compact
1037+ and easy to share with colleagues who can convert them to their preferred
1038+ format.
1039+
1040+ A typical workflow::
1041+
1042+ # Capture profile in production or during tests
1043+ python -m profiling.sampling attach --binary -o profile.bin 12345
1044+
1045+ # Later, analyze with different formats
1046+ python -m profiling.sampling replay profile.bin
1047+ python -m profiling.sampling replay --flamegraph -o profile.html profile.bin
1048+ python -m profiling.sampling replay --heatmap -o heatmap profile.bin
1049+
1050+
9701051Live mode
9711052=========
9721053
@@ -1178,6 +1259,10 @@ Global options
11781259
11791260 Attach to and profile a running process by PID.
11801261
1262+ .. option :: replay
1263+
1264+ Convert a binary profile file to another output format.
1265+
11811266
11821267Sampling options
11831268----------------
@@ -1256,12 +1341,22 @@ Output options
12561341
12571342 Generate HTML heatmap with line-level sample counts.
12581343
1344+ .. option :: --binary
1345+
1346+ Generate high-performance binary format for later conversion with the
1347+ ``replay `` command.
1348+
1349+ .. option :: --compression <type >
1350+
1351+ Compression for binary format: ``auto `` (use zstd if available, default),
1352+ ``zstd ``, or ``none ``.
1353+
12591354.. option :: -o <path >, --output <path >
12601355
12611356 Output file or directory path. Default behavior varies by format:
1262- ``--pstats `` writes to stdout, ``--flamegraph `` and ``--gecko `` generate
1263- files like ``flamegraph.PID.html ``, and ``--heatmap `` creates a directory
1264- named ``heatmap_PID ``.
1357+ ``--pstats `` writes to stdout, ``--flamegraph ``, ``--gecko ``, and
1358+ `` --binary `` generate files like ``flamegraph.PID.html ``, and ``--heatmap ``
1359+ creates a directory named ``heatmap_PID ``.
12651360
12661361
12671362pstats display options
0 commit comments