File: deferreds.py

package info (click to toggle)
twisted 25.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,560 kB
  • sloc: python: 203,171; makefile: 200; sh: 92; javascript: 36; xml: 31
file content (190 lines) | stat: -rw-r--r-- 4,100 bytes parent folder | download | duplicates (3)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

"""
See how fast deferreds are.

This is mainly useful to compare cdefer.Deferred to defer.Deferred
"""


from timer import timeit

from twisted.internet import defer
from twisted.python.compat import range

benchmarkFuncs = []


def benchmarkFunc(iter, args=()):
    """
    A decorator for benchmark functions that measure a single iteration
    count. Registers the function with the given iteration count to the global
    benchmarkFuncs list
    """

    def decorator(func):
        benchmarkFuncs.append((func, args, iter))
        return func

    return decorator


def benchmarkNFunc(iter, ns):
    """
    A decorator for benchmark functions that measure multiple iteration
    counts. Registers the function with the given iteration count to the global
    benchmarkFuncs list.
    """

    def decorator(func):
        for n in ns:
            benchmarkFuncs.append((func, (n,), iter))
        return func

    return decorator


def instantiate():
    """
    Only create a deferred
    """
    d = defer.Deferred()


instantiate = benchmarkFunc(100000)(instantiate)


def instantiateShootCallback():
    """
    Create a deferred and give it a normal result
    """
    d = defer.Deferred()
    d.callback(1)


instantiateShootCallback = benchmarkFunc(100000)(instantiateShootCallback)


def instantiateShootErrback():
    """
    Create a deferred and give it an exception result. To avoid Unhandled
    Errors, also register an errback that eats the error
    """
    d = defer.Deferred()
    try:
        1 / 0
    except BaseException:
        d.errback()
    d.addErrback(lambda x: None)


instantiateShootErrback = benchmarkFunc(200)(instantiateShootErrback)

ns = [10, 1000, 10000]


def instantiateAddCallbacksNoResult(n):
    """
    Creates a deferred and adds a trivial callback/errback/both to it the given
    number of times.
    """
    d = defer.Deferred()

    def f(result):
        return result

    for i in range(n):
        d.addCallback(f)
        d.addErrback(f)
        d.addBoth(f)
        d.addCallbacks(f, f)


instantiateAddCallbacksNoResult = benchmarkNFunc(20, ns)(
    instantiateAddCallbacksNoResult
)


def instantiateAddCallbacksBeforeResult(n):
    """
    Create a deferred and adds a trivial callback/errback/both to it the given
    number of times, and then shoots a result through all of the callbacks.
    """
    d = defer.Deferred()

    def f(result):
        return result

    for i in range(n):
        d.addCallback(f)
        d.addErrback(f)
        d.addBoth(f)
        d.addCallbacks(f)
    d.callback(1)


instantiateAddCallbacksBeforeResult = benchmarkNFunc(20, ns)(
    instantiateAddCallbacksBeforeResult
)


def instantiateAddCallbacksAfterResult(n):
    """
    Create a deferred, shoots it and then adds a trivial callback/errback/both
    to it the given number of times. The result is processed through the
    callbacks as they are added.
    """
    d = defer.Deferred()

    def f(result):
        return result

    d.callback(1)
    for i in range(n):
        d.addCallback(f)
        d.addErrback(f)
        d.addBoth(f)
        d.addCallbacks(f)


instantiateAddCallbacksAfterResult = benchmarkNFunc(20, ns)(
    instantiateAddCallbacksAfterResult
)


def pauseUnpause(n):
    """
    Adds the given number of callbacks/errbacks/both to a deferred while it is
    paused, and unpauses it, trigerring the processing of the value through the
    callbacks.
    """
    d = defer.Deferred()

    def f(result):
        return result

    d.callback(1)
    d.pause()
    for i in range(n):
        d.addCallback(f)
        d.addErrback(f)
        d.addBoth(f)
        d.addCallbacks(f)
    d.unpause()


pauseUnpause = benchmarkNFunc(20, ns)(pauseUnpause)


def benchmark():
    """
    Run all of the benchmarks registered in the benchmarkFuncs list
    """
    print(defer.Deferred.__module__)
    for func, args, iter in benchmarkFuncs:
        print(func.__name__, args, timeit(func, iter, *args))


if __name__ == "__main__":
    benchmark()