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