2018-03-04 12:25:25 -08:00
|
|
|
import numpy as np
|
2019-12-30 15:27:32 -05:00
|
|
|
import time
|
2017-12-06 17:51:57 -08:00
|
|
|
|
|
|
|
|
2020-01-02 17:42:13 -08:00
|
|
|
class TimerStat:
|
2017-12-06 17:51:57 -08:00
|
|
|
"""A running stat for conveniently logging the duration of a code block.
|
|
|
|
|
|
|
|
Example:
|
2018-03-04 12:25:25 -08:00
|
|
|
wait_timer = TimerStat()
|
2017-12-06 17:51:57 -08:00
|
|
|
with wait_timer:
|
|
|
|
ray.wait(...)
|
|
|
|
|
|
|
|
Note that this class is *not* thread-safe.
|
|
|
|
"""
|
|
|
|
|
2018-03-04 12:25:25 -08:00
|
|
|
def __init__(self, window_size=10):
|
|
|
|
self._window_size = window_size
|
|
|
|
self._samples = []
|
|
|
|
self._units_processed = []
|
2017-12-06 17:51:57 -08:00
|
|
|
self._start_time = None
|
2018-03-04 12:25:25 -08:00
|
|
|
self._total_time = 0.0
|
|
|
|
self.count = 0
|
2017-12-06 17:51:57 -08:00
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
assert self._start_time is None, "concurrent updates not supported"
|
2017-12-07 17:03:58 -08:00
|
|
|
self._start_time = time.time()
|
2017-12-06 17:51:57 -08:00
|
|
|
|
|
|
|
def __exit__(self, type, value, tb):
|
|
|
|
assert self._start_time is not None
|
2018-03-04 12:25:25 -08:00
|
|
|
time_delta = time.time() - self._start_time
|
|
|
|
self.push(time_delta)
|
2017-12-06 17:51:57 -08:00
|
|
|
self._start_time = None
|
2018-03-04 12:25:25 -08:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def mean(self):
|
|
|
|
return np.mean(self._samples)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def mean_units_processed(self):
|
|
|
|
return float(np.mean(self._units_processed))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def mean_throughput(self):
|
|
|
|
time_total = sum(self._samples)
|
|
|
|
if not time_total:
|
|
|
|
return 0.0
|
|
|
|
return sum(self._units_processed) / time_total
|