Skip to content

Commit 15af610

Browse files
author
Andras Mitzki
committed
CustomElementNumber: Add support to define element number for print_stats()
- It is possible that we need more stats than the default 20 Signed-off-by: Andras Mitzki <[email protected]>
1 parent 19c18c8 commit 15af610

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

pytest-profiling/pytest_profiling.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ class Profiling(object):
2727
profs = []
2828
combined = None
2929

30-
def __init__(self, svg, dir):
30+
def __init__(self, svg, dir, element_number):
3131
self.svg = svg
3232
self.dir = 'prof' if dir is None else dir[0]
33+
self.element_number = element_number
3334
self.profs = []
3435
self.gprof2dot = os.path.abspath(os.path.join(os.path.dirname(sys.executable), 'gprof2dot'))
3536
if not os.path.isfile(self.gprof2dot):
@@ -59,7 +60,7 @@ def pytest_sessionfinish(self, session, exitstatus): # @UnusedVariable
5960
def pytest_terminal_summary(self, terminalreporter):
6061
if self.combined:
6162
terminalreporter.write("Profiling (from {prof}):\n".format(prof=self.combined))
62-
pstats.Stats(self.combined, stream=terminalreporter).strip_dirs().sort_stats('cumulative').print_stats(20)
63+
pstats.Stats(self.combined, stream=terminalreporter).strip_dirs().sort_stats('cumulative').print_stats(int(self.element_number))
6364
if self.svg_name:
6465
terminalreporter.write("SVG profile in {svg}.\n".format(svg=self.svg_name))
6566

@@ -98,11 +99,14 @@ def pytest_addoption(parser):
9899
help="generate profiling graph (using gprof2dot and dot -Tsvg)")
99100
group.addoption("--pstats-dir", nargs=1,
100101
help="configure the dump directory of profile data files")
102+
group.addoption("--element-number", action="store", default="20",
103+
help="defines how many elements will display in a result")
101104

102105

103106
def pytest_configure(config):
104107
"""pytest_configure hook for profiling plugin"""
105108
profile_enable = any(config.getvalue(x) for x in ('profile', 'profile_svg'))
106109
if profile_enable:
107110
config.pluginmanager.register(Profiling(config.getvalue('profile_svg'),
108-
config.getvalue('pstats_dir')))
111+
config.getvalue('pstats_dir'),
112+
config.getvalue('element_number')))

0 commit comments

Comments
 (0)