pstats_extender
1import cProfile 2import os 3import pstats 4from contextlib import contextmanager 5from pstats import SortKey 6from time import time 7 8 9__all__ = ["profile", "SortKey"] 10 11 12@contextmanager 13def profile(sortby: SortKey = SortKey.CUMULATIVE, directory="../pstats"): 14 """ 15 sortby: SortKey.CUMULATIVE | SortKey.PCALLS | etc. 16 """ 17 if not os.path.exists(directory): 18 os.makedirs(directory) 19 try: 20 pr = cProfile.Profile() 21 pr.enable() 22 yield 23 finally: 24 pr.disable() 25 with open(os.path.join(directory, "%s-%d.py") % (__name__, int(time())), "w") as s: 26 ps = pstats.Stats(pr, stream=s).sort_stats(sortby) 27 ps.print_stats()
@contextmanager
def
profile( sortby: pstats.SortKey = <SortKey.CUMULATIVE: 'cumulative'>, directory='../pstats'):
13@contextmanager 14def profile(sortby: SortKey = SortKey.CUMULATIVE, directory="../pstats"): 15 """ 16 sortby: SortKey.CUMULATIVE | SortKey.PCALLS | etc. 17 """ 18 if not os.path.exists(directory): 19 os.makedirs(directory) 20 try: 21 pr = cProfile.Profile() 22 pr.enable() 23 yield 24 finally: 25 pr.disable() 26 with open(os.path.join(directory, "%s-%d.py") % (__name__, int(time())), "w") as s: 27 ps = pstats.Stats(pr, stream=s).sort_stats(sortby) 28 ps.print_stats()
sortby: SortKey.CUMULATIVE | SortKey.PCALLS | etc.
class
SortKey(enum.StrEnum):
37@_simple_enum(StrEnum) 38class SortKey: 39 CALLS = 'calls', 'ncalls' 40 CUMULATIVE = 'cumulative', 'cumtime' 41 FILENAME = 'filename', 'module' 42 LINE = 'line' 43 NAME = 'name' 44 NFL = 'nfl' 45 PCALLS = 'pcalls' 46 STDNAME = 'stdname' 47 TIME = 'time', 'tottime' 48 49 def __new__(cls, *values): 50 value = values[0] 51 obj = str.__new__(cls, value) 52 obj._value_ = value 53 for other_value in values[1:]: 54 cls._value2member_map_[other_value] = obj 55 obj._all_values = values 56 return obj
An enumeration.
CALLS =
<SortKey.CALLS: 'calls'>
CUMULATIVE =
<SortKey.CUMULATIVE: 'cumulative'>
FILENAME =
<SortKey.FILENAME: 'filename'>
LINE =
<SortKey.LINE: 'line'>
NAME =
<SortKey.NAME: 'name'>
NFL =
<SortKey.NFL: 'nfl'>
PCALLS =
<SortKey.PCALLS: 'pcalls'>
STDNAME =
<SortKey.STDNAME: 'stdname'>
TIME =
<SortKey.TIME: 'time'>