Skip to content

Commit 1fcfffc

Browse files
committed
gh-138122: Refactor the CLI of profile.sampling into subcommands
1 parent 77cb39e commit 1fcfffc

File tree

13 files changed

+1341
-1413
lines changed

13 files changed

+1341
-1413
lines changed

Doc/library/profile.rst

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -347,81 +347,6 @@ The statistical profiler produces output similar to deterministic profilers but
347347

348348
.. _profile-cli:
349349

350-
:mod:`!profiling.sampling` Module Reference
351-
=======================================================
352-
353-
.. module:: profiling.sampling
354-
:synopsis: Python statistical profiler.
355-
356-
This section documents the programmatic interface for the :mod:`!profiling.sampling` module.
357-
For command-line usage, see :ref:`sampling-profiler-cli`. For conceptual information
358-
about statistical profiling, see :ref:`statistical-profiling`
359-
360-
.. function:: sample(pid, *, sort=2, sample_interval_usec=100, duration_sec=10, filename=None, all_threads=False, limit=None, show_summary=True, output_format="pstats", realtime_stats=False, native=False, gc=True)
361-
362-
Sample a Python process and generate profiling data.
363-
364-
This is the main entry point for statistical profiling. It creates a
365-
:class:`SampleProfiler`, collects stack traces from the target process, and
366-
outputs the results in the specified format.
367-
368-
:param int pid: Process ID of the target Python process
369-
:param int sort: Sort order for pstats output (default: 2 for cumulative time)
370-
:param int sample_interval_usec: Sampling interval in microseconds (default: 100)
371-
:param int duration_sec: Duration to sample in seconds (default: 10)
372-
:param str filename: Output filename (None for stdout/default naming)
373-
:param bool all_threads: Whether to sample all threads (default: False)
374-
:param int limit: Maximum number of functions to display (default: None)
375-
:param bool show_summary: Whether to show summary statistics (default: True)
376-
:param str output_format: Output format - 'pstats' or 'collapsed' (default: 'pstats')
377-
:param bool realtime_stats: Whether to display real-time statistics (default: False)
378-
:param bool native: Whether to include ``<native>`` frames (default: False)
379-
:param bool gc: Whether to include ``<GC>`` frames (default: True)
380-
381-
:raises ValueError: If output_format is not 'pstats' or 'collapsed'
382-
383-
Examples::
384-
385-
# Basic usage - profile process 1234 for 10 seconds
386-
import profiling.sampling
387-
profiling.sampling.sample(1234)
388-
389-
# Profile with custom settings
390-
profiling.sampling.sample(1234, duration_sec=30, sample_interval_usec=50, all_threads=True)
391-
392-
# Generate collapsed stack traces for flamegraph.pl
393-
profiling.sampling.sample(1234, output_format='collapsed', filename='profile.collapsed')
394-
395-
.. class:: SampleProfiler(pid, sample_interval_usec, all_threads)
396-
397-
Low-level API for the statistical profiler.
398-
399-
This profiler uses periodic stack sampling to collect performance data
400-
from running Python processes with minimal overhead. It can attach to
401-
any Python process by PID and collect stack traces at regular intervals.
402-
403-
:param int pid: Process ID of the target Python process
404-
:param int sample_interval_usec: Sampling interval in microseconds
405-
:param bool all_threads: Whether to sample all threads or just the main thread
406-
407-
.. method:: sample(collector, duration_sec=10)
408-
409-
Sample the target process for the specified duration.
410-
411-
Collects stack traces from the target process at regular intervals
412-
and passes them to the provided collector for processing.
413-
414-
:param collector: Object that implements ``collect()`` method to process stack traces
415-
:param int duration_sec: Duration to sample in seconds (default: 10)
416-
417-
The method tracks sampling statistics and can display real-time
418-
information if realtime_stats is enabled.
419-
420-
.. seealso::
421-
422-
:ref:`sampling-profiler-cli`
423-
Command-line interface documentation for the statistical profiler.
424-
425350
Deterministic Profiler Command Line Interface
426351
=============================================
427352

Lib/profiling/sampling/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
system restrictions or missing privileges.
4646
"""
4747

48-
from .sample import main
48+
from .cli import main
4949

5050
def handle_permission_error():
5151
"""Handle PermissionError by displaying appropriate error message."""

0 commit comments

Comments
 (0)