/
ioloop_util.py
49 lines (35 loc) · 1.65 KB
/
ioloop_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import time
class UtilizationCollector(object):
"""Collects stats for overall callback durations"""
def __init__(self, monitor):
self.monitor = monitor
self.monitor.stats_init = False
def start(self):
self.original_run_callback = self.monitor.io_loop._run_callback
self.original_add_handler = self.monitor.io_loop.add_handler
def run_timed_callback(callback):
last_start_time = time.time()
result = self.original_run_callback(callback)
duration = (time.time() - last_start_time)
self.monitor.count('callback_duration', duration)
return result
def add_timed_handler(fd, handler, events):
def timed_handler(*args, **kwargs):
profiler_enabled = False
start_time = time.time()
if self.monitor.stats_init == True:
self.monitor.profiler.enable()
profiler_enabled = True
result = handler(*args, **kwargs)
if profiler_enabled == True:
self.monitor.profiler.disable()
profiler_enabled = False
duration = (time.time() - start_time)
self.monitor.count('callback_duration', duration)
return result
self.original_add_handler(fd, timed_handler, events)
self.monitor.io_loop.add_handler = add_timed_handler
self.monitor.io_loop._run_callback = run_timed_callback
def stop(self):
self.monitor.io_loop._run_callback = self.original_run_callback
self.monitor.io_loop.add_handler = self.original_add_handler