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 Attributes:
11 profiler_path (str) path where to save the profiling results.
12
13 """
14
15 def __init__(self, problem: Problem, algorithm: Algorithm, backend: Backend, profiler_path: str = 'profiling-results.prof'):
16 super().__init__(problem, algorithm, backend)
17 self._profiler_path = profiler_path
18
[docs]
19 def run(self) -> Result:
20 with cProfile.Profile() as pr:
21 result = super().run()
22 stats = pstats.Stats(pr)
23 stats.sort_stats(pstats.SortKey.TIME)
24 stats.reverse_order()
25 stats.print_stats()
26 stats.dump_stats(self._profiler_path)
27 return result