Debian Codegraph @

docs/core/benchmarks/deferreds.py

raw
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 See how fast deferreds are.
6
7 This is mainly useful to compare cdefer.Deferred to defer.Deferred
8 """
9
10
11 from timer import timeit
12
13 from twisted.internet import defer
14 from twisted.python.compat import range
15
16 benchmarkFuncs = []
17
18
19 def benchmarkFunc(iter, args=()):
20 """
21 A decorator for benchmark functions that measure a single iteration
22 count. Registers the function with the given iteration count to the global
23 benchmarkFuncs list
24 """
25
26 def decorator(func):
27 benchmarkFuncs.append((func, args, iter))
28 return func
29
30 return decorator
31
32
33 def benchmarkNFunc(iter, ns):
34 """
35 A decorator for benchmark functions that measure multiple iteration
36 counts. Registers the function with the given iteration count to the global
37 benchmarkFuncs list.
38 """
39
40 def decorator(func):
41 for n in ns:
42 benchmarkFuncs.append((func, (n,), iter))
43 return func
44
45 return decorator
46
47
48 def instantiate():
49 """
50 Only create a deferred
51 """
52 d = defer.Deferred()
53
54
55 instantiate = benchmarkFunc(100000)(instantiate)
56
57
58 def instantiateShootCallback():
59 """
60 Create a deferred and give it a normal result
61 """
62 d = defer.Deferred()
63 d.callback(1)
64
65
66 instantiateShootCallback = benchmarkFunc(100000)(instantiateShootCallback)
67
68
69 def instantiateShootErrback():
70 """
71 Create a deferred and give it an exception result. To avoid Unhandled
72 Errors, also register an errback that eats the error
73 """
74 d = defer.Deferred()
75 try:
76 1 / 0
77 except BaseException:
78 d.errback()
79 d.addErrback(lambda x: None)
80
81
82 instantiateShootErrback = benchmarkFunc(200)(instantiateShootErrback)
83
84 ns = [10, 1000, 10000]
85
86
87 def instantiateAddCallbacksNoResult(n):
88 """
89 Creates a deferred and adds a trivial callback/errback/both to it the given
90 number of times.
91 """
92 d = defer.Deferred()
93
94 def f(result):
95 return result
96
97 for i in range(n):
98 d.addCallback(f)
99 d.addErrback(f)
100 d.addBoth(f)
101 d.addCallbacks(f, f)
102
103
104 instantiateAddCallbacksNoResult = benchmarkNFunc(20, ns)(
105 instantiateAddCallbacksNoResult
106 )
107
108
109 def instantiateAddCallbacksBeforeResult(n):
110 """
111 Create a deferred and adds a trivial callback/errback/both to it the given
112 number of times, and then shoots a result through all of the callbacks.
113 """
114 d = defer.Deferred()
115
116 def f(result):
117 return result
118
119 for i in range(n):
120 d.addCallback(f)
121 d.addErrback(f)
122 d.addBoth(f)
123 d.addCallbacks(f)
124 d.callback(1)
125
126
127 instantiateAddCallbacksBeforeResult = benchmarkNFunc(20, ns)(
128 instantiateAddCallbacksBeforeResult
129 )
130
131
132 def instantiateAddCallbacksAfterResult(n):
133 """
134 Create a deferred, shoots it and then adds a trivial callback/errback/both
135 to it the given number of times. The result is processed through the
136 callbacks as they are added.
137 """
138 d = defer.Deferred()
139
140 def f(result):
141 return result
142
143 d.callback(1)
144 for i in range(n):
145 d.addCallback(f)
146 d.addErrback(f)
147 d.addBoth(f)
148 d.addCallbacks(f)
149
150
151 instantiateAddCallbacksAfterResult = benchmarkNFunc(20, ns)(
152 instantiateAddCallbacksAfterResult
153 )
154
155
156 def pauseUnpause(n):
157 """
158 Adds the given number of callbacks/errbacks/both to a deferred while it is
159 paused, and unpauses it, trigerring the processing of the value through the
160 callbacks.
161 """
162 d = defer.Deferred()
163
164 def f(result):
165 return result
166
167 d.callback(1)
168 d.pause()
169 for i in range(n):
170 d.addCallback(f)
171 d.addErrback(f)
172 d.addBoth(f)
173 d.addCallbacks(f)
174 d.unpause()
175
176
177 pauseUnpause = benchmarkNFunc(20, ns)(pauseUnpause)
178
179
180 def benchmark():
181 """
182 Run all of the benchmarks registered in the benchmarkFuncs list
183 """
184 print(defer.Deferred.__module__)
185 for func, args, iter in benchmarkFuncs:
186 print(func.__name__, args, timeit(func, iter, *args))
187
188
189 if __name__ == "__main__":
190 benchmark()