def test_module_stress(self): self.assertEqual(yappi.is_running(), False) yappi.start() yappi.clear_stats() self.assertRaises(_yappi.error, yappi.set_clock_type, "wall") yappi.stop() yappi.clear_stats() yappi.set_clock_type("cpu") self.assertRaises(yappi.YappiError, yappi.set_clock_type, "dummy") self.assertEqual(yappi.is_running(), False) yappi.clear_stats() yappi.clear_stats()
def _start_profiling(self, clock): with lock: if yappi.is_running(): raise HTTPBadRequest("profile is already running") log.info("Starting profiling using %r clock", clock) yappi.set_clock_type(clock) yappi.start(builtins=True, profile_threads=True)
def stop(): pages.require("/admin/settings.edit", noautoreturn=True) pages.postOnly() import yappi if yappi.is_running(): yappi.stop() raise cherrypy.HTTPRedirect("/settings/profiler")
def injection_unload(**kwargs): try: import yappi if yappi.is_running(): yappi.stop() except: pass
def _stop_profiling(filename, format): logging.debug("Stopping profiling") with _lock: if yappi.is_running(): yappi.stop() stats = yappi.get_func_stats() stats.save(filename, format) yappi.clear_stats()
def injection_load(**kwargs): import threading try: import yappi if not yappi.is_running(): yappi.start() except: pass
def _stop_profiling(filename, format): logging.debug("Stopping CPU profiling") with _lock: if yappi.is_running(): yappi.stop() stats = yappi.get_func_stats() stats.save(filename, format) yappi.clear_stats()
def start(): pages.require("/admin/settings.edit") import yappi if not yappi.is_running(): yappi.start() time.sleep(0.5) messagebus.postMessage("/system/settings/activatedprofiler",pages.getAcessingUser()) raise cherrypy.HTTPRedirect("/settings/profiler")
def start(): pages.require("/admin/settings.edit", noautoreturn=True) pages.postOnly() import yappi if not yappi.is_running(): yappi.start() time.sleep(0.5) messagebus.postMessage("/system/settings/activatedprofiler",pages.getAcessingUser()) raise cherrypy.HTTPRedirect("/settings/profiler")
def _start_profiling(self, clock): with lock: if yappi.is_running(): raise http.Error(http.BAD_REQUEST, "profile is already running") log.info("Starting profiling using %r clock", clock) yappi.set_clock_type(clock) yappi.start(builtins=True, profile_threads=True)
def stop(self): if not yappi.is_running(): raise UsageError("CPU profiler is not running") logging.info("Stopping CPU profiling") yappi.stop() stats = yappi.get_func_stats() stats.save(self.filename, self.format) yappi.clear_stats()
def _start_profiling(self, clock): with lock: if yappi.is_running(): raise http.Error( http.BAD_REQUEST, "profile is already running") log.info("Starting profiling using %r clock", clock) yappi.set_clock_type(clock) yappi.start(builtins=True, profile_threads=True)
def update(self, config, current_time=None): """ Updates the state of the profiler - either enabling or disabling it, based on the current time and whether or not the current profiling interval has started/stopped """ # no profiling if the profiler isn't available if yappi is None: return if current_time is None: current_time = time.time() try: # check if profiling is enabled in the config and turn it on/off if necessary if config.enable_profiling: if not self._is_enabled: self._update_start_interval(config, current_time) self._is_enabled = True else: if yappi.is_running(): self._stop(config, current_time) self._is_enabled = False # only do profiling if we are still enabled if not self._is_enabled: return # check if the current profiling interval needs to start or stop if yappi.is_running(): if current_time > self._profile_end: self._stop(config, current_time) self._update_start_interval(config, current_time) else: if current_time > self._profile_start: self._start(config, current_time) except Exception, e: global_log.log( scalyr_logging.DEBUG_LEVEL_0, "Failed to update profiler: %s, %s" % (str(e), traceback.format_exc()), limit_once_per_x_secs=300, limit_key="profiler-update", )
def prof_start(self): """ Start service profiling """ import yappi if yappi.is_running(): return "Already running" else: yappi.start() return "Profiling started"
def _stop_profiling(self): with lock: if not yappi.is_running(): raise HTTPBadRequest("profile is not running") log.info("Stopping profiling, writing profile to %r", self.config.profile.filename) yappi.stop() stats = yappi.get_func_stats() stats.save(self.config.profile.filename, type="pstat") yappi.clear_stats()
def _start_profiling(): global yappi logging.debug("Starting profiling") with _lock: import yappi # yappi start semantics are a bit too liberal, returning success if # yappi is already started, happily having too different code paths # that thinks they own the single process profiler. if yappi.is_running(): raise Error('Profiler is already running') yappi.start()
def _stop_profiling(self): with lock: if not yappi.is_running(): raise http.Error(http.BAD_REQUEST, "profile is not running") log.info("Stopping profiling, writing profile to %r", self.config.profile.filename) yappi.stop() stats = yappi.get_func_stats() stats.save(self.config.profile.filename, type="pstat") yappi.clear_stats()
async def start_prof(): """ Start code profiling :return: """ import yappi if yappi.is_running(): return "Already running" yappi.start() return "Profiling started"
async def stop_prof(): """ Stop code profiling :return: """ import yappi if not yappi.is_running(): return "Not running" return "Profiling stopped"
def injection(cmd=None, **kwargs): import yappi if cmd == 'reset': yappi.clear_stats() return True else: if not yappi.is_running(): yappi.start() result = [] for f in yappi.get_func_stats(): result.append((f.name, f.ncall, f.nactualcall, f.ttot, f.tsub, f.tavg, f.module, f.lineno, f.builtin)) return result
def impl(): out = ''' <p> Profile is{0}running. <a href='start'>Start</a> <a href='stop'>Stop</a> <a href='clear_stats'>Clear stats</a> '''.format( ' ' if yappi.is_running() else ' not ' ) fkeys = 'index module lineno name ncall nactualcall builtin ttot tsub tavg'.split() out += '<table><tr>{0}</tr>'.format(''.join(map('<th>{0}</th>'.format, fkeys))) fmt = '<tr>{0}</tr>'.format(''.join('<td>{0.%s}</td>' % (k, ) for k in fkeys)) out += ''.join(map(fmt.format, yappi.get_func_stats())) out += '</table>' return out
def injection(thread_stack_info=None, **kwargs): import threading import sys result = [] if thread_stack_info is None: import inspect yi = {} try: import yappi if not yappi.is_running(): yappi.start() for d in yappi.get_thread_stats(): yi[d[2]] = (d[3], d[4]) except: pass for t in threading.enumerate(): if not t.name.startswith('__pptop_injection'): try: target = '{}.{}'.format( t._target.__module__, t._target.__qualname__ if hasattr( t._target, '__qualname__') else t._target.__name__) except: target = None y = yi.get(t.ident) r = (t.ident, t.daemon, t.name, target, y[0] if y else 0, y[1] if y else 0) try: x = inspect.getframeinfo(sys._current_frames()[t.ident]) r += (' '.join(x.code_context).strip(), '{}:{}'.format(x.filename, x.lineno)) except: # from pptop.logger import log_traceback # log_traceback() r += (None, None) result.append(r) else: try: import traceback import linecache for fi in traceback.extract_stack( sys._current_frames()[thread_stack_info]): f = fi.filename ln = fi.lineno cmd = linecache.getline(f, ln).strip() result.append((cmd, '{}:{}'.format(f, ln))) except Exception: e = sys.exc_info() result.append((e[0].__name__, str(e[1]))) return result
def _start_profiling(clock, builtins, threads): global yappi logging.debug("Starting profiling") import yappi with _lock: # yappi start semantics are a bit too liberal, returning success if # yappi is already started, happily having too different code paths # that thinks they own the single process profiler. if yappi.is_running(): raise Error('Profiler is already running') yappi.set_clock_type(clock) yappi.start(builtins=builtins, profile_threads=threads)
def _start_profiling(clock, builtins, threads): global yappi logging.debug("Starting CPU profiling") import yappi with _lock: # yappi start semantics are a bit too liberal, returning success if # yappi is already started, happily having too different code paths # that thinks they own the single process profiler. if yappi.is_running(): raise UsageError('CPU profiler is already running') yappi.set_clock_type(clock) yappi.start(builtins=builtins, profile_threads=threads)
def start(): pages.require("/admin/settings.edit", noautoreturn=True) pages.postOnly() import yappi if not yappi.is_running(): yappi.start() try: yappi.set_clock_type("cpu") except: logging.exception("CPU time profiling not supported") time.sleep(0.5) messagebus.postMessage("/system/settings/activatedprofiler", pages.getAcessingUser()) raise cherrypy.HTTPRedirect("/settings/profiler")
def start(self): # Lazy import so we do not effect runtime environment if profiling is # not used. global yappi import yappi # pylint: disable=import-error # yappi start semantics are a bit too liberal, returning success if # yappi is already started, happily having two different code paths # that thinks they own the single process profiler. if yappi.is_running(): raise UsageError('CPU profiler is already running') logging.info("Starting CPU profiling") yappi.set_clock_type(self.clock) yappi.start(builtins=self.builtins, profile_threads=self.threads)
def start(self): # Lazy import so we do not effect runtime environment if profiling is # not used. global yappi import yappi # yappi start semantics are a bit too liberal, returning success if # yappi is already started, happily having two different code paths # that thinks they own the single process profiler. if yappi.is_running(): raise UsageError('CPU profiler is already running') logging.info("Starting CPU profiling") yappi.set_clock_type(self.clock) yappi.start(builtins=self.builtins, profile_threads=self.threads)
def profile(): import yappi, cStringIO if not yappi.is_running(): yappi.start() s = yappi.get_func_stats() s = s.sort(flask.request.args.get('sort', 'tsub')) f = cStringIO.StringIO() s.print_all(f, columns={ 0: ('name', 80), 1: ('ncall', 20), 2: ('tsub', 8), 3: ('ttot', 8) }) if flask.request.args.get('clear', 'N').lower() \ in {'y', 'yes', 'true', 'on'}: yappi.clear_stats() return '<pre>\n' + f.getvalue() + '</pre>\n'
def index(self, start=None, stop=None, clear=None, show=None): start = yappi.is_running() or start self.stop() result = None try: if show: stats = self.get_stats() else: stats = None if clear: self.clear() result = GenericView('profiler', {'stats': stats}).render() finally: if start: self.start() return result
def _handle_sigusr2(sig, stack): ''' Signal handler for SIGUSR2, only available on Unix-like systems ''' try: import yappi except ImportError: return if yappi.is_running(): yappi.stop() filename = 'callgrind.salt-{0}-{1}'.format(int(time.time()), os.getpid()) destfile = os.path.join(tempfile.gettempdir(), filename) yappi.get_func_stats().save(destfile, type='CALLGRIND') if sys.stderr.isatty(): sys.stderr.write('Saved profiling data to: {0}\n'.format(destfile)) yappi.clear_stats() else: if sys.stderr.isatty(): sys.stderr.write('Profiling started\n') yappi.start()
def _handle_sigusr2(sig, stack): """ Signal handler for SIGUSR2, only available on Unix-like systems """ try: import yappi except ImportError: return if yappi.is_running(): yappi.stop() filename = "callgrind.salt-{0}-{1}".format(int(time.time()), os.getpid()) destfile = os.path.join(tempfile.gettempdir(), filename) yappi.get_func_stats().save(destfile, type="CALLGRIND") if sys.stderr.isatty(): sys.stderr.write("Saved profiling data to: {0}\n".format(destfile)) yappi.clear_stats() else: if sys.stderr.isatty(): sys.stderr.write("Profiling started\n") yappi.start()
def setUp(self): if yappi.is_running(): yappi.stop() yappi.clear_stats() yappi.set_clock_type('cpu') # reset to default clock type
def is_running(self): return yappi.is_running()
def __enter__(self): """ Start the yappi profiler if it is not running. """ if not yappi.is_running(): yappi.start(self._builtins)
def is_profiler_running(): """Return True if the profiler is running.""" # GET /profiler return yappi.is_running()
def is_running(): with _lock: return yappi and yappi.is_running()
def get(self, req, resp): if yappi is None: raise http.Error(http.NOT_FOUND, "yappi is not installed") msg = {"running": yappi.is_running()} resp.send_json(msg)