2022-08-22 11:51:56 +08:00
|
|
|
# Copyright (c) OpenMMLab. All rights reserved.
|
|
|
|
import time
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2022-08-23 16:56:47 +08:00
|
|
|
import mmengine
|
|
|
|
|
2022-08-22 11:51:56 +08:00
|
|
|
|
|
|
|
def test_timer_init():
|
2022-08-23 16:56:47 +08:00
|
|
|
timer = mmengine.Timer(start=False)
|
2022-08-22 11:51:56 +08:00
|
|
|
assert not timer.is_running
|
|
|
|
timer.start()
|
|
|
|
assert timer.is_running
|
2022-08-23 16:56:47 +08:00
|
|
|
timer = mmengine.Timer()
|
2022-08-22 11:51:56 +08:00
|
|
|
assert timer.is_running
|
|
|
|
|
|
|
|
|
|
|
|
def test_timer_run():
|
2022-08-23 16:56:47 +08:00
|
|
|
timer = mmengine.Timer()
|
2022-08-22 11:51:56 +08:00
|
|
|
time.sleep(1)
|
|
|
|
assert abs(timer.since_start() - 1) < 1e-2
|
|
|
|
time.sleep(1)
|
|
|
|
assert abs(timer.since_last_check() - 1) < 1e-2
|
|
|
|
assert abs(timer.since_start() - 2) < 1e-2
|
2022-08-23 16:56:47 +08:00
|
|
|
timer = mmengine.Timer(False)
|
|
|
|
with pytest.raises(mmengine.TimerError):
|
2022-08-22 11:51:56 +08:00
|
|
|
timer.since_start()
|
2022-08-23 16:56:47 +08:00
|
|
|
with pytest.raises(mmengine.TimerError):
|
2022-08-22 11:51:56 +08:00
|
|
|
timer.since_last_check()
|
|
|
|
|
|
|
|
|
|
|
|
def test_timer_context(capsys):
|
2022-08-23 16:56:47 +08:00
|
|
|
with mmengine.Timer():
|
2022-08-22 11:51:56 +08:00
|
|
|
time.sleep(1)
|
|
|
|
out, _ = capsys.readouterr()
|
|
|
|
assert abs(float(out) - 1) < 1e-2
|
2022-08-23 16:56:47 +08:00
|
|
|
with mmengine.Timer(print_tmpl='time: {:.1f}s'):
|
2022-08-22 11:51:56 +08:00
|
|
|
time.sleep(1)
|
|
|
|
out, _ = capsys.readouterr()
|
|
|
|
assert out == 'time: 1.0s\n'
|