Source code for qlauncher.launcher.profiling_launcher

 1import cProfile
 2import pstats
 3
 4from qlauncher.base import Algorithm, Backend, Problem, Result
 5from qlauncher.launcher.qlauncher import QLauncher
 6
 7
[docs] 8class ProfilingLauncher(QLauncher): 9 """Launcher made for debugging purposes of algorithms and other launchers focusing on performance issues 10 11 Attributes: 12 profiler_path (str) path where to save the profiling results. 13 14 """ 15 16 def __init__(self, problem: Problem, algorithm: Algorithm, backend: Backend, profiler_path: str = 'profiling-results.prof'): 17 super().__init__(problem, algorithm, backend) 18 self._profiler_path = profiler_path 19
[docs] 20 def run(self) -> Result: 21 with cProfile.Profile() as pr: 22 result = super().run() 23 stats = pstats.Stats(pr) 24 stats.sort_stats(pstats.SortKey.TIME) 25 stats.reverse_order() 26 stats.print_stats() 27 stats.dump_stats(self._profiler_path) 28 return result