class ResourceHistory(object): """ Simple class to contain a RingBuffer (fixed-length FIFO) history of resource samples, with the mapping: timestamp => ({process_status => (process_sample, number_of_procs)}, disk_usage_in_bytes) """ def __init__(self, maxlen, initialize=True): if not maxlen >= 1: raise ValueError("maxlen must be greater than 0") self._maxlen = maxlen self._values = RingBuffer(maxlen, None) if initialize: self.add(time.time(), ResourceMonitorBase.FullResourceResult({}, 0)) def add(self, timestamp, value): """Store a new resource sample corresponding to the given timestamp""" if self._values and not timestamp >= self._values[-1][0]: raise ValueError("Refusing to add timestamp in the past!") self._values.append((timestamp, value)) def get(self, timestamp): """Get the resource sample nearest to the given timestamp""" closest = min(bisect_left(self._values, (timestamp, None)), len(self) - 1) return self._values[closest] def __iter__(self): return iter(self._values) def __len__(self): return len(self._values) def __repr__(self): return 'ResourceHistory(%s)' % ', '.join( [str(r) for r in self._values])
class ResourceHistory(object): """Simple class to contain a RingBuffer (fixed-length FIFO) history of resource samples, with the mapping: timestamp => (number_of_procs, ProcessSample, disk_usage_in_bytes) """ def __init__(self, maxlen, initialize=True): if not maxlen >= 1: raise ValueError("maxlen must be greater than 0") self._maxlen = maxlen self._values = RingBuffer(maxlen, None) if initialize: self.add(time.time(), ResourceMonitorBase.ResourceResult(0, ProcessSample.empty(), 0)) def add(self, timestamp, value): """Store a new resource sample corresponding to the given timestamp""" if self._values and not timestamp >= self._values[-1][0]: raise ValueError("Refusing to add timestamp in the past!") self._values.append((timestamp, value)) def get(self, timestamp): """Get the resource sample nearest to the given timestamp""" closest = min(bisect_left(self._values, (timestamp, None)), len(self) - 1) return self._values[closest] def __iter__(self): return iter(self._values) def __len__(self): return len(self._values) def __repr__(self): return 'ResourceHistory(%s)' % ', '.join([str(r) for r in self._values])
def __init__(self, maxlen, initialize=True): if not maxlen >= 1: raise ValueError("maxlen must be greater than 0") self._maxlen = maxlen self._values = RingBuffer(maxlen, None) if initialize: self.add(time.time(), ResourceMonitorBase.ResourceResult(0, ProcessSample.empty(), 0))
def test_append(): r = RingBuffer(5) for i in xrange(0, 5): r.append(i) assert (r[0], r[1], r[2], r[3], r[4]) == (0, 1, 2, 3, 4) for i in xrange(5, 10): r.append(i) assert (r[0], r[1], r[2], r[3], r[4]) == (5, 6, 7, 8, 9)
def test_bad_operations(): with pytest.raises(ValueError): RingBuffer(0) r = RingBuffer() with pytest.raises(IndexError): r[1] with pytest.raises(IndexError): r[-1] r.append(1) with pytest.raises(RingBuffer.InvalidOperation): del r[0]
def test_circularity(): r = RingBuffer(3) r.append(1) r.append(2) r.append(3) assert 1 in r assert (r[0], r[3], r[6], r[-3]) == (1, 1, 1, 1) r.append(4) assert 1 not in r assert (r[0], r[3], r[6], r[-3]) == (2, 2, 2, 2)