mirror of
https://github.com/python/cpython
synced 2024-10-14 10:33:27 +00:00
gh-69990: Make Profile.print_stats support sorting by multiple values (GH-104590)
Co-authored-by: Chiu-Hsiang Hsu
This commit is contained in:
parent
351c103134
commit
2a7a0020c9
|
@ -299,6 +299,13 @@ functions:
|
||||||
Create a :class:`~pstats.Stats` object based on the current
|
Create a :class:`~pstats.Stats` object based on the current
|
||||||
profile and print the results to stdout.
|
profile and print the results to stdout.
|
||||||
|
|
||||||
|
The *sort* parameter specifies the sorting order of the displayed
|
||||||
|
statistics. It accepts a single key or a tuple of keys to enable
|
||||||
|
multi-level sorting, as in :func:`Stats.sort_stats <pstats.Stats.sort_stats>`.
|
||||||
|
|
||||||
|
.. versionadded:: 3.13
|
||||||
|
:meth:`~Profile.print_stats` now accepts a tuple of keys.
|
||||||
|
|
||||||
.. method:: dump_stats(filename)
|
.. method:: dump_stats(filename)
|
||||||
|
|
||||||
Write the results of the current profile to *filename*.
|
Write the results of the current profile to *filename*.
|
||||||
|
|
|
@ -41,7 +41,9 @@ class Profile(_lsprof.Profiler):
|
||||||
|
|
||||||
def print_stats(self, sort=-1):
|
def print_stats(self, sort=-1):
|
||||||
import pstats
|
import pstats
|
||||||
pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats()
|
if not isinstance(sort, tuple):
|
||||||
|
sort = (sort,)
|
||||||
|
pstats.Stats(self).strip_dirs().sort_stats(*sort).print_stats()
|
||||||
|
|
||||||
def dump_stats(self, file):
|
def dump_stats(self, file):
|
||||||
import marshal
|
import marshal
|
||||||
|
|
|
@ -387,8 +387,9 @@ def simulate_cmd_complete(self):
|
||||||
|
|
||||||
def print_stats(self, sort=-1):
|
def print_stats(self, sort=-1):
|
||||||
import pstats
|
import pstats
|
||||||
pstats.Stats(self).strip_dirs().sort_stats(sort). \
|
if not isinstance(sort, tuple):
|
||||||
print_stats()
|
sort = (sort,)
|
||||||
|
pstats.Stats(self).strip_dirs().sort_stats(*sort).print_stats()
|
||||||
|
|
||||||
def dump_stats(self, file):
|
def dump_stats(self, file):
|
||||||
with open(file, 'wb') as f:
|
with open(file, 'wb') as f:
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
from difflib import unified_diff
|
from difflib import unified_diff
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
from test.support.os_helper import TESTFN, unlink, temp_dir, change_cwd
|
from test.support.os_helper import TESTFN, unlink, temp_dir, change_cwd
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager, redirect_stdout
|
||||||
|
|
||||||
import profile
|
import profile
|
||||||
from test.profilee import testfunc, timer
|
from test.profilee import testfunc, timer
|
||||||
|
@ -92,6 +92,11 @@ def test_run(self):
|
||||||
self.profilermodule.run("int('1')", filename=TESTFN)
|
self.profilermodule.run("int('1')", filename=TESTFN)
|
||||||
self.assertTrue(os.path.exists(TESTFN))
|
self.assertTrue(os.path.exists(TESTFN))
|
||||||
|
|
||||||
|
def test_run_with_sort_by_values(self):
|
||||||
|
with redirect_stdout(StringIO()) as f:
|
||||||
|
self.profilermodule.run("int('1')", sort=('tottime', 'stdname'))
|
||||||
|
self.assertIn("Ordered by: internal time, standard name", f.getvalue())
|
||||||
|
|
||||||
def test_runctx(self):
|
def test_runctx(self):
|
||||||
with silent():
|
with silent():
|
||||||
self.profilermodule.runctx("testfunc()", globals(), locals())
|
self.profilermodule.runctx("testfunc()", globals(), locals())
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
:meth:`Profile.print_stats` has been improved to accept multiple sort arguments. Patched by Chiu-Hsiang Hsu and Furkan Onder.
|
Loading…
Reference in a new issue