Source code for buildtest.utils.timer

import time

[docs]class TimerError(Exception): """A custom exception used to report errors in use of Timer class"""
[docs]class Timer: def __init__(self): self._start_time = None self._duration = 0
[docs] def start(self): """Start a new timer""" if self._start_time is not None: raise TimerError("Timer is running. Use .stop() to stop it") self._start_time = time.perf_counter()
[docs] def stop(self): """Stop the timer, and report the elapsed time""" if self._start_time is None: raise TimerError("Timer is not running. Use .start() to start it") self._duration += time.perf_counter() - self._start_time self._start_time = None
# return elapsed_time
[docs] def duration(self): return round(self._duration, 3)