Skip to content

StopWatch implementation

Roberto Prevato edited this page Sep 21, 2019 · 1 revision

essentials includes an implementation of StopWatch class, edited from: Python Cookbook, 3rd Edition by Brian K. Jones, David Beazley.

import time
import pytest
from essentials.diagnostics import StopWatch


def test_stopwatch():
    a = StopWatch()
    a.start()

    time.sleep(0.01)

    a.stop()
    assert pytest.approx(0.01, .1) == a.elapsed_s
    assert pytest.approx(a.elapsed_ms, .1) == a.elapsed_s * 1000


def test_stopwatch_with_context_manager():
    with StopWatch() as a:
        time.sleep(0.01)

    assert pytest.approx(0.01, .1) == a.elapsed_s
    assert pytest.approx(a.elapsed_ms, .1) == a.elapsed_s * 1000