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 main(): monkey.patch_all() tasks = [] gevent_profiler.attach() g = gevent.spawn(eat_up_cpu) g.join() gevent_profiler.detach()
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 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()
parser.add_argument('--debug', action="store_true", default=False) args = parser.parse_args() return args def run(): """Main driver function""" args = parse_config() #Setup debugging output bitHopper.setup_logging(logging.DEBUG if args.debug else logging.INFO) #Setup mining port bitHopper.setup_miner(port = args.mine_port, host=args.mine_localname) #Set up the control website bitHopper.setup_control(port = args.config_port, host=args.config_localname) #Setup Custom Pools bitHopper.custom_pools() while True: gevent.sleep(100) if __name__ == "__main__": import gevent_profiler gevent_profiler.attach() run()
parser.add_argument('--debug', action="store_true", default=False) args = parser.parse_args() return args def run(): """Main driver function""" args = parse_config() #Setup debugging output bitHopper.setup_logging(logging.DEBUG if args.debug else logging.INFO) #Setup mining port bitHopper.setup_miner(port=args.mine_port, host=args.mine_localname) #Set up the control website bitHopper.setup_control(port=args.config_port, host=args.config_localname) #Setup Custom Pools bitHopper.custom_pools() while True: gevent.sleep(100) if __name__ == "__main__": import gevent_profiler gevent_profiler.attach() run()
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()