cpython/Misc/NEWS.d
Daniel Olshansky 01602ae403 bpo-37958: Adding get_profile_dict to pstats (GH-15495)
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 the following script:

```
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(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

        keys_filter can be optionally set to limit the key-value pairs 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

pp = pprint.PrettyPrinter(depth=6)
pp.pprint(get_profile_dict(ps))
```

will produce:

```
{"<method 'disable' of '_lsprof.Profiler' objects>": {'cumtime': 0.0,
                                                      'file_name': '~',
                                                      'line_number': 0,
                                                      'ncalls': '1',
                                                      'percall_cumtime': 0.0,
                                                      'percall_tottime': 0.0,
                                                      'tottime': 0.0},
 'create_stats': {'cumtime': 0.0,
                  'file_name': '/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/cProfile.py',
                  'line_number': 50,
                  'ncalls': '1',
                  'percall_cumtime': 0.0,
                  'percall_tottime': 0.0,
                  'tottime': 0.0},
 'fib': {'cumtime': 0.0,
         'file_name': 'get_profile_dict.py',
         'line_number': 5,
         'ncalls': '15/1',
         'percall_cumtime': 0.0,
         'percall_tottime': 0.0,
         'tottime': 0.0},
 'total_tt': 0.0}
 ```

 As an example, this can be used to generate a stacked column chart using various visualization tools which will assist in easily identifying program bottlenecks.



https://bugs.python.org/issue37958



