Hey everyone,
I've been using cProfile to profile some of my code, but found out that while
the tools to print the results are very convenient, it was difficult to extract
and send to a logging tool (i.e. fluentd) where it can be further parsed and
analyzed. I'm thinking of adding a function to cProfile
(https://github.com/python/cpython/blob/master/Lib/cProfile.py) similar to
"get_profile_dict" below, but wanted to get some feedback from the community
before I submit a proper PR for it.
The code below is just a sample of how I used the proposed function to extract
the analysis of a Fibonacci call.
Thanks!
```
import cProfile, pstats
import pprint
from pstats import func_std_string, f8
def fib(n):
if n == 0:
return 0
if n == 1:
return 1
return fib(n-1) + fib(n-2)
pr = cProfile.Profile()
pr.enable()
fib(5)
pr.create_stats()
ps = pstats.Stats(pr).sort_stats('tottime', 'cumtime')
def get_profile_dict(ps):
profile_dict = {}
width, list = ps.get_print_list([])
if list:
for func in list:
cc, nc, tt, ct, callers = ps.stats[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_name = func_std_string(func)
profile_dict[func_name] = {
"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
}
profile_dict["total_tt"] = float(f8(ps.total_tt))
return profile_dict
pp = pprint.PrettyPrinter(depth=6)
pp.pprint(get_profile_dict(ps))
```
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/LNX3K3QEEWBXGZ2IVLXASAL5YW6VZ5UN/
Code of Conduct: http://python.org/psf/codeofconduct/