Debian Codegraph @

python/test/unit/common/test_timer.py

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 """Unit tests for timing facilities"""
2
3 # Copyright (C) 2017 Jan Blechta
4 #
5 # This file is part of DOLFIN.
6 #
7 # DOLFIN is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # DOLFIN is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
19
20 import pytest
21
22 import gc
23 import random
24 from time import sleep
25
26 from dolfin import *
27
28
29 # Seed random generator for determinism
30 random.seed(0)
31
32 def get_random_task_name():
33 """Get pseudo-random string"""
34 return hex(random.randint(0, 1e32))
35
36
37 def test_context_manager_named():
38 """Test that named Timer works as context manager"""
39 task = get_random_task_name()
40
41 # Execute task in the context manager
42 with Timer(task) as t:
43 sleep(0.05)
44 assert t.elapsed()[0] >= 0.05
45
46 # Check timing
47 t = timing(task, TimingClear.clear)
48 assert t[0] == 1
49 assert t[1] >= 0.05
50
51
52 def test_context_manager_anonymous():
53 """Test that anonymous Timer works as context manager"""
54 with Timer() as t:
55 sleep(0.05)
56 assert t.elapsed()[0] >= 0.05
57
58
59 # Test case for decorated free function
60 fun2_task = get_random_task_name()
61 @timed(fun2_task)
62 def fun2(*args, **kwargs):
63 "Foo"
64 sleep(0.05)
65 return args, kwargs
66
67 class C(object):
68 # Test case for decorated instancemethod
69 task_method2 = get_random_task_name()
70 @timed(task_method2)
71 def method2(self, *args, **kwargs):
72 "Foo"
73 sleep(0.05)
74 return args, kwargs
75
76 # Test case for decorated staticmethod
77 task_method3 = get_random_task_name()
78 @staticmethod
79 @timed(task_method3)
80 def method3(*args, **kwargs):
81 "Foo"
82 sleep(0.05)
83 return args, kwargs
84
85 # Test case for decorated classmethod
86 task_method5 = get_random_task_name()
87 @classmethod
88 @timed(task_method5)
89 def method5(cls, *args, **kwargs):
90 "Foo"
91 sleep(0.05)
92 return args, kwargs
93
94 # Testing object with decorated methods
95 o = C()
96
97
98 @pytest.mark.parametrize(("fun", "task"), [
99 (fun2, fun2_task),
100 (o.method2, o.task_method2),
101 (o.method3, o.task_method3),
102 (o.method5, o.task_method5),
103 (C.method3, C.task_method3),
104 (C.method5, C.task_method5),
105 ])
106 def test_decorator_functionality(fun, task):
107 """Test functionality of timed decorator on function,
108 instancemethod, classmethod, staticmethod..."""
109 # Check that decorator preserves docstring
110 assert fun.__doc__ == "Foo"
111
112 # Check that function call works and returns what expected
113 assert fun(1, 2, 3, four=5) == ((1, 2, 3), {'four': 5})
114 assert fun(1, 2, 3, four=5) == ((1, 2, 3), {'four': 5})
115
116 # Check that correct timing was recorded
117 t = timing(task, TimingClear.clear)
118 assert t[0] == 2
119 assert t[1] >= 0.1
120
121
122 def test_decorator_timer_scope():
123 """Check that underlying Timer object is not launched
124 by decorator unless function is invoked"""
125 task = get_random_task_name()
126
127 # Delete eventual previous timing
128 try:
129 timing(task, TimingClear.clear)
130 except RuntimeError:
131 pass
132
133 @timed(task)
134 def foo():
135 pass
136
137 # Delete function (including eventual timer which should not
138 # exist yet) before using it
139 del foo
140 gc.collect()
141
142 # Check that there's no timing entry
143 with pytest.raises(RuntimeError):
144 timing(task, TimingClear.clear)
145
146
147 def test_decorator_timing_correctness():
148 """Check that timing is measured really when the function
149 is executed, not during existence of decorator"""
150 task = get_random_task_name()
151
152 @timed(task)
153 def foo():
154 pass
155
156 sleep(0.05)
157 foo()
158 sleep(0.05)
159
160 # Check that sleeping above did not influence timer
161 assert timing(task, TimingClear.clear)[1] < 0.1