def test_record_multiple(self): t = Timer(TimerTest.tid) t.record(42) t.record(2) t.record(7) self.assertEqual(t.count(), 3) self.assertEqual(t.total_time(), 51)
def test_measure(self): t = Timer(TimerTest.tid) t.record(42) t.record(2) t.record(7) ms = t._measure() def get_stat(s): return ms[TimerTest.tid.with_stat(s)] self.assertEqual(len(ms), 4) self.assertEqual(get_stat('count'), 3) self.assertEqual(get_stat('totalTime'), 51) self.assertEqual(get_stat('max'), 42) self.assertEqual(get_stat('totalOfSquares'), 42**2 + 2**2 + 7**2) self.assertEqual(t.count(), 0) self.assertEqual(t.total_time(), 0)
def test_count_and_total_time(self): """Avoid breaking the API.""" t = Timer(self.tid, writer=MemoryWriter()) self.assertTrue(t._writer.is_empty()) t.record(42) self.assertEqual(0, t.count()) self.assertEqual(0, t.total_time())
def timer(self, name, tags=None): return self._new_meter(name, tags, lambda id: Timer(id, self._clock), Timer, self.noopTimer)
def test_stopwatch(self): clock = ManualClock() t = Timer(self.tid, clock=clock, meter_type="T", writer=MemoryWriter()) with t.stopwatch(): clock.set_monotonic_time(42) self.assertEqual("T:test:42", t._writer.last_line())
def test_record_zero(self): t = Timer(self.tid, meter_type="T", writer=MemoryWriter()) t.record(0) self.assertEqual("T:test:0", t._writer.last_line())
def test_record_negative(self): t = Timer(self.tid, meter_type="T", writer=MemoryWriter()) t.record(-42) self.assertTrue(t._writer.is_empty())
def test_record(self): t = Timer(self.tid, meter_type="T", writer=MemoryWriter()) self.assertTrue(t._writer.is_empty()) t.record(42) self.assertEqual("T:test:42", t._writer.last_line())
def test_invalid_meter_type(self): with self.assertRaises(ValueError): Timer(self.tid, meter_type='x')
def pct_timer(self, name: str, tags: Optional[dict] = None) -> Timer: return Timer(self._new_meter(name, tags), clock=self._clock, meter_type="T", writer=self._writer)
def test_record(self): t = Timer(TimerTest.tid) t.record(42) self.assertEqual(t.count(), 1) self.assertEqual(t.total_time(), 42)
def test_record_negative(self): t = Timer(TimerTest.tid) t.record(-42) self.assertEqual(t.count(), 0) self.assertEqual(t.total_time(), 0)