Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / core / benchmarks / failure.py
1
2 """See how slow failure creation is"""
3
4 import random
5 from twisted.python import failure
6
7 random.seed(10050)
8 O = [0, 20, 40, 60, 80, 10, 30, 50, 70, 90]
9 DEPTH = 30
10
11 def pickVal():
12     return random.choice([None, 1, 'Hello', [], {1: 1}, (1, 2, 3)])
13
14 def makeLocals(n):
15     return ';'.join(['x%d = %s' % (i, pickVal()) for i in range(n)])
16
17 for nLocals in O:
18     for i in range(DEPTH):
19         s = """
20 def deepFailure%d_%d():
21     %s
22     deepFailure%d_%d()
23 """ % (nLocals, i, makeLocals(nLocals), nLocals, i + 1)
24     exec s
25
26     exec """
27 def deepFailure%d_%d():
28     1 / 0
29 """ % (nLocals, DEPTH)
30
31 R = range(5000)
32 def fail(n):
33     for i in R:
34         try:
35             eval('deepFailure%d_0' % n)()
36         except:
37             failure.Failure()
38
39 def fail_str(n):
40     for i in R:
41         try:
42             eval('deepFailure%d_0' % n)()
43         except:
44             str(failure.Failure())
45
46 class PythonException(Exception): pass
47
48 def fail_easy(n):
49     for i in R:
50         try:
51             failure.Failure(PythonException())
52         except:
53             pass
54
55 from timer import timeit
56 # for i in O:
57 #     timeit(fail, 1, i)
58
59 # for i in O:
60 #     print 'easy failing', i, timeit(fail_easy, 1, i)
61
62 for i in O:
63     print 'failing', i, timeit(fail, 1, i)
64
65 # for i in O:
66 #     print 'string failing', i, timeit(fail_str, 1, i)