diff options
Diffstat (limited to 'Lib/pstats.py')
-rw-r--r-- | Lib/pstats.py | 57 |
1 files changed, 55 insertions, 2 deletions
diff --git a/Lib/pstats.py b/Lib/pstats.py index 4b419a8..e781b91 100644 --- a/Lib/pstats.py +++ b/Lib/pstats.py @@ -25,11 +25,13 @@ import os import time import marshal import re + from enum import Enum from functools import cmp_to_key +from dataclasses import dataclass +from typing import Dict -__all__ = ["Stats", "SortKey"] - +__all__ = ["Stats", "SortKey", "FunctionProfile", "StatsProfile"] class SortKey(str, Enum): CALLS = 'calls', 'ncalls' @@ -52,6 +54,22 @@ class SortKey(str, Enum): return obj +@dataclass(unsafe_hash=True) +class FunctionProfile: + ncalls: int + tottime: float + percall_tottime: float + cumtime: float + percall_cumtime: float + file_name: str + line_number: int + +@dataclass(unsafe_hash=True) +class StatsProfile: + '''Class for keeping track of an item in inventory.''' + total_tt: float + func_profiles: Dict[str, FunctionProfile] + class Stats: """This class is used for creating reports from data generated by the Profile class. It is a "friend" of that class, and imports data either @@ -333,6 +351,41 @@ class Stats: return new_list, msg + def get_stats_profile(self): + """This method returns an instance of StatsProfile, which contains a mapping + of function names to instances of FunctionProfile. Each FunctionProfile + instance holds information related to the function's profile such as how + long the function took to run, how many times it was called, etc... + """ + func_list = self.fcn_list[:] if self.fcn_list else list(self.stats.keys()) + if not func_list: + return StatsProfile(0, {}) + + total_tt = float(f8(self.total_tt)) + func_profiles = {} + stats_profile = StatsProfile(total_tt, func_profiles) + + for func in func_list: + cc, nc, tt, ct, callers = self.stats[func] + file_name, line_number, 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_profile = FunctionProfile( + ncalls, + tottime, # time spent in this function alone + percall_tottime, + cumtime, # time spent in the function plus all functions that this function called, + percall_cumtime, + file_name, + line_number + ) + func_profiles[func_name] = func_profile + + return stats_profile + def get_print_list(self, sel_list): width = self.max_name_len if self.fcn_list: |