class ProfilingPanel(Panel): """ Panel that displays profiling information. """ title = _("Profiling") template = "debug_toolbar/panels/profiling.html" def process_request(self, request): self.profiler = cProfile.Profile() return self.profiler.runcall(super().process_request, request) def add_node(self, func_list, func, max_depth, cum_time=0.1): func_list.append(func) func.has_subfuncs = False if func.depth < max_depth: for subfunc in func.subfuncs(): if subfunc.stats[3] >= cum_time: func.has_subfuncs = True self.add_node(func_list, subfunc, max_depth, cum_time=cum_time) def generate_stats(self, request, response): if not hasattr(self, "profiler"): return None # Could be delayed until the panel content is requested (perf. optim.) self.profiler.create_stats() self.stats = Stats(self.profiler) self.stats.calc_callees() root_func = cProfile.label(super().process_request.__code__) if root_func in self.stats.stats: root = FunctionCall(self.stats, root_func, depth=0) func_list = [] self.add_node( func_list, root, dt_settings.get_config()["PROFILER_MAX_DEPTH"], root.stats[3] / 8, ) self.record_stats({"func_list": func_list})
class Stat: """manage interface between pstat data and GUI item""" def __init__(self, pstat=None, path=None): # pStat profile statistics instance self.pstat = None self.load(pstat, path) self.itemArray = {} self.itemArray[TAB_FUNCTIONSTAT] = {} self.itemArray[TAB_SOURCE] = {} # Reference from pstat key to Qt object in the GUI self.pStatArray = {} self.pStatArray[TAB_FUNCTIONSTAT] = {} self.pStatArray[TAB_SOURCE] = {} def getTotalTime(self): if self.pstat: return self.pstat.total_tt else: return 0 def getCallNumber(self): if self.pstat: return self.pstat.total_calls else: return 0 def getPrimitiveCallRatio(self): if self.pstat: return 100.0 * float(self.pstat.prim_calls) / float( self.pstat.total_calls) else: return 0 def getStatNumber(self): if self.pstat: return len(self.pstat.stats) else: return 0 def getStatItems(self): if self.pstat: return list(self.pstat.stats.items()) def getStatKeys(self): if self.pstat: return list(self.pstat.stats.keys()) def getCalleesItems(self): if self.pstat: return list(self.pstat.all_callees.items()) def getStatTotalTime(self, pstatTriplet): if self.pstat: try: return self.pstat.stats[pstatTriplet][2] except KeyError: return 0 else: return 0 def getStatCumulativeTime(self, pstatTriplet): if self.pstat: try: return self.pstat.stats[pstatTriplet][3] except KeyError: return 0 else: return 0 def load(self, pstat=None, path=None): if pstat and path: print('''' Warning : both pstat and path parameter given. path override pstat !''') if pstat: self.pstat = pstat if path: self.pstat = Stats(str(path)) if self.pstat: self.pstat.calc_callees() def save(self, path): try: self.pstat.dump_stats(path) except: pass def setStatLink(self, guiItem, pstatTriplet, target): self.itemArray[target][guiItem] = pstatTriplet self.pStatArray[target][pstatTriplet] = guiItem def getPstatFromGui(self, guiItem, target): try: return self.itemArray[target][guiItem] except KeyError: return None def getGuiFromPstat(self, pStatTtriplet, target): try: return self.pStatArray[target][pStatTtriplet] except KeyError: return None def finish(self): ' clear when finish ' self.process.kill()
def read_runtime(path: Path) -> dict: profile = Stats(str(path)) profile.calc_callees() return profile.total_tt
from pathlib import Path from pstats import Stats import numpy as np from astropy import units as u from matplotlib import pyplot as plt from snat_sim.plasticc import PLAsTICC results_dir = Path(__file__).resolve().parent / 'lc_runtime_scaling' num_lc = [] runtime = [] for stat_file in results_dir.glob('*.pstat'): stats = Stats(str(stat_file)) stats.calc_callees() runtime.append(stats.total_tt) num_lc.append(int(stat_file.stem.split('_')[-1])) fit_params = np.polyfit(y=runtime, x=num_lc, deg=1) linear_fit = np.poly1d(fit_params) cadence_used = 'alt_sched' total_cadence_lc = PLAsTICC(cadence_used, 11).count_light_curves() total_cadence = total_cadence_lc * fit_params[0] * u.s fig, axis = plt.subplots() axis.scatter(num_lc, runtime) axis.plot(axis.get_xlim(), linear_fit(axis.get_xlim()),
class Stat: """manage interface between pstat data and GUI item""" def __init__(self, pstat=None, path=None): # pStat profile statistics instance self.pstat = None self.load(pstat, path) self.itemArray = {} self.itemArray[TAB_FUNCTIONSTAT] = {} self.itemArray[TAB_SOURCE] = {} # Reference from pstat key to Qt object in the GUI self.pStatArray = {} self.pStatArray[TAB_FUNCTIONSTAT] = {} self.pStatArray[TAB_SOURCE] = {} def getTotalTime(self): if self.pstat: return self.pstat.total_tt else: return 0 def getCallNumber(self): if self.pstat: return self.pstat.total_calls else: return 0 def getPrimitiveCallRatio(self): if self.pstat: return 100.0 * float(self.pstat.prim_calls) / float( self.pstat.total_calls) else: return 0 def getStatNumber(self): if self.pstat: return len(self.pstat.stats) else: return 0 def getStatItems(self): if self.pstat: return list(self.pstat.stats.items()) def getStatKeys(self): if self.pstat: return list(self.pstat.stats.keys()) def getCalleesItems(self): if self.pstat: return list(self.pstat.all_callees.items()) def getStatTotalTime(self, pstatTriplet): if self.pstat: try: return self.pstat.stats[pstatTriplet][2] except KeyError: return 0 else: return 0 def getStatCumulativeTime(self, pstatTriplet): if self.pstat: try: return self.pstat.stats[pstatTriplet][3] except KeyError: return 0 else: return 0 def load(self, pstat=None, path=None): if pstat and path: print('''' Warning : both pstat and path parameter given. path override pstat !''') if pstat: self.pstat = pstat if path: self.pstat = Stats(str(path)) if self.pstat: self.pstat.calc_callees() def save(self, path): try: self.pstat.dump_stats(path) except: pass def setStatLink(self, guiItem, pstatTriplet, target): self.itemArray[target][guiItem] = pstatTriplet self.pStatArray[target][pstatTriplet] = guiItem def getPstatFromGui(self, guiItem, target): try: return self.itemArray[target][guiItem] except KeyError: return None def getGuiFromPstat(self, pStatTtriplet, target): try: return self.pStatArray[target][pStatTtriplet] except KeyError: return None def finish(self): ' clear when finish ' self.process.kill()