|
| 1 | +"""Tests for codetiming.Timer |
| 2 | +
|
| 3 | +Based on the Pytest test runner |
| 4 | +""" |
| 5 | +# Standard library imports |
| 6 | +import re |
| 7 | + |
| 8 | +# Third party imports |
| 9 | +import pytest |
| 10 | + |
| 11 | +# Codetiming imports |
| 12 | +from codetiming import Timer, TimerError |
| 13 | + |
| 14 | + |
| 15 | +# |
| 16 | +# Test functions |
| 17 | +# |
| 18 | +TIME_PREFIX = "Wasted time:" |
| 19 | +TIME_MESSAGE = f"{TIME_PREFIX} {{:.4f}} seconds" |
| 20 | +RE_TIME_MESSAGE = re.compile(TIME_PREFIX + r" 0\.\d{4} seconds") |
| 21 | + |
| 22 | + |
| 23 | +@Timer(text=TIME_MESSAGE) |
| 24 | +def timewaster(num): |
| 25 | + """Just waste a little bit of time""" |
| 26 | + sum(n ** 2 for n in range(num)) |
| 27 | + |
| 28 | + |
| 29 | +@Timer(name="accumulator", text=TIME_MESSAGE) |
| 30 | +def accumulated_timewaste(num): |
| 31 | + """Just waste a little bit of time""" |
| 32 | + sum(n ** 2 for n in range(num)) |
| 33 | + |
| 34 | + |
| 35 | +class CustomLogger: |
| 36 | + """Simple class used to test custom logging capabilities in Timer""" |
| 37 | + |
| 38 | + def __init__(self): |
| 39 | + self.messages = "" |
| 40 | + |
| 41 | + def __call__(self, message): |
| 42 | + self.messages += message |
| 43 | + |
| 44 | + |
| 45 | +# |
| 46 | +# Tests |
| 47 | +# |
| 48 | +def test_timer_as_decorator(capsys): |
| 49 | + """Test that decorated function prints timing information""" |
| 50 | + timewaster(1000) |
| 51 | + stdout, stderr = capsys.readouterr() |
| 52 | + assert RE_TIME_MESSAGE.match(stdout) |
| 53 | + assert stdout.count("\n") == 1 |
| 54 | + assert stderr == "" |
| 55 | + |
| 56 | + |
| 57 | +def test_timer_as_context_manager(capsys): |
| 58 | + """Test that timed context prints timing information""" |
| 59 | + with Timer(text=TIME_MESSAGE): |
| 60 | + sum(n ** 2 for n in range(1000)) |
| 61 | + stdout, stderr = capsys.readouterr() |
| 62 | + assert RE_TIME_MESSAGE.match(stdout) |
| 63 | + assert stdout.count("\n") == 1 |
| 64 | + assert stderr == "" |
| 65 | + |
| 66 | + |
| 67 | +def test_explicit_timer(capsys): |
| 68 | + """Test that timed section prints timing information""" |
| 69 | + t = Timer(text=TIME_MESSAGE) |
| 70 | + t.start() |
| 71 | + sum(n ** 2 for n in range(1000)) |
| 72 | + t.stop() |
| 73 | + stdout, stderr = capsys.readouterr() |
| 74 | + assert RE_TIME_MESSAGE.match(stdout) |
| 75 | + assert stdout.count("\n") == 1 |
| 76 | + assert stderr == "" |
| 77 | + |
| 78 | + |
| 79 | +def test_error_if_timer_not_running(): |
| 80 | + """Test that timer raises error if it is stopped before started""" |
| 81 | + t = Timer(text=TIME_MESSAGE) |
| 82 | + with pytest.raises(TimerError): |
| 83 | + t.stop() |
| 84 | + |
| 85 | + |
| 86 | +def test_access_timer_object_in_context(capsys): |
| 87 | + """Test that we can access the timer object inside a context""" |
| 88 | + with Timer(text=TIME_MESSAGE) as t: |
| 89 | + assert isinstance(t, Timer) |
| 90 | + assert t.text.startswith(TIME_PREFIX) |
| 91 | + _, _ = capsys.readouterr() # Do not print log message to standard out |
| 92 | + |
| 93 | + |
| 94 | +def test_custom_logger(): |
| 95 | + """Test that we can use a custom logger""" |
| 96 | + logger = CustomLogger() |
| 97 | + with Timer(text=TIME_MESSAGE, logger=logger): |
| 98 | + sum(n ** 2 for n in range(1000)) |
| 99 | + assert RE_TIME_MESSAGE.match(logger.messages) |
| 100 | + |
| 101 | + |
| 102 | +def test_timer_without_text(capsys): |
| 103 | + """Test that timer with logger=None does not print anything""" |
| 104 | + with Timer(logger=None): |
| 105 | + sum(n ** 2 for n in range(1000)) |
| 106 | + |
| 107 | + stdout, stderr = capsys.readouterr() |
| 108 | + assert stdout == "" |
| 109 | + assert stderr == "" |
| 110 | + |
| 111 | + |
| 112 | +def test_accumulated_decorator(capsys): |
| 113 | + """Test that decorated timer can accumulate""" |
| 114 | + accumulated_timewaste(1000) |
| 115 | + accumulated_timewaste(1000) |
| 116 | + |
| 117 | + stdout, stderr = capsys.readouterr() |
| 118 | + lines = stdout.strip().split("\n") |
| 119 | + assert len(lines) == 2 |
| 120 | + assert RE_TIME_MESSAGE.match(lines[0]) |
| 121 | + assert RE_TIME_MESSAGE.match(lines[1]) |
| 122 | + assert stderr == "" |
| 123 | + |
| 124 | + |
| 125 | +def test_accumulated_context_manager(capsys): |
| 126 | + """Test that context manager timer can accumulate""" |
| 127 | + t = Timer(name="accumulator", text=TIME_MESSAGE) |
| 128 | + with t: |
| 129 | + sum(n ** 2 for n in range(1000)) |
| 130 | + with t: |
| 131 | + sum(n ** 2 for n in range(1000)) |
| 132 | + |
| 133 | + stdout, stderr = capsys.readouterr() |
| 134 | + lines = stdout.strip().split("\n") |
| 135 | + assert len(lines) == 2 |
| 136 | + assert RE_TIME_MESSAGE.match(lines[0]) |
| 137 | + assert RE_TIME_MESSAGE.match(lines[1]) |
| 138 | + assert stderr == "" |
| 139 | + |
| 140 | + |
| 141 | +def test_accumulated_explicit_timer(capsys): |
| 142 | + """Test that explicit timer can accumulate""" |
| 143 | + t = Timer(name="accumulated_explicit_timer", text=TIME_MESSAGE) |
| 144 | + total = 0 |
| 145 | + t.start() |
| 146 | + sum(n ** 2 for n in range(1000)) |
| 147 | + total += t.stop() |
| 148 | + t.start() |
| 149 | + sum(n ** 2 for n in range(1000)) |
| 150 | + total += t.stop() |
| 151 | + |
| 152 | + stdout, stderr = capsys.readouterr() |
| 153 | + lines = stdout.strip().split("\n") |
| 154 | + assert len(lines) == 2 |
| 155 | + assert RE_TIME_MESSAGE.match(lines[0]) |
| 156 | + assert RE_TIME_MESSAGE.match(lines[1]) |
| 157 | + assert stderr == "" |
| 158 | + assert total == Timer.timers["accumulated_explicit_timer"] |
| 159 | + |
| 160 | + |
| 161 | +def test_error_if_restarting_running_timer(): |
| 162 | + """Test that restarting a running timer raises an error""" |
| 163 | + t = Timer(text=TIME_MESSAGE) |
| 164 | + t.start() |
| 165 | + with pytest.raises(TimerError): |
| 166 | + t.start() |
0 commit comments