This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pybench.py
81 lines (59 loc) · 2.09 KB
/
test_pybench.py
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
from itertools import product
from time import sleep
from pybench import Benchmark
class TimedRegion(Benchmark):
def test(self):
with self.timed_region('stuff'):
pass
class Parametrized(Benchmark):
params = [('a', range(3)), ('b', range(3))]
def test(self, a=None, b=None):
pass
def test_no_params():
assert Benchmark().run(method=lambda: None)['timings']['total'] > 0.0
def test_no_repeats():
assert not Benchmark().run(method=lambda: None, repeats=0)['timings']
def test_sleep():
def myfunc(n, duration):
for _ in range(n):
sleep(duration)
times = Benchmark().run(method=myfunc,
params=[('n', range(3)),
('duration', (0.001, 0.002))])
for (n, d), t in times['timings'].items():
assert abs(n*d - t['total']) < 1e-3
def test_timed_region():
result = TimedRegion().run()
assert result['timings']['total'] >= 0.0
assert result['timings']['stuff'] >= 0.0
def test_save_load(tmpdir):
b = TimedRegion()
d = tmpdir.join(b.name).strpath
result = b.run()
b.save(d)
TimedRegion().load(d) == result
def test_combine_regions(tmpdir):
b = TimedRegion()
da = tmpdir.join('a').strpath
db = tmpdir.join('b').strpath
keys = b.run()['timings'].keys()
b.save(da)
b.save(db)
result = TimedRegion().combine({da: 'a', db: 'b'})
assert all('a ' + k in result['timings'] for k in keys)
assert all('b ' + k in result['timings'] for k in keys)
def test_parametrized():
result = Parametrized().run()
_, pvalues = zip(*result['params'])
assert all(result['timings'][p]['total'] >= 0.0
for p in product(*pvalues))
def test_combine_parametrized(tmpdir):
b = Parametrized()
da = tmpdir.join('a').strpath
db = tmpdir.join('b').strpath
params = b.run()['timings'].keys()
b.save(da)
b.save(db)
result = Parametrized().combine({da: 'a', db: 'b'})
assert all('a total' in result['timings'][p] for p in params)
assert all('b total' in result['timings'][p] for p in params)