mirror of https://github.com/python/cpython
bpo-21862: Add -m option to cProfile for profiling modules (#4297)
* bpo-21862: Add -m option to cProfile for profiling modules
This commit is contained in:
parent
4fc4defd1c
commit
7973e279a2
|
@ -123,13 +123,18 @@ them in various ways.
|
||||||
The file :mod:`cProfile` can also be invoked as a script to profile another
|
The file :mod:`cProfile` can also be invoked as a script to profile another
|
||||||
script. For example::
|
script. For example::
|
||||||
|
|
||||||
python -m cProfile [-o output_file] [-s sort_order] myscript.py
|
python -m cProfile [-o output_file] [-s sort_order] (-m module | myscript.py)
|
||||||
|
|
||||||
``-o`` writes the profile results to a file instead of to stdout
|
``-o`` writes the profile results to a file instead of to stdout
|
||||||
|
|
||||||
``-s`` specifies one of the :func:`~pstats.Stats.sort_stats` sort values to sort
|
``-s`` specifies one of the :func:`~pstats.Stats.sort_stats` sort values to sort
|
||||||
the output by. This only applies when ``-o`` is not supplied.
|
the output by. This only applies when ``-o`` is not supplied.
|
||||||
|
|
||||||
|
``-m`` specifies that a module is being profiled instead of a script.
|
||||||
|
|
||||||
|
.. versionadded:: 3.7
|
||||||
|
Added the ``-m`` option.
|
||||||
|
|
||||||
The :mod:`pstats` module's :class:`~pstats.Stats` class has a variety of methods
|
The :mod:`pstats` module's :class:`~pstats.Stats` class has a variety of methods
|
||||||
for manipulating and printing the data saved into a profile results file::
|
for manipulating and printing the data saved into a profile results file::
|
||||||
|
|
||||||
|
|
|
@ -255,6 +255,12 @@ contextlib
|
||||||
:func:`contextlib.asynccontextmanager` has been added. (Contributed by
|
:func:`contextlib.asynccontextmanager` has been added. (Contributed by
|
||||||
Jelle Zijlstra in :issue:`29679`.)
|
Jelle Zijlstra in :issue:`29679`.)
|
||||||
|
|
||||||
|
cProfile
|
||||||
|
--------
|
||||||
|
|
||||||
|
cProfile command line now accepts `-m module_name` as an alternative to
|
||||||
|
script path. (Contributed by Sanyam Khurana in :issue:`21862`.)
|
||||||
|
|
||||||
crypt
|
crypt
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
|
|
@ -121,9 +121,11 @@ def label(code):
|
||||||
# ____________________________________________________________
|
# ____________________________________________________________
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
import os, sys
|
import os
|
||||||
|
import sys
|
||||||
|
import runpy
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
usage = "cProfile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
|
usage = "cProfile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ..."
|
||||||
parser = OptionParser(usage=usage)
|
parser = OptionParser(usage=usage)
|
||||||
parser.allow_interspersed_args = False
|
parser.allow_interspersed_args = False
|
||||||
parser.add_option('-o', '--outfile', dest="outfile",
|
parser.add_option('-o', '--outfile', dest="outfile",
|
||||||
|
@ -131,6 +133,8 @@ def main():
|
||||||
parser.add_option('-s', '--sort', dest="sort",
|
parser.add_option('-s', '--sort', dest="sort",
|
||||||
help="Sort order when printing to stdout, based on pstats.Stats class",
|
help="Sort order when printing to stdout, based on pstats.Stats class",
|
||||||
default=-1)
|
default=-1)
|
||||||
|
parser.add_option('-m', dest="module", action="store_true",
|
||||||
|
help="Profile a library module", default=False)
|
||||||
|
|
||||||
if not sys.argv[1:]:
|
if not sys.argv[1:]:
|
||||||
parser.print_usage()
|
parser.print_usage()
|
||||||
|
@ -140,6 +144,13 @@ def main():
|
||||||
sys.argv[:] = args
|
sys.argv[:] = args
|
||||||
|
|
||||||
if len(args) > 0:
|
if len(args) > 0:
|
||||||
|
if options.module:
|
||||||
|
code = "run_module(modname, run_name='__main__')"
|
||||||
|
globs = {
|
||||||
|
'run_module': runpy.run_module,
|
||||||
|
'modname': args[0]
|
||||||
|
}
|
||||||
|
else:
|
||||||
progname = args[0]
|
progname = args[0]
|
||||||
sys.path.insert(0, os.path.dirname(progname))
|
sys.path.insert(0, os.path.dirname(progname))
|
||||||
with open(progname, 'rb') as fp:
|
with open(progname, 'rb') as fp:
|
||||||
|
|
|
@ -6,6 +6,7 @@ from test.support import run_unittest, TESTFN, unlink
|
||||||
# rip off all interesting stuff from test_profile
|
# rip off all interesting stuff from test_profile
|
||||||
import cProfile
|
import cProfile
|
||||||
from test.test_profile import ProfileTest, regenerate_expected_output
|
from test.test_profile import ProfileTest, regenerate_expected_output
|
||||||
|
from test.support.script_helper import assert_python_failure, assert_python_ok
|
||||||
|
|
||||||
|
|
||||||
class CProfileTest(ProfileTest):
|
class CProfileTest(ProfileTest):
|
||||||
|
@ -35,6 +36,19 @@ class CProfileTest(ProfileTest):
|
||||||
finally:
|
finally:
|
||||||
unlink(TESTFN)
|
unlink(TESTFN)
|
||||||
|
|
||||||
|
# Issue 21862
|
||||||
|
def test_module_path_option(self):
|
||||||
|
# Test -m switch with modules
|
||||||
|
|
||||||
|
# Test that -m switch needs an argument
|
||||||
|
assert_python_failure('-m', 'cProfile', '-m')
|
||||||
|
|
||||||
|
# Test failure for not-existent module
|
||||||
|
assert_python_failure('-m', 'cProfile', '-m', 'random_module_xyz')
|
||||||
|
|
||||||
|
# Test successful run
|
||||||
|
assert_python_ok('-m', 'cProfile', '-m', 'timeit', '-n', '1')
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
run_unittest(CProfileTest)
|
run_unittest(CProfileTest)
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
cProfile command line now accepts `-m module_name` as an alternative to
|
||||||
|
script path. Patch by Sanyam Khurana.
|
Loading…
Reference in New Issue