##// END OF EJS Templates
deps: bumped webob==1.8.8
deps: bumped webob==1.8.8

File last commit:

r1152:a0c49580 default
r1305:783b11b6 default
Show More
timer.py
66 lines | 2.0 KiB | text/x-python | PythonLexer
application: added statsd client for sending usage statistics.
r920 import functools
py3: fixes for python3
r1045 from time import perf_counter as time_now
application: added statsd client for sending usage statistics.
r920
def safe_wraps(wrapper, *args, **kwargs):
"""Safely wraps partial functions."""
while isinstance(wrapper, functools.partial):
wrapper = wrapper.func
return functools.wraps(wrapper, *args, **kwargs)
lint: auto-fixes
r1152 class Timer:
application: added statsd client for sending usage statistics.
r920 """A context manager/decorator for statsd.timing()."""
statsd: synced client with rhodecode
r1027 def __init__(self, client, stat, rate=1, tags=None, use_decimals=True, auto_send=True):
application: added statsd client for sending usage statistics.
r920 self.client = client
self.stat = stat
self.rate = rate
metrics: use new statsd client logic, and start gathering new metrics
r1005 self.tags = tags
application: added statsd client for sending usage statistics.
r920 self.ms = None
self._sent = False
self._start_time = None
statsd: synced client with rhodecode
r1027 self.use_decimals = use_decimals
self.auto_send = auto_send
application: added statsd client for sending usage statistics.
r920
def __call__(self, f):
"""Thread-safe timing function decorator."""
@safe_wraps(f)
def _wrapped(*args, **kwargs):
start_time = time_now()
try:
return f(*args, **kwargs)
finally:
elapsed_time_ms = 1000.0 * (time_now() - start_time)
statsd: synced client with rhodecode
r1027 self.client.timing(self.stat, elapsed_time_ms, self.rate, self.tags, self.use_decimals)
self._sent = True
application: added statsd client for sending usage statistics.
r920 return _wrapped
def __enter__(self):
return self.start()
def __exit__(self, typ, value, tb):
statsd: synced client with rhodecode
r1027 self.stop(send=self.auto_send)
application: added statsd client for sending usage statistics.
r920
def start(self):
self.ms = None
self._sent = False
self._start_time = time_now()
return self
def stop(self, send=True):
if self._start_time is None:
raise RuntimeError('Timer has not started.')
dt = time_now() - self._start_time
self.ms = 1000.0 * dt # Convert to milliseconds.
if send:
self.send()
return self
def send(self):
if self.ms is None:
raise RuntimeError('No data recorded.')
if self._sent:
raise RuntimeError('Already sent data.')
self._sent = True
statsd: synced client with rhodecode
r1027 self.client.timing(self.stat, self.ms, self.rate, self.tags, self.use_decimals)