-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtiming_test.py
More file actions
89 lines (71 loc) · 2.17 KB
/
timing_test.py
File metadata and controls
89 lines (71 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Timing tests."""
import logging
import time
from unittest import mock
from absl.testing import absltest
from absl.testing import parameterized
from pathwaysutils.debug import timing
class TimingTest(parameterized.TestCase):
def test_timer_context_manager(self):
self.enter_context(
mock.patch.object(
time,
"time",
side_effect=[1, 8.9],
autospec=True,
)
)
with timing.Timer("test_timer") as timer:
pass
self.assertEqual(timer.name, "test_timer")
self.assertEqual(timer.start, 1)
self.assertEqual(timer.stop, 8.9)
self.assertEqual(timer.duration, 7.9)
self.assertEqual(str(timer), "test_timer elapsed 7.9000 seconds.")
def test_timeit_log(self):
@timing.timeit
def my_function():
pass
self.enter_context(
mock.patch.object(
time,
"time",
side_effect=[1, 8.9, 0], # Third time is used for logging.
autospec=True,
)
)
with self.assertLogs(timing._logger, logging.DEBUG) as log_output:
my_function()
self.assertEqual(
log_output.output,
[
"DEBUG:pathwaysutils.debug.timing:my_function"
" elapsed 7.9000 seconds."
],
)
def test_timeit_return_value(self):
@timing.timeit
def my_function():
return "test"
self.assertEqual(my_function(), "test")
def test_timeit_exception(self):
@timing.timeit
def my_function():
raise ValueError("test")
with self.assertRaises(ValueError):
my_function()
if __name__ == "__main__":
absltest.main()