Automerge-Triggered-By: @gpshead
2020-01-15 14:51:54 -08:00
..
next bpo-37958: Adding get_profile_dict to pstats (GH-15495) 2020-01-15 14:51:54 -08:00
3.5.0.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.5.0a1.rst Fix typos mostly in comments, docs and test names (GH-15209) 2019-08-30 16:21:19 -04:00
3.5.0a2.rst bpo-32523: Simplifying news entries with multiple paragraphs. (GH-8154) 2019-05-09 21:52:32 +02:00
3.5.0a3.rst bpo-35110: Fix unintentional spaces around hyphens and dashes. (GH-10231) 2018-10-31 02:26:06 +02:00
3.5.0a4.rst bpo-35042: Use the :pep: role where a PEP is specified (#10036) 2018-10-26 15:58:26 -07:00
3.5.0b1.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.5.0b2.rst bpo-35042: Use the :pep: role where a PEP is specified (#10036) 2018-10-26 15:58:26 -07:00
3.5.0b3.rst bpo-35110: Fix unintentional spaces around hyphens and dashes. (GH-10231) 2018-10-31 02:26:06 +02:00
3.5.0b4.rst bpo-35110: Fix unintentional spaces around hyphens and dashes. (GH-10231) 2018-10-31 02:26:06 +02:00
3.5.0rc1.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.5.0rc2.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.5.0rc3.rst bpo-35042: Use the :pep: role where a PEP is specified (#10036) 2018-10-26 15:58:26 -07:00
3.5.0rc4.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.5.1.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.5.1rc1.rst bpo-35110: Fix unintentional spaces around hyphens and dashes. (GH-10231) 2018-10-31 02:26:06 +02:00
3.5.2.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.5.2rc1.rst Fix typos in docs, comments and test assert messages (#14872) 2019-07-21 16:12:33 -04:00
3.5.3.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.5.3rc1.rst bpo-32523: Simplifying news entries with multiple paragraphs. (GH-8154) 2019-05-09 21:52:32 +02:00
3.5.4.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.5.4rc1.rst Fix typos mostly in comments, docs and test names (GH-15209) 2019-08-30 16:21:19 -04:00
3.5.5.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.5.5rc1.rst bpo-32523: Simplifying news entries with multiple paragraphs. (GH-8154) 2019-05-09 21:52:32 +02:00
3.6.0.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.6.0a1.rst Fix typos in docs, comments and test assert messages (#14872) 2019-07-21 16:12:33 -04:00
3.6.0a2.rst bpo-35042: Use the :pep: role where a PEP is specified (#10036) 2018-10-26 15:58:26 -07:00
3.6.0a3.rst bpo-35042: Use the :pep: role where a PEP is specified (#10036) 2018-10-26 15:58:26 -07:00
3.6.0a4.rst bpo-35042: Use the :pep: role where a PEP is specified (#10036) 2018-10-26 15:58:26 -07:00
3.6.0b1.rst Fix typos mostly in comments, docs and test names (GH-15209) 2019-08-30 16:21:19 -04:00
3.6.0b2.rst bpo-32523: Simplifying news entries with multiple paragraphs. (GH-8154) 2019-05-09 21:52:32 +02:00
3.6.0b3.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.6.0b4.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.6.0rc1.rst Fix typos mostly in comments, docs and test names (GH-15209) 2019-08-30 16:21:19 -04:00
3.6.0rc2.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.6.1.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.6.1rc1.rst Fix typos mostly in comments, docs and test names (GH-15209) 2019-08-30 16:21:19 -04:00
3.6.2.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.6.2rc1.rst bpo-32523: Simplifying news entries with multiple paragraphs. (GH-8154) 2019-05-09 21:52:32 +02:00
3.6.2rc2.rst bpo-34906: Doc: Fix typos (2) (GH-9735) 2018-10-06 16:35:53 +02:00
3.6.3.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.6.3rc1.rst Fix typos in docs, comments and test assert messages (#14872) 2019-07-21 16:12:33 -04:00
3.6.4.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.6.4rc1.rst bpo-32523: Simplifying news entries with multiple paragraphs. (GH-8154) 2019-05-09 21:52:32 +02:00
3.6.5.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.6.5rc1.rst bpo-32523: Simplifying news entries with multiple paragraphs. (GH-8154) 2019-05-09 21:52:32 +02:00
3.6.6.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.6.6rc1.rst bpo-35042: Use the :pep: role where a PEP is specified (#10036) 2018-10-26 15:58:26 -07:00
3.7.0.rst Forward port 3.7.0 final changes 2018-06-27 18:45:50 -04:00
3.7.0a1.rst Fix typos mostly in comments, docs and test names (GH-15209) 2019-08-30 16:21:19 -04:00
3.7.0a2.rst bpo-32523: Simplifying news entries with multiple paragraphs. (GH-8154) 2019-05-09 21:52:32 +02:00
3.7.0a3.rst bpo-32523: Simplifying news entries with multiple paragraphs. (GH-8154) 2019-05-09 21:52:32 +02:00
3.7.0a4.rst bpo-32523: Simplifying news entries with multiple paragraphs. (GH-8154) 2019-05-09 21:52:32 +02:00
3.7.0b1.rst bpo-32523: Simplifying news entries with multiple paragraphs. (GH-8154) 2019-05-09 21:52:32 +02:00
3.7.0b2.rst bpo-32523: Simplifying news entries with multiple paragraphs. (GH-8154) 2019-05-09 21:52:32 +02:00
3.7.0b3.rst bpo-34906: Doc: Fix typos (2) (GH-9735) 2018-10-06 16:35:53 +02:00
3.7.0b4.rst bpo-32523: Simplifying news entries with multiple paragraphs. (GH-8154) 2019-05-09 21:52:32 +02:00
3.7.0b5.rst bpo-35042: Use the :pep: role where a PEP is specified (#10036) 2018-10-26 15:58:26 -07:00
3.7.0rc1.rst bpo-35042: Use the :pep: role where a PEP is specified (#10036) 2018-10-26 15:58:26 -07:00
3.8.0a1.rst Fix typos mostly in comments, docs and test names (GH-15209) 2019-08-30 16:21:19 -04:00
3.8.0a2.rst Minor edits to news entries (ported from 3.7) (GH-12293) 2019-03-12 12:21:22 -04:00
3.8.0a3.rst IDLE: changelog correction and addition (GH-15042) 2019-07-31 01:35:30 -04:00
3.8.0a4.rst Fix typos mostly in comments, docs and test names (GH-15209) 2019-08-30 16:21:19 -04:00
3.8.0b1.rst bpo-32972: Document IsolatedAsyncioTestCase of unittest module (GH-15878) 2019-09-11 04:02:14 -07:00
3.9.0a1.rst Minor fixes to the NEWS entries (GH-17556) 2019-12-10 16:09:58 +00:00
3.9.0a2.rst Fix link to bpo issue in Changelog (GH-17692) 2020-01-06 20:48:16 +05:30