Debian Codegraph @

pypy/module/_lsprof/test/test_cprofile.py

raw symbols SCIP
1 class AppTestCProfile(object):
2 spaceconfig = {
3 "usemodules": ['_lsprof', 'time'],
4 }
5
6 def setup_class(cls):
7 cls.w_file = cls.space.wrap(__file__)
8
9 def test_repr(self):
10 import _lsprof
11 assert repr(_lsprof.Profiler) == "<class '_lsprof.Profiler'>"
12
13 def test_builtins(self):
14 import _lsprof
15 prof = _lsprof.Profiler()
16 lst = []
17 prof.enable()
18 lst.append(len(lst))
19 prof.disable()
20 stats = prof.getstats()
21 expected = (
22 "<built-in method builtins.len>",
23 "<method 'append' of 'list' objects>",
24 "<method 'disable' of '_lsprof.Profiler' objects>",
25 )
26 for entry in stats:
27 assert entry.code in expected
28
29 def test_builtins_callers(self):
30 import _lsprof
31 prof = _lsprof.Profiler(subcalls=True)
32 lst = []
33 def f1():
34 lst.append(len(lst))
35 prof.enable(subcalls=True)
36 f1()
37 prof.disable()
38 stats = prof.getstats()
39 expected = (
40 "<built-in method builtins.len>",
41 "<method 'append' of 'list' objects>",
42 )
43 by_id = set()
44 for entry in stats:
45 if entry.code == f1.__code__:
46 assert len(entry.calls) == 2
47 for subentry in entry.calls:
48 assert subentry.code in expected
49 by_id.add(id(subentry.code))
50 elif entry.code in expected:
51 by_id.add(id(entry.code))
52 # :-( cProfile.py relies on the id() of the strings...
53 assert len(by_id) == len(expected)
54
55 def test_direct(self):
56 import _lsprof
57 def getticks():
58 return len(ticks)
59 prof = _lsprof.Profiler(getticks, 0.25, True, False)
60 ticks = []
61 def bar(m):
62 ticks.append(1)
63 if m == 1:
64 foo(42)
65 ticks.append(1)
66 def spam(m):
67 bar(m)
68 def foo(n):
69 bar(n)
70 ticks.append(1)
71 bar(n+1)
72 ticks.append(1)
73 spam(n+2)
74 prof.enable()
75 foo(0)
76 prof.disable()
77 assert len(ticks) == 16
78 stats = prof.getstats()
79 entries = {}
80 for entry in stats:
81 assert hasattr(entry.code, 'co_name')
82 entries[entry.code.co_name] = entry
83 efoo = entries['foo']
84 assert efoo.callcount == 2
85 assert efoo.reccallcount == 1
86 assert efoo.inlinetime == 1.0
87 assert efoo.totaltime == 4.0
88 assert len(efoo.calls) == 2
89 ebar = entries['bar']
90 assert ebar.callcount == 6
91 assert ebar.reccallcount == 3
92 assert ebar.inlinetime == 3.0
93 assert ebar.totaltime == 3.5
94 assert len(ebar.calls) == 1
95 espam = entries['spam']
96 assert espam.callcount == 2
97 assert espam.reccallcount == 0
98 assert espam.inlinetime == 0.0
99 assert espam.totaltime == 1.0
100 assert len(espam.calls) == 1
101
102 foo2spam, foo2bar = efoo.calls
103 if foo2bar.code.co_name == 'spam':
104 foo2bar, foo2spam = foo2spam, foo2bar
105 assert foo2bar.code.co_name == 'bar'
106 assert foo2bar.callcount == 4
107 assert foo2bar.reccallcount == 2
108 assert foo2bar.inlinetime == 2.0
109 assert foo2bar.totaltime == 3.0
110 assert foo2spam.code.co_name == 'spam'
111 assert foo2spam.callcount == 2
112 assert foo2spam.reccallcount == 0
113 assert foo2spam.inlinetime == 0.0
114 assert foo2spam.totaltime == 1.0
115
116 bar2foo, = ebar.calls
117 assert bar2foo.code.co_name == 'foo'
118 assert bar2foo.callcount == 1
119 assert bar2foo.reccallcount == 0
120 assert bar2foo.inlinetime == 0.5
121 assert bar2foo.totaltime == 2.0
122
123 spam2bar, = espam.calls
124 assert spam2bar.code.co_name == 'bar'
125 assert spam2bar.callcount == 2
126 assert spam2bar.reccallcount == 0
127 assert spam2bar.inlinetime == 1.0
128 assert spam2bar.totaltime == 1.0
129
130 def test_scale_of_result(self):
131 import _lsprof, time
132 prof = _lsprof.Profiler()
133 def foo(n):
134 t = time.time()
135 while abs(t - time.time()) < 1.0:
136 pass # busy-wait for 1 second
137 def bar(n):
138 foo(n)
139 prof.enable()
140 bar(0)
141 prof.disable()
142 stats = prof.getstats()
143 entries = {}
144 for entry in stats:
145 entries[entry.code] = entry
146 efoo = entries[foo.__code__]
147 ebar = entries[bar.__code__]
148 assert 0.9 < efoo.totaltime < 2.9
149 # --- cannot test .inlinetime, because it does not include
150 # --- the time spent doing the call to time.time()
151 #assert 0.9 < efoo.inlinetime < 2.9
152 for subentry in ebar.calls:
153 assert 0.9 < subentry.totaltime < 2.9
154 #assert 0.9 < subentry.inlinetime < 2.9
155
156 def test_builtin_exception(self):
157 import math
158 import _lsprof
159
160 prof = _lsprof.Profiler()
161 prof.enable()
162 try:
163 math.sqrt("a")
164 except TypeError:
165 pass
166 prof.disable()
167 stats = prof.getstats()
168 assert len(stats) == 2