File size: 1,877 Bytes
cf6a8b4 | 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | import time
class _Timer:
"""A running stat for conveniently logging the duration of a code block.
Example:
wait_timer = TimerStat()
with wait_timer:
ray.wait(...)
Note that this class is *not* thread-safe.
"""
def __init__(self, window_size=10):
self._window_size = window_size
self._samples = []
self._units_processed = []
self._start_time = None
self._total_time = 0.0
self.count = 0
def __enter__(self):
assert self._start_time is None, "concurrent updates not supported"
self._start_time = time.time()
def __exit__(self, exc_type, exc_value, tb):
assert self._start_time is not None
time_delta = time.time() - self._start_time
self.push(time_delta)
self._start_time = None
def push(self, time_delta):
self._samples.append(time_delta)
if len(self._samples) > self._window_size:
self._samples.pop(0)
self.count += 1
self._total_time += time_delta
def push_units_processed(self, n):
self._units_processed.append(n)
if len(self._units_processed) > self._window_size:
self._units_processed.pop(0)
def has_units_processed(self):
return len(self._units_processed) > 0
@property
def mean(self):
if len(self._samples) == 0:
return 0.0
return float(sum(self._samples)) / len(self._samples)
@property
def mean_units_processed(self):
if len(self._units_processed) == 0:
return 0.0
return float(sum(self._units_processed)) / len(self._units_processed)
@property
def mean_throughput(self):
time_total = float(sum(self._samples))
if not time_total:
return 0.0
return float(sum(self._units_processed)) / time_total
|