generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup: make timing work for iterables
- don't time quick functions that get called loads of times - time the traversal of an iterable, not the time to a return a value
- Loading branch information
Showing
6 changed files
with
100 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import logging | ||
import re | ||
|
||
from ingestion.utils import Stopwatch, report_time, report_time_of_iterable | ||
|
||
REPORT_REGEX = re.compile( | ||
r"start_time=\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}, " | ||
r"end_time=\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}, " | ||
r"elapsed_time=0:00:\d\d", | ||
) | ||
|
||
|
||
def test_stopwatch_generates_a_report(caplog): | ||
caplog.set_level(logging.INFO) | ||
s = Stopwatch() | ||
s.start() | ||
s.stop() | ||
s.report() | ||
|
||
messages = [r.message for r in caplog.records] | ||
assert len(messages) == 1 | ||
assert re.match( | ||
REPORT_REGEX, | ||
messages[0], | ||
) | ||
|
||
|
||
def test_report_time_generates_a_report(caplog): | ||
caplog.set_level(logging.INFO) | ||
|
||
@report_time | ||
def foo(): | ||
return 1 + 1 | ||
|
||
assert foo() == 2 | ||
|
||
messages = [r.message for r in caplog.records] | ||
assert len(messages) == 1 | ||
assert re.search( | ||
REPORT_REGEX, | ||
messages[0], | ||
) | ||
assert "function=foo, " in messages[0] | ||
|
||
|
||
def test_report_time_of_iterator(caplog): | ||
caplog.set_level(logging.INFO) | ||
|
||
@report_time_of_iterable | ||
def foo(): | ||
yield 1 | ||
yield 2 | ||
|
||
generator = foo() | ||
values = list(generator) | ||
assert values == [1, 2] | ||
|
||
messages = [r.message for r in caplog.records] | ||
assert len(messages) == 1 | ||
assert re.search( | ||
REPORT_REGEX, | ||
messages[0], | ||
) | ||
assert "function=foo, " in messages[0] |