Debian Codegraph @

python/test/unit/common/test_timer.py

raw ◀ ▶
Source reconstructed at patch state 1 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 # resolution of the timer is not precise,
33 # need to allow some tolerance
34 timing_resolution = 0.015
35
36 def get_random_task_name():
37 """Get pseudo-random string"""
38 return hex(random.randint(0, 1e32))
39
40
41 def test_context_manager_named():
42 """Test that named Timer works as context manager"""
43 task = get_random_task_name()
44
45 # Execute task in the context manager
46 with Timer(task) as t:
47 sleep(0.1)
48 assert t.elapsed()[0] >= 0.1 - timing_resolution
49
50 # Check timing
51 t = timing(task, TimingClear.clear)
52 assert t[0] == 1
53 assert t[1] >= 0.05 - timing_resolution
54
55
56 def test_context_manager_anonymous():
57 """Test that anonymous Timer works as context manager"""
58 with Timer() as t:
59 sleep(0.1)
60 assert t.elapsed()[0] >= 0.1 - timing_resolution
61
62
63 # Test case for decorated free function
64 fun2_task = get_random_task_name()
65 @timed(fun2_task)
66 def fun2(*args, **kwargs):
67 "Foo"
68 sleep(0.05)
69 return args, kwargs
70
71 class C(object):
72 # Test case for decorated instancemethod
73 task_method2 = get_random_task_name()
74 @timed(task_method2)
75 def method2(self, *args, **kwargs):
76 "Foo"
77 sleep(0.05)
78 return args, kwargs
79
80 # Test case for decorated staticmethod
81 task_method3 = get_random_task_name()
82 @staticmethod
83 @timed(task_method3)
84 def method3(*args, **kwargs):
85 "Foo"
86 sleep(0.05)
87 return args, kwargs
88
89 # Test case for decorated classmethod
90 task_method5 = get_random_task_name()
91 @classmethod
92 @timed(task_method5)
93 def method5(cls, *args, **kwargs):
94 "Foo"
95 sleep(0.05)
96 return args, kwargs
97
98 # Testing object with decorated methods
99 o = C()
100
101
102 @pytest.mark.parametrize(("fun", "task"), [
103 (fun2, fun2_task),
104 (o.method2, o.task_method2),
105 (o.method3, o.task_method3),
106 (o.method5, o.task_method5),
107 (C.method3, C.task_method3),
108 (C.method5, C.task_method5),
109 ])
110 def test_decorator_functionality(fun, task):
111 """Test functionality of timed decorator on function,
112 instancemethod, classmethod, staticmethod..."""
113 # Check that decorator preserves docstring
114 assert fun.__doc__ == "Foo"
115
116 # Check that function call works and returns what expected
117 assert fun(1, 2, 3, four=5) == ((1, 2, 3), {'four': 5})
118 assert fun(1, 2, 3, four=5) == ((1, 2, 3), {'four': 5})
119
120 # Check that correct timing was recorded
121 t = timing(task, TimingClear.clear)
122 assert t[0] == 2
123 assert t[1] >= 0.1 - timing_resolution
124
125
126 def test_decorator_timer_scope():
127 """Check that underlying Timer object is not launched
128 by decorator unless function is invoked"""
129 task = get_random_task_name()
130
131 # Delete eventual previous timing
132 try:
133 timing(task, TimingClear.clear)
134 except RuntimeError:
135 pass
136
137 @timed(task)
138 def foo():
139 pass
140
141 # Delete function (including eventual timer which should not
142 # exist yet) before using it
143 del foo
144 gc.collect()
145
146 # Check that there's no timing entry
147 with pytest.raises(RuntimeError):
148 timing(task, TimingClear.clear)
149
150
151 def test_decorator_timing_correctness():
152 """Check that timing is measured really when the function
153 is executed, not during existence of decorator"""
154 task = get_random_task_name()
155
156 @timed(task)
157 def foo():
158 pass
159
160 sleep(0.05)
161 foo()
162 sleep(0.05)
163
164 # Check that sleeping above did not influence timer
165 assert timing(task, TimingClear.clear)[1] < 0.1 + timing_resolution