Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-37958: Adding get_profile_dict to pstats #15495

Merged
merged 9 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Lib/pstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,45 @@ def eval_print_amount(self, sel, list, msg):

return new_list, msg

def get_profile_dict(self, keys_filter=None):
"""
Returns a dict where the key is a function name and the value is a
dict with the following keys:
ncalls, tottime, percall_tottime, cumtime, percall_cumtime,
file_name, line_number

:param list keys_filter: Optional parameterto limit the keys returned
in the retrieved dict
"""
pstats_dict = {}
func_list = self.fcn_list[:] if self.fcn_list else list(self.stats.keys())

if not func_list:
return pstats_dict

pstats_dict["total_tt"] = float(f8(self.total_tt))
for func in func_list:
cc, nc, tt, ct, callers = self.stats[func]
file, line, func_name = func
ncalls = str(nc) if nc == cc else (str(nc) + '/' + str(cc))
tottime = float(f8(tt))
percall_tottime = -1 if nc == 0 else float(f8(tt/nc))
cumtime = float(f8(ct))
percall_cumtime = -1 if cc == 0 else float(f8(ct/cc))
func_dict = {
"ncalls": ncalls,
"tottime": tottime, # time spent in this function alone
"percall_tottime": percall_tottime,
"cumtime": cumtime, # time spent in the function plus all functions that this function called,
"percall_cumtime": percall_cumtime,
"file_name": file,
"line_number": line
}
func_dict_filtered = func_dict if not keys_filter else { key: func_dict[key] for key in keys_filter }
pstats_dict[func_name] = func_dict_filtered

return pstats_dict

def get_print_list(self, sel_list):
width = self.max_name_len
if self.fcn_list:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pstats is really useful or profiling and printing the output of the execution of some block of code, but I've found on multiple occasions when I'd like to access this output directly in an easily usable dictionary on which I can further analyze or manipulate.

The proposal is to add a function called get_profile_dict inside of pstats that'll automatically return this data the data in an easily accessible dict.

The output of get_profile_dict is a dict pointing from function names to a dict with the following keys: ncalls, tottime, percall_tottime, cumtime, percall_cumtime, file_name, line_number.

A specific use case of this is to generate a stacked column chart using various visualization tools which will assist in easily identifying program bottlenecks.