Skip to content

Commit 38ab68c

Browse files
committed
Add pstats.SampledStats to display sample results
This variant overrides how column headers are printed to avoid conflating call counts with sample counts.
1 parent 6bb49a4 commit 38ab68c

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

Lib/profile/sample.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def sample(self, duration_sec=10):
5959
def print_stats(self, sort=-1):
6060
if not isinstance(sort, tuple):
6161
sort = (sort,)
62-
pstats.Stats(self).strip_dirs().sort_stats(*sort).print_stats()
62+
pstats.SampledStats(self).strip_dirs().sort_stats(*sort).print_stats()
6363

6464
def dump_stats(self, file):
6565
with open(file, "wb") as f:

Lib/pstats.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,10 @@ def print_call_heading(self, name_size, column_title):
467467
subheader = isinstance(value, tuple)
468468
break
469469
if subheader:
470-
print(" "*name_size + " ncalls tottime cumtime", file=self.stream)
470+
self.print_call_subheading(name_size)
471+
472+
def print_call_subheading(self, name_size):
473+
print(" "*name_size + " ncalls tottime cumtime", file=self.stream)
471474

472475
def print_call_line(self, name_size, source, call_dict, arrow="->"):
473476
print(func_std_string(source).ljust(name_size) + arrow, end=' ', file=self.stream)
@@ -516,6 +519,35 @@ def print_line(self, func): # hack: should print percentages
516519
print(f8(ct/cc), end=' ', file=self.stream)
517520
print(func_std_string(func), file=self.stream)
518521

522+
523+
class SampledStats(Stats):
524+
def __init__(self, *args, stream=None):
525+
super().__init__(*args, stream=stream)
526+
527+
self.sort_arg_dict = {
528+
"samples" : (((1,-1), ), "sample count"),
529+
"nsamples" : (((1,-1), ), "sample count"),
530+
"cumtime" : (((3,-1), ), "cumulative time"),
531+
"cumulative": (((3,-1), ), "cumulative time"),
532+
"filename" : (((4, 1), ), "file name"),
533+
"line" : (((5, 1), ), "line number"),
534+
"module" : (((4, 1), ), "file name"),
535+
"name" : (((6, 1), ), "function name"),
536+
"nfl" : (((6, 1),(4, 1),(5, 1),), "name/file/line"),
537+
"psamples" : (((0,-1), ), "primitive call count"),
538+
"stdname" : (((7, 1), ), "standard name"),
539+
"time" : (((2,-1), ), "internal time"),
540+
"tottime" : (((2,-1), ), "internal time"),
541+
}
542+
543+
def print_call_subheading(self, name_size):
544+
print(" "*name_size + " nsamples tottime cumtime", file=self.stream)
545+
546+
def print_title(self):
547+
print(' nsamples tottime persample cumtime persample', end=' ', file=self.stream)
548+
print('filename:lineno(function)', file=self.stream)
549+
550+
519551
class TupleComp:
520552
"""This class provides a generic function for comparing any two tuples.
521553
Each instance records a list of tuple-indices (from most significant

0 commit comments

Comments
 (0)