forked from samuelcolvin/python-devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_timer.py
More file actions
71 lines (57 loc) · 1.71 KB
/
test_timer.py
File metadata and controls
71 lines (57 loc) · 1.71 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
import io
import re
import sys
from time import sleep
import pytest
from devtools import debug
from devtools.timer import Timer
@pytest.mark.skipif(sys.platform != 'linux', reason='not on linux')
def test_simple():
f = io.StringIO()
t = debug.timer(name='foobar', file=f)
with t:
sleep(0.01)
v = f.getvalue()
assert re.fullmatch(r'foobar: 0\.01[012]s elapsed\n', v)
@pytest.mark.skipif(sys.platform != 'linux', reason='not on linux')
def test_multiple():
f = io.StringIO()
t = Timer(file=f)
for i in [0.001, 0.002, 0.003]:
with t(i):
sleep(i)
t.summary()
v = f.getvalue()
assert re.sub(r'0\.00\d', '0.00X', v) == (
'0.00X: 0.00Xs elapsed\n'
'0.00X: 0.00Xs elapsed\n'
'0.00X: 0.00Xs elapsed\n'
'3 times: mean=0.00Xs stdev=0.00Xs min=0.00Xs max=0.00Xs\n'
)
def test_unfinished():
t = Timer().start()
assert str(t.results[0]) == '-1.000s elapsed'
@pytest.mark.skipif(sys.platform != 'linux', reason='not on linux')
def test_multiple_not_verbose():
f = io.StringIO()
t = Timer(file=f)
for i in [0.01, 0.02, 0.03]:
with t(verbose=False):
sleep(i)
t.summary(True)
v = re.sub('[123]s', '0s', f.getvalue())
assert v == (
' 0.010s elapsed\n'
' 0.020s elapsed\n'
' 0.030s elapsed\n'
'3 times: mean=0.020s stdev=0.010s min=0.010s max=0.030s\n'
)
def test_unfinished_summary():
f = io.StringIO()
t = Timer(file=f).start()
t.summary()
v = f.getvalue()
assert v == '1 times: mean=0.000s stdev=0.000s min=0.000s max=0.000s\n'
def test_summary_not_started():
with pytest.raises(RuntimeError):
Timer().summary()