From eeff27bae9af5023b3c7655cd36eff9e46fbfa4c Mon Sep 17 00:00:00 2001 From: sfaulds Date: Thu, 16 Mar 2017 15:50:44 +1100 Subject: [PATCH] use print_stats to output stats to allow internal sorting --- resources/lib/functions.py | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/resources/lib/functions.py b/resources/lib/functions.py index add7e40..8799d58 100644 --- a/resources/lib/functions.py +++ b/resources/lib/functions.py @@ -31,6 +31,7 @@ from urlparse import urlparse import cProfile import pstats import json as json +import StringIO import xbmcplugin import xbmcgui @@ -62,12 +63,13 @@ def mainEntryPoint(): log.info("===== EmbyCon START =====") - ProfileCode = __settings__.getSetting('profile') == "true" - - if(ProfileCode): - xbmcgui.Dialog().ok("Profiling Enabled", "Remember to turn off when you have finished testing") - pr = cProfile.Profile() - pr.enable() + profile_code = __settings__.getSetting('profile') == "true" + pr = None + if(profile_code): + return_value = xbmcgui.Dialog().yesno("Profiling Enabled", "Do you want to run profiling?") + if return_value: + pr = cProfile.Profile() + pr.enable() ADDON_VERSION = ClientInformation().getVersion() log.info("EmbyCon -> running Python: " + str(sys.version_info)) @@ -169,13 +171,25 @@ def mainEntryPoint(): dataManager.canRefreshNow = True - if(ProfileCode): + if(pr): pr.disable() - ps = pstats.Stats(pr) - + + fileTimeStamp = time.strftime("%Y-%m-%d %H-%M-%S") - tabFileName = __addondir__ + "profile_(" + fileTimeStamp + ").tab" + tabFileName = __addondir__ + "profile_(" + fileTimeStamp + ").txt" f = open(tabFileName, 'wb') + + s = StringIO.StringIO() + ps = pstats.Stats(pr, stream=s) + ps = ps.sort_stats('cumulative') + ps.print_stats() + ps.strip_dirs() + ps = ps.sort_stats('cumulative') + ps.print_stats() + f.write(s.getvalue()) + + ''' + ps = pstats.Stats(pr) f.write("NumbCalls\tTotalTime\tCumulativeTime\tFunctionName\tFileName\r\n") for (key, value) in ps.stats.items(): (filename, count, func_name) = key @@ -184,7 +198,9 @@ def mainEntryPoint(): f.write(str(ncalls) + "\t" + "{:10.4f}".format(total_time) + "\t" + "{:10.4f}".format(cumulative_time) + "\t" + func_name + "\t" + filename + "\r\n") except ValueError: f.write(str(ncalls) + "\t" + "{0}".format(total_time) + "\t" + "{0}".format(cumulative_time) + "\t" + func_name + "\t" + filename + "\r\n") - f.close() + ''' + + f.close() log.info("===== EmbyCon FINISHED =====")