Debian Codegraph @

python/test/unit/common/test_timer.py

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