Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / core / benchmarks / deferreds.py
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 twisted.internet import defer
12 from timer import timeit
13
14 benchmarkFuncs = []
15
16 def benchmarkFunc(iter, args=()):
17     """
18     A decorator for benchmark functions that measure a single iteration
19     count. Registers the function with the given iteration count to the global
20     benchmarkFuncs list
21     """
22     def decorator(func):
23         benchmarkFuncs.append((func, args, iter))
24         return func
25     return decorator
26
27 def benchmarkNFunc(iter, ns):
28     """
29     A decorator for benchmark functions that measure multiple iteration
30     counts. Registers the function with the given iteration count to the global
31     benchmarkFuncs list.
32     """
33     def decorator(func):
34         for n in ns:
35             benchmarkFuncs.append((func, (n,), iter))
36         return func
37     return decorator
38
39 def instantiate():
40     """
41     Only create a deferred
42     """
43     d = defer.Deferred()
44 instantiate = benchmarkFunc(100000)(instantiate)
45
46 def instantiateShootCallback():
47     """
48     Create a deferred and give it a normal result
49     """
50     d = defer.Deferred()
51     d.callback(1)
52 instantiateShootCallback = benchmarkFunc(100000)(instantiateShootCallback)
53
54 def instantiateShootErrback():
55     """
56     Create a deferred and give it an exception result. To avoid Unhandled
57     Errors, also register an errback that eats the error
58     """
59     d = defer.Deferred()
60     try:
61         1/0
62     except:
63         d.errback()
64     d.addErrback(lambda x: None)
65 instantiateShootErrback = benchmarkFunc(200)(instantiateShootErrback)
66
67 ns = [10, 1000, 10000]
68
69 def instantiateAddCallbacksNoResult(n):
70     """
71     Creates a deferred and adds a trivial callback/errback/both to it the given
72     number of times.
73     """
74     d = defer.Deferred()
75     def f(result):
76         return result
77     for i in xrange(n):
78         d.addCallback(f)
79         d.addErrback(f)
80         d.addBoth(f)
81         d.addCallbacks(f, f)
82 instantiateAddCallbacksNoResult = benchmarkNFunc(20, ns)(instantiateAddCallbacksNoResult)
83
84 def instantiateAddCallbacksBeforeResult(n):
85     """
86     Create a deferred and adds a trivial callback/errback/both to it the given
87     number of times, and then shoots a result through all of the callbacks.
88     """
89     d = defer.Deferred()
90     def f(result):
91         return result
92     for i in xrange(n):
93         d.addCallback(f)
94         d.addErrback(f)
95         d.addBoth(f)
96         d.addCallbacks(f)
97     d.callback(1)
98 instantiateAddCallbacksBeforeResult = benchmarkNFunc(20, ns)(instantiateAddCallbacksBeforeResult)
99
100 def instantiateAddCallbacksAfterResult(n):
101     """
102     Create a deferred, shoots it and then adds a trivial callback/errback/both
103     to it the given number of times. The result is processed through the
104     callbacks as they are added.
105     """
106     d = defer.Deferred()
107     def f(result):
108         return result
109     d.callback(1)
110     for i in xrange(n):
111         d.addCallback(f)
112         d.addErrback(f)
113         d.addBoth(f)
114         d.addCallbacks(f)
115 instantiateAddCallbacksAfterResult = benchmarkNFunc(20, ns)(instantiateAddCallbacksAfterResult)
116
117 def pauseUnpause(n):
118     """
119     Adds the given number of callbacks/errbacks/both to a deferred while it is
120     paused, and unpauses it, trigerring the processing of the value through the
121     callbacks.
122     """
123     d = defer.Deferred()
124     def f(result):
125         return result
126     d.callback(1)
127     d.pause()
128     for i in xrange(n):
129         d.addCallback(f)
130         d.addErrback(f)
131         d.addBoth(f)
132         d.addCallbacks(f)
133     d.unpause()
134 pauseUnpause = benchmarkNFunc(20, ns)(pauseUnpause)
135
136 def benchmark():
137     """
138     Run all of the benchmarks registered in the benchmarkFuncs list
139     """
140     print defer.Deferred.__module__
141     for func, args, iter in benchmarkFuncs:
142         print func.__name__, args, timeit(func, iter, *args)
143
144 if __name__ == '__main__':
145     benchmark()