def lsprofile(ui, func, fp): format = ui.config('profiling', 'format', default='text') field = ui.config('profiling', 'sort', default='inlinetime') limit = ui.configint('profiling', 'limit', default=30) climit = ui.configint('profiling', 'nested', default=5) if format not in ['text', 'kcachegrind']: ui.warn( _("unrecognized profiling format '%s'" " - Ignored\n") % format) format = 'text' try: from mercurial import lsprof except ImportError: raise util.Abort( _('lsprof not available - install from ' 'http://codespeak.net/svn/user/arigo/hack/misc/lsprof/')) p = lsprof.Profiler() p.enable(subcalls=True) try: return func() finally: p.disable() if format == 'kcachegrind': import lsprofcalltree calltree = lsprofcalltree.KCacheGrind(p) calltree.output(fp) else: # format == 'text' stats = lsprof.Stats(p.getstats()) stats.sort(field) stats.pprint(limit=limit, file=fp, climit=climit)
def __exit__(self, *args): self.c_profile.disable() k_profile = lsprofcalltree.KCacheGrind(self.c_profile) k_file = open("{0}/profile_result.out".format(config.OUTPUT_PATH), 'w+') k_profile.output(k_file) k_file.close() self.c_profile.print_stats('cumulative')
def __call__(self): """ Profile this request and output results in a cachegrind compatible format. """ import cProfile try: import lsprofcalltree except ImportError: import hf.external.lsprofcalltree as lsprofcalltree try: p = cProfile.Profile() p.runctx('self._real_call()', globals(), locals()) finally: count = 1 filename = None path = cp.request.path_info.strip("/").replace("/", "_") script = cp.request.app.script_name.strip("/").replace("/", "_") path = path + "_" + script while not filename or os.path.exists(filename): filename = os.path.join(hf.hf_dir, "cachegrind.out.%s_%d" % (path, count)) count += 1 print "writing profile output to %s" % filename k = lsprofcalltree.KCacheGrind(p) data = open(filename, 'w+') k.output(data) data.close() return self.result
def main(): parser = get_parser() (options, args) = parser.parse_args() if options.exclude_cam_ids is not None: options.exclude_cam_ids = options.exclude_cam_ids.split() if options.exclude_camns is not None: options.exclude_camns = [ int(camn) for camn in options.exclude_camns.split() ] if len(args) > 1: print("args", args) print( ("arguments interpreted as FILE supplied more " "than once"), file=sys.stderr, ) parser.print_help() sys.exit(1) if len(args) < 1: parser.print_help() sys.exit(1) src_filename = args[0] args = (src_filename, ) kwargs = dict( dest_filename=options.dest_filename, reconstructor_filename=options.reconstructor_path, start_frame=options.start, stop_frame=options.stop, exclude_cam_ids=options.exclude_cam_ids, exclude_camns=options.exclude_camns, dynamic_model_name=options.dynamic_model, debug=options.debug, frames_per_second=options.fps, area_threshold=options.area_threshold, min_observations_to_save=options.min_observations_to_save, options=options, ) if int(os.environ.get("PROFILE", "0")): import cProfile import lsprofcalltree p = cProfile.Profile() print("running kalmanize in profile mode") p.runctx("kalmanize(*args, **kwargs)", globals(), locals()) k = lsprofcalltree.KCacheGrind(p) data = open(os.path.expanduser("~/kalmanize.kgrind"), "w+") k.output(data) data.close() else: kalmanize(*args, **kwargs)
def doProfile(who): import cProfile, lsprofcalltree profile = cProfile.Profile() profile.enable() AddItem(who, "{1001:100}") profile.disable() stats = lsprofcalltree.KCacheGrind(profile) stats.output(open('cProfile.callgrind', 'w'))
def _runcommand(ui, options, cmd, cmdfunc): def checkargs(): try: return cmdfunc() except error.SignatureError: raise error.ParseError(cmd, _("invalid arguments")) if options['profile']: format = ui.config('profiling', 'format', default='text') if not format in ['text', 'kcachegrind']: ui.warn( _("unrecognized profiling format '%s'" " - Ignored\n") % format) format = 'text' output = ui.config('profiling', 'output') if output: path = os.path.expanduser(output) path = ui.expandpath(path) ostream = open(path, 'wb') else: ostream = sys.stderr try: from mercurial import lsprof except ImportError: raise util.Abort( _('lsprof not available - install from ' 'http://codespeak.net/svn/user/arigo/hack/misc/lsprof/')) p = lsprof.Profiler() p.enable(subcalls=True) try: return checkargs() finally: p.disable() if format == 'kcachegrind': import lsprofcalltree calltree = lsprofcalltree.KCacheGrind(p) calltree.output(ostream) else: # format == 'text' stats = lsprof.Stats(p.getstats()) stats.sort() stats.pprint(top=10, file=ostream, climit=5) if output: ostream.close() else: return checkargs()
def main(): options, args = create_option_parser().parse_args() import cProfile import lsprofcalltree profiler = cProfile.Profile() profile_file = open(options.profilefile, 'wb') try: profiler.runcall(test_langdb, options) finally: k_cache_grind = lsprofcalltree.KCacheGrind(profiler) k_cache_grind.output(profile_file) profile_file.close()
# We should have very low error assert mean_error < MAX_MEAN_ERROR return {'fps': fps} def benchmark(): rd = check_online_reconstruction(with_water=False, with_orientation=False, multithreaded=False) pprint.pprint(rd) if __name__ == '__main__': if len(sys.argv) == 2: kcachegrind_output_fname = sys.argv[1] else: kcachegrind_output_fname = None if kcachegrind_output_fname is not None: import cProfile import lsprofcalltree p = cProfile.Profile() print 'running test in profile mode' p.runctx('benchmark()', globals(), locals()) k = lsprofcalltree.KCacheGrind(p) data = open(kcachegrind_output_fname, 'w') k.output(data) data.close() else: benchmark()
def main(): # make the Python warnings go to Friture logger logging.captureWarnings(True) logFormat = "%(asctime)s %(levelname)s %(name)s: %(message)s" formatter = logging.Formatter(logFormat) logFileName = "friture.log.txt" dirs = appdirs.AppDirs("Friture", "") logDir = dirs.user_data_dir try: os.makedirs(logDir) except OSError as e: if e.errno != errno.EEXIST: raise logFilePath = os.path.join(logDir, logFileName) # log to file fileHandler = logging.handlers.RotatingFileHandler(logFilePath, maxBytes=100000, backupCount=5) fileHandler.setLevel(logging.DEBUG) fileHandler.setFormatter(formatter) rootLogger = logging.getLogger() rootLogger.setLevel(logging.DEBUG) rootLogger.addHandler(fileHandler) if hasattr(sys, "frozen"): # redirect stdout and stderr to the logger if this is a pyinstaller bundle sys.stdout = StreamToLogger(logging.getLogger('STDOUT'), logging.INFO) sys.stderr = StreamToLogger(logging.getLogger('STDERR'), logging.ERROR) else: # log to console if this is not a pyinstaller bundle console = logging.StreamHandler() console.setLevel(logging.DEBUG) console.setFormatter(formatter) rootLogger.addHandler(console) # make Qt logs go to Friture logger QtCore.qInstallMessageHandler(qt_message_handler) logger = logging.getLogger(__name__) logger.info("Friture %s starting on %s (%s)", friture.__version__, platform.system(), sys.platform) # make sure Qt loads the desktop OpenGL stack, rather than OpenGL ES or a software OpenGL # only the former option is compatible with the use of PyOpenGL QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_UseDesktopOpenGL) if platform.system() == "Windows": logger.info("Applying Windows-specific setup") # enable automatic scaling for high-DPI screens os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1" # set the App ID for Windows 7 to properly display the icon in the # taskbar. import ctypes myappid = 'Friture.Friture.Friture.current' # arbitrary string try: ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID( myappid) except: logger.error( "Could not set the app model ID. If the plaftorm is older than Windows 7, this is normal." ) app = QApplication(sys.argv) if platform.system() == "Darwin": logger.info("Applying Mac OS-specific setup") # help the packaged application find the Qt plugins (imageformats and platforms) pluginsPath = os.path.normpath( os.path.join(QApplication.applicationDirPath(), os.path.pardir, 'PlugIns')) logger.info("Adding the following to the Library paths: %s", pluginsPath) QApplication.addLibraryPath(pluginsPath) # on macOS, OpenGL 2.1 does not work well # request a 3.2 Core context instead format = QSurfaceFormat() format.setDepthBufferSize(24) format.setStencilBufferSize(8) format.setVersion(3, 2) format.setProfile(QSurfaceFormat.CoreProfile) QSurfaceFormat.setDefaultFormat(format) # Splash screen #pixmap = QPixmap(":/images/splash.png") #splash = QSplashScreen(pixmap) #splash.show() #splash.showMessage("Initializing the audio subsystem") app.processEvents() window = Friture() window.show() #splash.finish(window) profile = "no" # "python" or "kcachegrind" or anything else to disable if len(sys.argv) > 1: if sys.argv[1] == "--python": profile = "python" elif sys.argv[1] == "--kcachegrind": profile = "kcachegrind" elif sys.argv[1] == "--no": profile = "no" else: logger.info("command-line arguments (%s) not recognized", sys.argv[1:]) return_code = 0 if profile == "python": import cProfile import pstats # friture.cprof can be visualized with SnakeViz # http://jiffyclub.github.io/snakeviz/ # snakeviz friture.cprof cProfile.runctx('app.exec_()', globals(), locals(), filename="friture.cprof") logger.info("Profile saved to '%s'", "friture.cprof") stats = pstats.Stats("friture.cprof") stats.strip_dirs().sort_stats('time').print_stats(20) stats.strip_dirs().sort_stats('cumulative').print_stats(20) elif profile == "kcachegrind": import cProfile import lsprofcalltree p = cProfile.Profile() p.run('app.exec_()') k = lsprofcalltree.KCacheGrind(p) with open('cachegrind.out.00000', 'wb') as data: k.output(data) else: return_code = app.exec_() # explicitly delete the main windows instead of waiting for the interpreter shutdown # tentative to prevent errors on exit on macos del window sys.exit(return_code)
def main(): print("Platform is %s (%s)" % (platform.system(), sys.platform)) if platform.system() == "Windows": print("Applying Windows-specific setup") # On Windows, redirect stderr to a file import imp import ctypes if (hasattr(sys, "frozen") or # new py2exe hasattr(sys, "importers") or # old py2exe imp.is_frozen("__main__")): # tools/freeze sys.stderr = open(os.path.expanduser("~/friture.exe.log"), "w") # set the App ID for Windows 7 to properly display the icon in the # taskbar. myappid = 'Friture.Friture.Friture.current' # arbitrary string try: ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID( myappid) except: print( "Could not set the app model ID. If the plaftorm is older than Windows 7, this is normal." ) app = QApplication(sys.argv) if platform.system() == "Darwin": if hasattr(sys, "frozen"): #py2app sys.stdout = open(os.path.expanduser("~/friture.out.txt"), "w") sys.stderr = open(os.path.expanduser("~/friture.err.txt"), "w") print("Applying Mac OS-specific setup") # help the py2app-packaged application find the Qt plugins (imageformats and platforms) pluginsPath = os.path.normpath( os.path.join(QApplication.applicationDirPath(), os.path.pardir, 'PlugIns')) print("Adding the following to the Library paths: " + pluginsPath) QApplication.addLibraryPath(pluginsPath) # Splash screen pixmap = QPixmap(":/images/splash.png") splash = QSplashScreen(pixmap) splash.show() splash.showMessage("Initializing the audio subsystem") app.processEvents() # Logger class logger = Logger() window = Friture(logger) window.show() splash.finish(window) profile = "no" # "python" or "kcachegrind" or anything else to disable if len(sys.argv) > 1: if sys.argv[1] == "--python": profile = "python" elif sys.argv[1] == "--kcachegrind": profile = "kcachegrind" elif sys.argv[1] == "--no": profile = "no" else: print("command-line arguments (%s) not recognized" % sys.argv[1:]) if profile == "python": import cProfile import pstats cProfile.runctx('app.exec_()', globals(), locals(), filename="friture.cprof") stats = pstats.Stats("friture.cprof") stats.strip_dirs().sort_stats('time').print_stats(20) stats.strip_dirs().sort_stats('cumulative').print_stats(20) sys.exit(0) elif profile == "kcachegrind": import cProfile import lsprofcalltree p = cProfile.Profile() p.run('app.exec_()') k = lsprofcalltree.KCacheGrind(p) with open('cachegrind.out.00000', 'wb') as data: k.output(data) sys.exit(0) else: sys.exit(app.exec_())
def _inner(*args, **kwargs): try: # For Python 2.5+: import cProfile as profile except ImportError: # For Python 2.4: import profile from django.conf import settings import sys # NOTE: Must use this, or the 'filename' global/local var gets # messed up. #filename2 = filename filename2 = None #print '_inner()' if filename2 is None: # Default to <function_name>_<time>.txt and .out filename2 = '%s_%s' % (view_func.__name__, time.time()) filename_full = os.path.join(settings.PROFILER_OUTPUT_ROOT, filename2) #print 'filename2: %r' %filename2 #print 'filename_full: %r' %filename_full if settings.PROFILER_OUTPUT_ROOT is None: raise Exception('settings.PROFILER_OUTPUT_ROOT must be set ' 'to save profiler output.') elif not os.path.exists(settings.PROFILER_OUTPUT_ROOT): os.mkdir(settings.PROFILER_OUTPUT_ROOT) # raise Exception('The settings.PROFILER_OUTPUT_ROOT folder %r ' # 'does not exist.' % settings.PROFILER_OUTPUT_ROOT) #print ' view_func: %r' % view_func #print ' args: %s' % repr(args) #print ' kwargs: %s' % repr(kwargs) #print ' ----------' #response = view_func(*args, **kwargs) if settings.PROFILER_OUTPUT_LINEBYLINE: import line_profiler prof = line_profiler.LineProfiler(view_func) response = prof.runcall(view_func, *args, **kwargs) #print 'response: %r' % response # Line-by-line profiler file1 = open(filename_full + '.lineprofiler.txt', 'w') #prof.print_stats(file1) stats = prof.get_stats() #line_profiler.show_text(stats.timings, stats.unit, stream=file1) def show_text2(stats, unit, stream=None): """ Show text for the given timings. """ if stream is None: stream = sys.stdout #print >>stream, 'Timer unit: %g s' % unit #print >>stream, '' for (fn, lineno, name), timings in sorted(stats.items()): show_func2(fn, lineno, name, stats[fn, lineno, name], unit, stream=stream) def show_func2(filename, start_lineno, func_name, timings, unit, stream=None): """ Show results for a single function. """ from line_profiler import linecache, inspect if stream is None: stream = sys.stdout print >>stream, "File: %s" % filename print >>stream, "Function: %s at line %s" % (func_name, start_lineno) template = '%6s %9s %12s %8s %8s %-s' d = {} total_time = 0.0 linenos = [] for lineno, nhits, time in timings: total_time += time linenos.append(lineno) print >>stream, "Total time: %g s" % (total_time * unit) if not os.path.exists(filename): print >>stream, "" print >>stream, "Could not find file %s" % filename print >>stream, "Are you sure you are running this " \ "program from the same directory" print >>stream, "that you ran the profiler from?" print >>stream, "Continuing without the function's " \ "contents." # Fake empty lines so we can see the timings, # if not the code. nlines = max(linenos) - min(min(linenos), start_lineno) + 1 sublines = [''] * nlines else: all_lines = linecache.getlines(filename) sublines = inspect.getblock(all_lines[start_lineno-1:]) for lineno, nhits, time in timings: d[lineno] = (nhits, time, '%5.1f' % (float(time) / nhits), '%5.1f' % (100*time / total_time)) linenos = range(start_lineno, start_lineno + len(sublines)) empty = ('', '', '', '') header = template % ('Line #', 'Hits', 'Time', 'Per Hit', '% Time', 'Line Contents') print >>stream, "" print >>stream, header print >>stream, '=' * len(header) for lineno, line in zip(linenos, sublines): nhits, time, per_hit, percent = d.get(lineno, empty) if per_hit != '': per_hit = round(float(per_hit) * unit, 2) if per_hit == 0: per_hit = '-' else: per_hit = '%0.2f' % per_hit if time != '': time = round(float(time) * unit, 2) if time == 0: time = '-' else: time = '%0.2f' % time if percent != '' and float(percent) == 0: percent = '-' print >>stream, template % \ (lineno, nhits, time, per_hit, percent, line.rstrip('\n').rstrip('\r')) print >>stream, "" show_text2(stats.timings, stats.unit, stream=file1) del file1 else: # Other (not line-by-line) profilers. prof = profile.Profile() response = prof.runcall(view_func, *args, **kwargs) #print 'response: %r' % response if hasattr(settings, 'PROFILER_OUTPUT_TXT') \ and settings.PROFILER_OUTPUT_TXT: # Save text output. file1 = open(filename_full + '.txt', 'w') old_stdout = sys.stdout sys.stdout = file1 # Internal Time #prof.print_stats(sort=1) # Cumulative prof.print_stats(sort=2) sys.stdout = old_stdout del file1 if (hasattr(settings, 'PROFILER_OUTPUT_BINARY') and settings.PROFILER_OUTPUT_BINARY) \ or (hasattr(settings, 'PROFILER_OUTPUT_PNG') and settings.PROFILER_OUTPUT_PNG): # Save the binary output. prof.dump_stats(filename_full + '.profile_out') if hasattr(settings, 'PROFILER_OUTPUT_PNG') \ and settings.PROFILER_OUTPUT_PNG: # Create the PNG callgraph image. os.system('%s -f pstats %s | dot -Tpng -o %s 2>NUL' % (relpath(__file__, 'scripts/gprof2dot.py'), filename_full + '.profile_out', filename_full + '.png')) if not hasattr(settings, 'PROFILER_OUTPUT_BINARY') \ or not settings.PROFILER_OUTPUT_BINARY: # We only wanted the PNG file, delete the binary file now # that we're done with it. os.remove(filename_full + '.profile_out') if hasattr(settings, 'PROFILER_OUTPUT_KCACHEGRIND') \ and settings.PROFILER_OUTPUT_KCACHEGRIND: # Save kcachegrind-compatible output. if hasattr(prof, 'getstats'): import lsprofcalltree k = lsprofcalltree.KCacheGrind(prof) file1 = open(filename_full + '.kcachegrind', 'w') k.output(file1) del file1 #print ' ----------' #print '~_inner()' return response
def __call__(self, environ, start_response): """ Profile this request and output results in a cachegrind compatible format. """ catch_response = [] body = [] def replace_start_response(status, headers, exc_info=None): catch_response.extend([status, headers]) start_response(status, headers, exc_info) return body.append def run_app(): app_iter = self.app(environ, replace_start_response) try: body.extend(app_iter) finally: if hasattr(app_iter, 'close'): app_iter.close() import __builtin__ try: import lsprofcalltree calltree_enabled = True except ImportError: calltree_enabled = False import sys, os, time pstat_fn = None cg_fn = None calltree_enabled = calltree_enabled and self.profile_grind if self.profile_data_dir: # XXX fixme, this should end up in a better location if not os.path.exists(self.profile_data_dir): os.mkdir(self.profile_data_dir) count = 1 path = environ.get('PATH_INFO', '/tmp') if path == '/': path = 'root' path = path.strip("/").replace("/", "_") pid = os.getpid() t = time.time() pstat_fn = os.path.join(self.profile_data_dir, "prof.out.%s.%d.%d" % (path, pid, t)) if calltree_enabled: cg_fn = os.path.join( self.profile_data_dir, "cachegrind.out.%s.%d.%d" % (path, pid, t)) if self.profile_type == 'line': import line_profiler p = prof = line_profiler.LineProfiler() # line profiler aparently needs to be a builtin self.profile_builtin = True # line profiler has no get_stat, so converting to cachegrind # will not work calltree_enabled = False else: p = prof = ContextualProfile() if self.profile_builtin: __builtin__.__dict__['_profiler'] = p if self.profile_type == 'line': # reset the profile for the next run for k in p.code_map.keys(): p.code_map[k] = {} try: if self.profile_builtin: run_app() else: p.runctx('run_app()', globals(), locals()) finally: if self.profile_print: if self.profile_type == 'line': # line profiler doesn't support sort p.print_stats() else: p.print_stats(sort=self.profile_sort) if pstat_fn: print >> sys.stderr, "writing profile data to %s" % pstat_fn p.dump_stats(pstat_fn) if calltree_enabled: print >> sys.stderr, "writing cachegrind output to %s" % cg_fn k = lsprofcalltree.KCacheGrind(p) data = open(cg_fn, 'w+') k.output(data) data.close() return body
'indexed_search.prof') stats = pstats.Stats('indexed_search.prof') stats.strip_dirs() stats.sort_stats('time', 'calls') if verbose: stats.print_stats() else: stats.print_stats(20) elif dokprofile: from cProfile import Profile import lsprofcalltree prof = Profile() prof.run( 'db.query_db(niter, dtype, onlyidxquery, onlynonidxquery, avoidfscache, verbose, inkernel)' ) kcg = lsprofcalltree.KCacheGrind(prof) ofile = open('indexed_search.kcg', 'w') kcg.output(ofile) ofile.close() elif doprofile: import hotshot, hotshot.stats prof = hotshot.Profile("indexed_search.prof") benchtime, stones = prof.run( 'db.query_db(niter, dtype, onlyidxquery, onlynonidxquery, avoidfscache, verbose, inkernel)' ) prof.close() stats = hotshot.stats.load("indexed_search.prof") stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(20) else:
import lsprofcalltree import cProfile from gevent_logic import launch profile = cProfile.Profile() profile.enable() launch() profile.disable() stats = lsprofcalltree.KCacheGrind(profile) stats.output(open('callgrind.profile', 'w'))