def test_empty_multi_stopwatch(self): timers = MultiStopwatch() self.assertListEqual(timers.get_cumulative_elapsed_time(), []) with self.assertRaises(ValueError): timers.get_percentage() self.assertListEqual(timers.get_n_splits(), []) self.assertListEqual(timers.get_mean_per_split(), []) with self.assertRaises(ValueError): timers.get_statistics()
def test_multi_stopwatch_without_elapsed_time(self): timers = MultiStopwatch(2, verbose=False) self.assertListEqual( timers.get_cumulative_elapsed_time(), [datetime.timedelta()] * 2 ) with self.assertRaises(ValueError): timers.get_percentage() self.assertListEqual(timers.get_n_splits(), [0, 0]) self.assertListEqual(timers.get_mean_per_split(), [datetime.timedelta()] * 2) with self.assertRaises(ValueError): timers.get_statistics()
def test_multi_stopwatch(self): timers = MultiStopwatch(2, verbose=False) for i in range(5): for i in range(2): with timers[0]: sleep(0.1) with timers[1]: sleep(0.1) # test get_cumulative_elapsed_time() assert_timedelta_close_seconds_list( timers.get_cumulative_elapsed_time(), [1, 0.5]) # test get_percentage() for p, exp in zip(timers.get_percentage(), [2 / 3, 1 / 3]): self.assertTrue(isclose(p, exp, rel_tol=0.05)) # test get_n_splits() self.assertListEqual(timers.get_n_splits(), [10, 5]) # test get_mean_per_split() assert_timedelta_close_seconds_list( timers.get_mean_per_split(), [0.1, 0.1]) # test get_statistics() self.assertListEqual( sorted(timers.get_statistics().keys()), ['cumulative_elapsed_time', 'mean_per_split', 'n_splits', 'percentage'])
def basic_example(): timers = MultiStopwatch(2, verbose=False) for _ in range(5): for _ in range(2): with timers[0]: sleep(0.1) with timers[1]: sleep(0.1) print("get_cumulative_elapsed_time():", timers.get_cumulative_elapsed_time()) print("get_n_splits():", timers.get_n_splits()) print("get_mean_per_split():", timers.get_mean_per_split()) print("get_percentage():", timers.get_percentage()) print("get_statistics():") pprint.pprint(timers.get_statistics()) print(timers.format_statistics())