From 844d6962e0d74b03a538eddf55c8fb48a8447406 Mon Sep 17 00:00:00 2001 From: Petr Stodulka Date: Fri, 5 Apr 2019 13:53:57 +0200 Subject: [PATCH] Enable profilling when env LEAPP_CPROFILE == '1' Currently on some machines the run of the leapp / snactor is too slow. Especially framework itself should be fast but sometimes the time differences are in order of tens seconds. From this point, it seems useful to be able to start profilling based on the env variable when it is needed. --- leapp/snactor/__init__.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/leapp/snactor/__init__.py b/leapp/snactor/__init__.py index db7a096fd..bd097b4d4 100644 --- a/leapp/snactor/__init__.py +++ b/leapp/snactor/__init__.py @@ -2,6 +2,17 @@ import pkgutil import socket +# for profilling +import cProfile +import pstats +try: + from StringIO import StringIO +except ImportError: + # TODO: low possibility of the problem with encoding with Python3; + # # but it should not be so problematic in this case, so keeping now just + # # like that to keep it simple + from io import StringIO + from leapp.utils.i18n import _ from leapp.snactor import commands from leapp.snactor.commands import workflow @@ -68,6 +79,17 @@ def cli(args): def main(): + profile_enabled = os.environ.get('LEAPP_CPROFILE', '0') == '1' + if profile_enabled: + pr = cProfile.Profile() + pr.enable() os.environ['LEAPP_HOSTNAME'] = socket.getfqdn() load_commands() cli.command.execute(version=_('snactor version {}').format(VERSION)) + if profile_enabled: + pr.disable() + s = StringIO.StringIO() + sortby = 'cumulative' + ps = pstats.Stats(pr, stream=s).sort_stats(sortby) + ps.print_stats() + print(s.getvalue())