def finish(self): from gi.repository import Gtk if self.debug: import yappi yappi.stop() yappi.convert2pstats( yappi.get_func_stats()).dump_stats('mfp-gui-funcstats.pstats') log.log_func = None if self.appwin: self.appwin.quit() self.appwin = None Gtk.main_quit()
def finish(self): from gi.repository import Gtk if self.debug: import yappi yappi.stop() yappi.convert2pstats(yappi.get_func_stats()).dump_stats( 'mfp-gui-funcstats.pstats') log.log_func = None if self.appwin: self.appwin.quit() self.appwin = None Gtk.main_quit()
def main_loop(self): if self.args.profile == -1: profile_file = None else: profile_file = self.args.profile or ('mars_' + self.__class__.__name__ + '.prof') try: if profile_file: import yappi yappi.set_clock_type('wall') yappi.start(builtins=False, profile_threads=False) with self.pool: try: self.start_service() self._running = True while True: self.pool.join(1) for proc in self.pool.processes: if not proc.is_alive(): self.service_logger.fatal( 'Process %d exited unpredictably. exitcode=%d', proc.pid, proc.exitcode) raise KeyboardInterrupt except: self._running = False self.stop_service() finally: self._running = False if profile_file: import yappi yappi.logger = logging.getLogger(__name__) p = yappi.convert2pstats(yappi.get_func_stats()) p.strip_dirs() p.sort_stats('time') p.print_stats(40) p.dump_stats(profile_file)
def main(): profile_file = os.environ.get(config.PROFILE_ENV) if profile_file: import yappi yappi.start() try: print_version() options = parse_args() if options.version: exit(0) print("", file=sys.stderr) try: cfg = config.init_app_config(voyandz.app, options.conffile) except config.ConfigError: exit(1) server.autostart() voyandz.app.run(host=cfg['listenaddr'], port=cfg['listenport'], request_handler=logging.FormattedRequestHandler) finally: if profile_file: yappi.stop() pstats = yappi.convert2pstats(yappi.get_func_stats()) pstats.dump_stats(profile_file) with open(profile_file + ".threads", "w") as threads_file: yappi.get_thread_stats().print_all(threads_file)
def stop(self, *args, **kwargs): if WITH_YAPPI and PROFILE_DIR: yappi.stop() stats = yappi.convert2pstats(yappi.get_func_stats().get()) stats.dump_stats('{}/{}{}.prof'.format(PROFILE_DIR, self.name, self.n)) logging.info("dumped profile of %s %d worker", self.name, self.n)
def main(): profile_file = os.environ.get(PROFILE_ENV) if profile_file: # If profiling is enabled, start it. import yappi yappi.start() try: print_version() # Parse config. options = parse_args() if options.version: exit(0) print("", file=sys.stderr) if options.install: try: install() except InstallError as e: print("Install failed: {}".format(e), file=sys.stderr) exit(0) try: config.load(options.cfgfile) except config.ConfigError as cfg_error: print(cfg_error, file=sys.stderr) exit(1) # Run app. port = options.port or config.config.port adfotg.app.run(host='0.0.0.0', port=port) finally: if profile_file: # If profiling was enabled, stop it and generate reports. yappi.stop() pstats = yappi.convert2pstats(yappi.get_func_stats()) pstats.dump_stats(profile_file) with open(profile_file + ".threads", "w") as threads_file: yappi.get_thread_stats().print_all(threads_file)
def profile_threaded(filename): import yappi # noqa # yappi is not a dependency import gil_load # noqa # same yappi.set_clock_type("cpu") try: gil_load.init() gil_load.start(av_sample_interval=0.1, output_interval=3, output=sys.stdout) monitoring_gil = True except RuntimeError: monitoring_gil = False pass yappi.start() yield yappi.stop() if monitoring_gil: gil_load.stop() print("Gil was held %0.1f %% of the time" % (100 * gil_load.get()[0])) p = yappi.get_func_stats() p = yappi.convert2pstats(p) p.dump_stats(filename) yappi.clear_stats()
def get_profiler_statistics(sort="cum_time", count=20, strip_dirs=True): """Return profiler statistics. :param str sort: dictionary key to sort by :param int|None count: the number of results to return, None returns all results. :param bool strip_dirs: if True strip the directory, otherwise return the full path """ json_stats = [] pstats = yappi.convert2pstats(yappi.get_func_stats()) if strip_dirs: pstats.strip_dirs() for func, func_stat in pstats.stats.items(): path, line, func_name = func cc, num_calls, total_time, cum_time, callers = func_stat json_stats.append({ "path": path, "line": line, "func_name": func_name, "num_calls": num_calls, "total_time": total_time, "total_time_per_call": total_time / num_calls if total_time else 0, "cum_time": cum_time, "cum_time_per_call": cum_time / num_calls if cum_time else 0 }) return sorted(json_stats, key=itemgetter(sort), reverse=True)[:count]
def on_event(self, event, payload): if event == Events.PRINT_STARTED: if self._profile_print_job(): # Start Profiling self._logger.info("Profiling started") yappi.start() if event == Events.PRINT_DONE or event == Events.PRINT_CANCELLED: if self._profile_print_job(): # Stop profiling and save statistics to a file for later analysis now__isoformat = datetime.now().isoformat() file_name = self._profile_output_folder( ) + 'profiler-func-' + now__isoformat + ".callgrind" self._logger.info("Saving callgrind data to %s" % file_name) func_stats = yappi.get_func_stats() func_stats.save(file_name, 'CALLGRIND') file_name = self._profile_output_folder( ) + 'profiler-func-' + now__isoformat + ".pstats" self._logger.info("Saving (functions) pstats data to %s" % file_name) func_pstats = yappi.convert2pstats(yappi.get_func_stats()) func_pstats.dump_stats(file_name) yappi.stop() yappi.clear_stats()
def profile_threaded(filename): import yappi # noqa # yappi is not a dependency yappi.set_clock_type("cpu") try: import gil_load # noqa # same gil_load.init() gil_load.start(av_sample_interval=0.1, output_interval=10, output=sys.stdout) monitoring_gil = True except (RuntimeError, ImportError): monitoring_gil = False pass yappi.start() yield yappi.stop() if monitoring_gil: gil_load.stop() stats = gil_load.get() print("GIL load information: ", gil_load.format(stats)) p = yappi.get_func_stats() p = yappi.convert2pstats(p) p.dump_stats(filename) yappi.clear_stats()
def __exit__(self, exc_type, exc_value, traceback): if self._profile_file is not None: try: logger.debug('Dumping profile...') yappi.stop() pstats = yappi.convert2pstats(yappi.get_func_stats()) pstats.dump_stats(self._profile_file) finally: super().__exit__(exc_type, exc_value, traceback)
def test_generator(self): def _gen(n): while(n > 0): yield n n -= 1 yappi.start() for x in _gen(5): pass self.assertTrue(yappi.convert2pstats(yappi.get_func_stats()) is not None)
def performance(func, isPlot): yappi.clear_stats() yappi.set_clock_type('cpu') yappi.start(builtins=True) func() yappi.stop() stats = yappi.convert2pstats(yappi.get_func_stats()) stats.sort_stats("cumulative") stats.print_stats() if isPlot: yappi.get_func_stats().save('callgrind.foo.prof', type='callgrind') os.system("cat callgrind.foo.prof | python gprof2dot.py -f callgrind | dot -Tpng -o output.png") os.system("rm -f callgrind.foo.prof")
def profile_threaded(filename): import yappi # noqa # yappi is not a dependency if filename is None: yield return yappi.start() yield yappi.stop() p = yappi.get_func_stats() p = yappi.convert2pstats(p) p.dump_stats(filename) yappi.clear_stats()
def main_loop(self): if self.args.profile == -1: profile_file = None else: profile_file = self.args.profile or ( 'mars_' + self.__class__.__name__ + '.prof') try: if profile_file: import yappi yappi.set_clock_type('wall') yappi.start(builtins=False, profile_threads=False) with self.pool: try: self.start() self._running = True while True: self.pool.join(1) stopped = [] for idx, proc in enumerate(self.pool.processes): if not proc.is_alive(): stopped.append(idx) if stopped: self.handle_process_down(stopped) except: self._running = False self.stop() finally: self._running = False if profile_file: import yappi yappi.logger = logging.getLogger(__name__) p = yappi.convert2pstats(yappi.get_func_stats()) p.strip_dirs() p.sort_stats('time') p.print_stats(40) p.dump_stats(profile_file)
def create_stats(self): self.stats = yappi.convert2pstats(yappi.get_func_stats()).stats
else: log.debug("Batch mode requires exactly one input file") finally: app.finish() else: # create initial patch if len(patchfiles): for p in patchfiles: app.open_file(p) elif not app.no_default: app.open_file(None) # allow session management app.session_management_setup() try: QuittableThread.wait_for_all() except (KeyboardInterrupt, SystemExit): log.log_force_console = True app.finish() for thread in app.leftover_threads: thread.join() if app.debug: import yappi yappi.stop() yappi.convert2pstats(yappi.get_func_stats()).dump_stats('mfp-main-funcstats.pstats')
#!/bin/python3 import threading from time import sleep import logging import yappi import activitywatch yappi.start(profile_threads=True) print("Starting to profile") thread = threading.Thread(target=activitywatch.start, kwargs={"loglevel": logging.DEBUG}, daemon=False) thread.start() try: sleep(60*60*24) # Sleep for 24h, allowing activitywatch to do work print("Expired") except KeyboardInterrupt: print("Interrupted") yappi.stop() print("Computing statistics, please wait...") p = yappi.convert2pstats(yappi.get_func_stats().get()) p.sort_stats("cumtime") p.print_stats()
def main(): description = mfp_banner % version() parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=description, epilog=mfp_footer) parser.add_argument("patchfile", nargs='*', help="Patch files to load") parser.add_argument("-f", "--init-file", action="append", default=[utils.homepath(".mfp/mfprc.py")], help="Python source file to exec at launch") parser.add_argument("-p", "--patch-path", action="append", default=[os.getcwd()], help="Search path for patch files") parser.add_argument("-l", "--init-lib", action="append", default=[], help="Extension library (*.so) to load at launch") parser.add_argument("-L", "--lib-path", action="append", default=[], help="Search path for extension libraries") parser.add_argument("-i", "--inputs", default=2, type=int, help="Number of JACK audio input ports") parser.add_argument("-o", "--outputs", default=2, type=int, help="Number of JACK audio output ports") parser.add_argument("--midi-ins", default=1, type=int, help="Number of MIDI input ports") parser.add_argument("--midi-outs", default=1, type=int, help="Number of MIDI output ports") parser.add_argument("-u", "--osc-udp-port", default=5555, type=int, help="UDP port to listen for OSC (default: 5555)") parser.add_argument("-v", "--verbose", action="store_true", help="Log all messages to console") parser.add_argument("--verbose-remote", action="store_true", help="Log all child console output") parser.add_argument("--max-bufsize", default=2048, help="Maximum JACK buffer size to support (default: 2048 frames)") parser.add_argument("--no-gui", action="store_true", help="Do not launch the GUI engine") parser.add_argument("--no-dsp", action="store_true", help="Do not launch the DSP engine") parser.add_argument("--no-default", action="store_true", help="Do not create a default patch") parser.add_argument("--no-restart", action="store_true", help="Do not restart DSP engine if it crashes") parser.add_argument("--no-onload", action="store_true", help="Do not run onload/loadbang functions") parser.add_argument("--help-builtins", action="store_true", help="Display help on builtin objects and exit") parser.add_argument("-s", "--socket-path", default="/tmp/mfp_rpcsock", help="Path to create Unix-domain socket for RPC") parser.add_argument("-d", "--debug", action="store_true", help="Enable debugging behaviors") # batch mode options parser.add_argument("-b", "--batch", action="store_true", help="Run in batch mode") parser.add_argument("-a", "--args", default='', help="Batch mode patch arguments") parser.add_argument("-I", "--batch-input", default=None, help="Batch mode input file") parser.add_argument("-e", "--batch-eval", action="store_true", help="Call eval() on input before sending") args = vars(parser.parse_args()) # test imports to make sure everything is installed properly test_imports() # create the app object app = MFPApp() # configure some things from command line app.no_gui = args.get("no_gui") or args.get("help_builtins") or args.get("help") app.no_dsp = args.get("no_dsp") or args.get("help_builtins") or args.get("help") app.no_default = args.get("no_default") app.no_restart = args.get("no_restart") app.no_onload = args.get("no_onload") app.dsp_inputs = args.get("inputs") app.dsp_outputs = args.get("outputs") app.midi_inputs = args.get("midi_ins") app.midi_outputs = args.get("midi_outs") app.osc_port = args.get("osc_udp_port") app.searchpath = ':'.join(args.get("patch_path")) app.extpath = ':'.join(args.get("lib_path")) app.max_blocksize = args.get("max_bufsize") app.socket_path = args.get("socket_path") app.debug = args.get("debug") if args.get('batch'): app.batch_mode = True app.batch_args = args.get("args") app.batch_input_file = args.get("batch_input") app.batch_eval = args.get("batch_eval", False) app.no_gui = True log.log_raw = True log.log_quiet = True if args.get("verbose"): log.log_force_console = True if args.get("verbose_remote"): app.debug_remote = True if app.no_gui: log.debug("Not starting GUI services") if app.no_dsp: log.debug("Not starting DSP engine") if app.no_default: log.debug("Not creating default patch") # launch processes and threads import signal signal.signal(signal.SIGTERM, exit_sighandler) try: app.setup() except (StartupError, KeyboardInterrupt, SystemExit): log.debug("Setup did not complete properly, exiting") app.finish() return # ok, now start configuring the running system add_evaluator_defaults() builtins.register() for libname in args.get("init_lib"): app.load_extension(libname) evaluator = Evaluator() pyfiles = args.get("init_file", []) for f in pyfiles: fullpath = utils.find_file_in_path(f, app.searchpath) log.debug("initfile: Looking for", f) if not fullpath: log.debug("initfile: Cannot find file %s, skipping" % f) continue try: os.stat(fullpath) except OSError: log.debug("initfile: Error accessing file", fullpath) continue try: evaluator.exec_file(fullpath) except Exception as e: log.debug("initfile: Exception while loading initfile", f) log.debug(e) if app.debug: import yappi yappi.start() if args.get("help"): log.log_debug = None log.log_file = None log.debug("printing help and exiting") parser.print_help() log.debug("done with print_help(), quitting") app.finish() elif args.get("help_builtins"): log.log_debug = None log.log_file = None app.open_file(None) for name, factory in sorted(app.registry.items()): if hasattr(factory, 'doc_tooltip_obj'): print("%-12s : %s" % ("[%s]" % name, factory.doc_tooltip_obj)) else: try: o = factory(name, None, app.patches['default'], None, "") print("%-12s : %s" % ("[%s]" % name, o.doc_tooltip_obj)) except Exception as e: import traceback print("(caught exception trying to create %s)" % name, e) traceback.print_exc() print("%-12s : No documentation found" % ("[%s]" % name,)) app.finish() else: patchfiles = args.get("patchfile") if app.batch_mode: try: if len(patchfiles) == 1: app.batch_obj = patchfiles[0] app.exec_batch() else: log.debug("Batch mode requires exactly one input file") finally: app.finish() else: # create initial patch if len(patchfiles): for p in patchfiles: app.open_file(p) elif not app.no_default: app.open_file(None) # allow session management app.session_management_setup() try: QuittableThread.wait_for_all() except (KeyboardInterrupt, SystemExit): log.log_force_console = True app.finish() for thread in app.leftover_threads: thread.join() if app.debug: import yappi yappi.stop() yappi.convert2pstats(yappi.get_func_stats()).dump_stats('mfp-main-funcstats.pstats')