def basic_example(): print("[basic_example]") timer = Stopwatch() sleep(0.1) timer.log_elapsed_time() # 0:00:00 timer.start() sleep(0.1) timer.log_elapsed_time() # 0:00:00.1 sleep(0.1) timer.pause() timer.log_elapsed_time() # 0:00:00.2 sleep(0.1) timer.log_elapsed_time() # 0:00:00.2 timer.split() # 0:00:00.2 timer.log_elapsed_time() # 0:00:00 print("timer.get_cumulative_elapsed_time():", timer.get_cumulative_elapsed_time()) # 0:00:00.2 sleep(0.1) timer.start() sleep(0.1) timer.log_elapsed_time() # 0:00:00.1 print("timer.get_cumulative_elapsed_time():", timer.get_cumulative_elapsed_time()) # 0:00:00.3 timer.split() # 0:00:00.1 sleep(0.1) timer.pause() timer.split() # 0:00:00.1 print("timer.get_cumulative_elapsed_time():", timer.get_cumulative_elapsed_time()) # 0:00:00.4 print("timer.split_elapsed_time:", [str(delta) for delta in timer.split_elapsed_time]) # [0:00:00.2, 0:00:00.1, 0:00:00.1] timer.reset() timer.log_elapsed_time() # 0:00:00 print("timer.get_cumulative_elapsed_time():", timer.get_cumulative_elapsed_time()) # 0:00:00 print("timer.split_elapsed_time:", timer.split_elapsed_time) # [] sleep(0.1) timer.start() sleep(0.1) timer.log_elapsed_time() # 0:00:00.1
def test_low_level_api(): timer = Stopwatch() sleep(0.1) assert timer.get_elapsed_time() == datetime.timedelta() timer.start() sleep(0.1) assert_timedelta_close_seconds(timer.get_elapsed_time(), 0.1) sleep(0.1) timer.pause() assert_timedelta_close_seconds(timer.get_elapsed_time(), 0.2) sleep(0.1) assert_timedelta_close_seconds(timer.get_elapsed_time(), 0.2) timer.split() # 0:00:00.2 assert timer.get_elapsed_time() == datetime.timedelta() assert_timedelta_close_seconds(timer.get_cumulative_elapsed_time(), 0.2) sleep(0.1) timer.start() sleep(0.1) assert_timedelta_close_seconds(timer.get_elapsed_time(), 0.1) assert_timedelta_close_seconds(timer.get_cumulative_elapsed_time(), 0.3) timer.split() # 0:00:00.1 sleep(0.1) timer.pause() timer.split() # 0:00:00.1 assert_timedelta_close_seconds(timer.get_cumulative_elapsed_time(), 0.4) assert_timedelta_close_seconds(timer.split_elapsed_time[0], 0.2) assert_timedelta_close_seconds(timer.split_elapsed_time[1], 0.1) assert_timedelta_close_seconds(timer.split_elapsed_time[2], 0.1) timer.reset() assert timer.get_elapsed_time() == datetime.timedelta() assert timer.get_cumulative_elapsed_time() == datetime.timedelta() assert timer.split_elapsed_time == [] sleep(0.1) timer.start() sleep(0.1) assert_timedelta_close_seconds(timer.get_elapsed_time(), 0.1)