/
test.py
147 lines (116 loc) · 4.55 KB
/
test.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""Test providers performance."""
import time
import gc
import dependency_injector.providers
class Tester(object):
"""Performance tester for provider module implementations."""
def __init__(self, provider_modules, duration_factor):
"""Initializer."""
self.provider_modules = provider_modules
self.tests = [getattr(self, name)
for name in dir(self)
if name.startswith('test')]
self.total_time = 0
self.duration_factor = duration_factor
def run(self):
"""Run all tests for all provider modules."""
for module in self.provider_modules:
print('\n')
print('Running tests for module - "{module}":'
.format(module=module.__name__))
gc.disable()
for test in self.tests:
start_time = time.time()
test(module)
self.total_time = time.time() - start_time
print('Test "{test}" took - {time}'
.format(test=test.__name__,
time=self.total_time))
gc.collect()
gc.enable()
print('\n')
def test_raw_3_kw_injections(self, providers):
"""Test 3 keyword argument injections."""
class A(object):
pass
class B(object):
pass
class C(object):
pass
class Test(object):
def __init__(self, a, b, c):
pass
for x in xrange(int(5000000 * self.duration_factor)):
Test(a=A(), b=B(), c=C())
def test_factory_3_factory_kw_injections(self, providers):
"""Test factory with 3 keyword argument injections via factories."""
class A(object):
pass
class B(object):
pass
class C(object):
pass
class Test(object):
def __init__(self, a, b, c):
pass
a_factory = providers.Factory(A)
b_factory = providers.Factory(B)
c_factory = providers.Factory(C)
test_factory = providers.Factory(Test,
a=a_factory,
b=b_factory,
c=c_factory)
for x in xrange(int(5000000 * self.duration_factor)):
test_factory()
def test_abstract_factory_3_factory_kw_injections(self, providers):
"""Test factory with 3 keyword argument injections via factories."""
class A(object):
pass
class B(object):
pass
class C(object):
pass
class Test(object):
def __init__(self, a, b, c):
pass
a_factory = providers.Factory(A)
b_factory = providers.Factory(B)
c_factory = providers.Factory(C)
test_factory = providers.AbstractFactory(object)
test_factory.override(providers.Factory(Test,
a=a_factory,
b=b_factory,
c=c_factory))
for x in xrange(int(5000000 * self.duration_factor)):
test_factory()
def test_factory_6_factory_kw_injections_0_context(self, providers):
"""Test factory with 6 keyword argument injections."""
class Test(object):
def __init__(self, a, b, c, d, e, f):
pass
test_factory = providers.Factory(Test, a=1, b=2, c=3, d=4, e=5, f=6)
for x in xrange(int(5000000 * self.duration_factor)):
test_factory()
def test_factory_6_factory_kw_injections_1_context(self, providers):
"""Test factory with 6 keyword argument injections."""
class Test(object):
def __init__(self, a, b, c, d, e, f):
pass
test_factory = providers.Factory(Test, f=6)
for x in xrange(int(5000000 * self.duration_factor)):
test_factory(a=1, b=2, c=3, d=4, e=5)
def test_factory_6_factory_kw_injections_3_context(self, providers):
"""Test factory with 6 keyword argument injections."""
class Test(object):
def __init__(self, a, b, c, d, e, f):
pass
test_factory = providers.Factory(Test, a=1, b=2, c=3)
for x in xrange(int(5000000 * self.duration_factor)):
test_factory(d=4, e=5, f=6)
if __name__ == '__main__':
tester = Tester(
provider_modules=[
dependency_injector.providers,
],
duration_factor=0.5)
tester.run()