def profile(duration=60, pf=None, trace_obj=None): """ profile """ import gevent_profiler if gevent_profiler._attach_expiration is not None: return False, 'profile has running!\n' start_time = strftime('%y%m%d-%H%M%S') if pf is None: save_path = os.environ.get('LOG_PATH', None) if not save_path: save_path = os.path.join(os.environ['ROOT_PATH'], 'log') pf = os.path.join(save_path, 'profile-%s.profile' % start_time) gevent_profiler.set_summary_output(pf) gevent_profiler._attach_duration = duration if trace_obj: gevent_profiler.set_trace_output(trace_obj) gevent_profiler.enable_trace_output(True) else: gevent_profiler.set_trace_output(None) gevent_profiler.enable_trace_output(False) gevent_profiler.attach() return True, 'profile start:%s\n' % start_time
def enableProfiler(signum, config): """ Enables the profiler on specified signal """ try: # If the profiler is available on the system then enable it import gevent_profiler if config.has_key('summaryOutput'): gevent_profiler.set_summary_output(config['summaryOutput']) if config.has_key('statsOutput'): gevent_profiler.set_stats_output(config['statsOutput']) if config.has_key('traceOutput'): gevent_profiler.set_trace_output(config['traceOutput']) if config.has_key('printPercentage'): gevent_profiler.print_percentages(config['printPercentage']) if config.has_key('countTimeBlocking'): gevent_profiler.time_blocking(config['countTimeBlocking']) gevent_profiler.attach_on_signal(signum=signum, duration=config['duration']) logging.getLogger().info("The profiler is enabled and waiting on signal %s." % signum) except Exception as ex: logging.getLogger().warn("Failed enabling the profiler: %s" % ex)
def begin(self): ts = time.time() gevent_profiler.set_stats_output("nose-gevent-profiler-%s-stats.txt" % ts) gevent_profiler.set_summary_output( "nose-gevent-profiler-%s-summary.txt" % ts) gevent_profiler.set_trace_output(None)
def GET(self): web.header('Content-Type', 'text/html') params = web.input(_sheep_app_server=None, _sheep_profile_count=None, _sheep_pid=None) if not web.ctx.env.get('QUERY_STRING', None): return FORM_HTML % socket.gethostname() count = params._sheep_profile_count backend = params._sheep_app_server pid = params._sheep_pid if not (count and backend): return '''params invaild''' count = int(count) if not pid: pid = os.getpid() raise web.seeother("/_sheep/profile/?_sheep_profile_count=%d&_sheep_app_server=%s&_sheep_pid=%d" % \ (count, backend, pid)) else: pid = int(pid) html = FRESH_HTML num_file = os.path.join('/tmp','%s.%d.counter' % (web.ctx.env['SERVER_NAME'], pid)) stat_file = os.path.join('/tmp', '%s.%d.prof' % (web.ctx.env['SERVER_NAME'], pid)) now = os.path.getsize(num_file) if os.path.exists(num_file) else 0 html = html % (now, count, count, backend.encode('utf8'), pid) if not getattr(SHEEPApplication, 'old_call', None): setattr(SHEEPApplication, 'old_call', SHEEPApplication.__call__) if not os.path.exists(num_file) and os.path.exists(stat_file): return output(stat_file) elif not os.path.exists(num_file): with open(num_file, 'w+') as f: f.write('') gevent_profiler.set_stats_output(stat_file) gevent_profiler.set_trace_output(None) def monkey_call(obj, environ, start_response): if os.path.exists(num_file): with open(num_file, 'a') as f: f.write('1') return obj.old_call(environ, start_response) SHEEPApplication.__call__ = monkey_call gevent_profiler.attach() elif os.path.getsize(num_file) >= count: os.remove(num_file) try: gevent_profiler.detach() except: pass SHEEPApplication.__call__ = SHEEPApplication.old_call return output(stat_file) return html
def start_profiler(prefix, pcts=False): """ Start the gevent-profiler, outputting to the passed prefix-stats and prefix-summary.txt. You must call stop_profiler at a later point. The profiler is not safe to run twice at once as it keeps global state internally. @param prefix The prefix to use when naming the output files. @param pcts Whether the profiler should add percentages as well. """ gevent_profiler.set_stats_output('%s-stats.txt' % prefix) gevent_profiler.set_summary_output('%s-summary.txt' % prefix) gevent_profiler.print_percentages(pcts) gevent_profiler.set_trace_output(None) #'%s-trace.txt' % prefix) @TODO make optional gevent_profiler.attach()
def __init__(self, datadir: str) -> None: # create a new file every time instead of overwritting the latest profiling summary_file = "{:%Y%m%d_%H%M}_profile_summary".format(datetime.now()) stats_file = "{:%Y%m%d_%H%M}_profile_stats".format(datetime.now()) summary_path = os.path.join(datadir, summary_file) stats_path = os.path.join(datadir, stats_file) gevent_profiler.set_trace_output(None) gevent_profiler.set_summary_output(summary_path) gevent_profiler.set_stats_output(stats_path) GreenletProfiler.set_clock_type("cpu") GreenletProfiler.start() # gevent_profiler.attach() self.datadir = datadir
def start_profiler(prefix, pcts=False): """ Start the gevent-profiler, outputting to the passed prefix-stats and prefix-summary.txt. You must call stop_profiler at a later point. The profiler is not safe to run twice at once as it keeps global state internally. @param prefix The prefix to use when naming the output files. @param pcts Whether the profiler should add percentages as well. """ gevent_profiler.set_stats_output('%s-stats.txt' % prefix) gevent_profiler.set_summary_output('%s-summary.txt' % prefix) gevent_profiler.print_percentages(pcts) gevent_profiler.set_trace_output( None) #'%s-trace.txt' % prefix) @TODO make optional gevent_profiler.attach()
def start(self): if self.profiling: return # create a new file every time instead of overwritting the latest profiling summary_file = '{:%Y%m%d_%H%M}_profile_summary'.format(datetime.now()) stats_file = '{:%Y%m%d_%H%M}_profile_stats'.format(datetime.now()) summary_path = os.path.join(self.datadir, summary_file) stats_path = os.path.join(self.datadir, stats_file) gevent_profiler.set_trace_output(None) gevent_profiler.set_summary_output(summary_path) gevent_profiler.set_stats_output(stats_path) GreenletProfiler.set_clock_type('cpu') GreenletProfiler.start() # gevent_profiler.attach() self.profiling = True
from session.regions import run_region_reloader gevent.spawn(run_region_reloader) try: from uwsgidecorators import postfork except ImportError: import gevent.monkey gevent.monkey.patch_all() init() else: init = postfork(init) from .app import application if __name__ == '__main__': import settings import gevent_profiler import signal from gevent.pywsgi import WSGIServer gevent_profiler.attach_on_signal(signum=signal.SIGUSR1, duration=30) gevent_profiler.set_stats_output('stats.txt') gevent_profiler.set_summary_output('summary.txt') gevent_profiler.set_trace_output('trace.txt') logger.info('listening %s:%d', settings.SESSION['host'], settings.SESSION['port']) WSGIServer((settings.SESSION['host'], settings.SESSION['port']), application).serve_forever()
def __enter__(self): gevent_profiler.set_stats_output(self.stats) gevent_profiler.set_summary_output(self.summary) gevent_profiler.set_trace_output(None) gevent_profiler.attach()
def begin(self): ts = time.time() gevent_profiler.set_stats_output("nose-gevent-profiler-%s-stats.txt" % ts) gevent_profiler.set_summary_output("nose-gevent-profiler-%s-summary.txt" % ts) gevent_profiler.set_trace_output(None)