1
# Copyright (c) 2007-2009 Twisted Matrix Laboratories.
2
# See LICENSE for details.
5
See how fast deferreds are.
7
This is mainly useful to compare cdefer.Deferred to defer.Deferred
11
from twisted.internet import defer
12
from timer import timeit
16
def benchmarkFunc(iter, args=()):
18
A decorator for benchmark functions that measure a single iteration
19
count. Registers the function with the given iteration count to the global
23
benchmarkFuncs.append((func, args, iter))
27
def benchmarkNFunc(iter, ns):
29
A decorator for benchmark functions that measure multiple iteration
30
counts. Registers the function with the given iteration count to the global
35
benchmarkFuncs.append((func, (n,), iter))
41
Only create a deferred
44
instantiate = benchmarkFunc(100000)(instantiate)
46
def instantiateShootCallback():
48
Create a deferred and give it a normal result
52
instantiateShootCallback = benchmarkFunc(100000)(instantiateShootCallback)
54
def instantiateShootErrback():
56
Create a deferred and give it an exception result. To avoid Unhandled
57
Errors, also register an errback that eats the error
64
d.addErrback(lambda x: None)
65
instantiateShootErrback = benchmarkFunc(200)(instantiateShootErrback)
67
ns = [10, 1000, 10000]
69
def instantiateAddCallbacksNoResult(n):
71
Creates a deferred and adds a trivial callback/errback/both to it the given
82
instantiateAddCallbacksNoResult = benchmarkNFunc(20, ns)(instantiateAddCallbacksNoResult)
84
def instantiateAddCallbacksBeforeResult(n):
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.
98
instantiateAddCallbacksBeforeResult = benchmarkNFunc(20, ns)(instantiateAddCallbacksBeforeResult)
100
def instantiateAddCallbacksAfterResult(n):
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.
115
instantiateAddCallbacksAfterResult = benchmarkNFunc(20, ns)(instantiateAddCallbacksAfterResult)
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
134
pauseUnpause = benchmarkNFunc(20, ns)(pauseUnpause)
138
Run all of the benchmarks registered in the benchmarkFuncs list
140
print defer.Deferred.__module__
141
for func, args, iter in benchmarkFuncs:
142
print func.__name__, args, timeit(func, iter, *args)
144
if __name__ == '__main__':