Debian Codegraph @

docs/core/benchmarks/failure.py

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