Debian Codegraph @

rpython/rlib/rvmprof/test/test_rvmprof.py

raw symbols SCIP
1 import py, os
2 import pytest
3 import time
4 from rpython.tool.udir import udir
5 from rpython.rlib import rvmprof
6 from rpython.translator.c.test.test_genc import compile
7 from rpython.translator.tool.cbuild import ExternalCompilationInfo
8 from rpython.rtyper.lltypesystem import rffi, lltype
9
10 @pytest.mark.usefixtures('init')
11 class RVMProfTest(object):
12
13 ENTRY_POINT_ARGS = ()
14
15 class MyCode(object):
16 def __init__(self, name='py:code:0:noname'):
17 self.name = name
18
19 def get_name(self):
20 return self.name
21
22 @pytest.fixture
23 def init(self):
24 self.register()
25 self.rpy_entry_point = compile(self.entry_point, self.ENTRY_POINT_ARGS)
26
27 def register(self):
28 rvmprof.register_code_object_class(self.MyCode,
29 self.MyCode.get_name)
30
31
32 class TestExecuteCode(RVMProfTest):
33
34 def entry_point(self):
35 res = self.main(self.MyCode(), 5)
36 assert res == 42
37 return 0
38
39 @rvmprof.vmprof_execute_code("xcode1", lambda self, code, num: code)
40 def main(self, code, num):
41 print num
42 return 42
43
44 def test(self):
45 assert self.entry_point() == 0
46 assert self.rpy_entry_point() == 0
47
48
49 class TestResultClass(RVMProfTest):
50
51 class A: pass
52
53 @rvmprof.vmprof_execute_code("xcode2", lambda self, num, code: code,
54 result_class=A)
55 def main(self, num, code):
56 print num
57 return self.A()
58
59 def entry_point(self):
60 a = self.main(7, self.MyCode())
61 assert isinstance(a, self.A)
62 return 0
63
64 def test(self):
65 assert self.entry_point() == 0
66 assert self.rpy_entry_point() == 0
67
68
69 class TestRegisterCode(RVMProfTest):
70
71 @rvmprof.vmprof_execute_code("xcode1", lambda self, code, num: code)
72 def main(self, code, num):
73 print num
74 return 42
75
76 def entry_point(self):
77 code = self.MyCode()
78 rvmprof.register_code(code, lambda code: 'some code')
79 res = self.main(code, 5)
80 assert res == 42
81 return 0
82
83 def test(self):
84 assert self.entry_point() == 0
85 assert self.rpy_entry_point() == 0
86
87
88 class RVMProfSamplingTest(RVMProfTest):
89
90 # the kernel will deliver SIGPROF at max 250 Hz. See also
91 # https://github.com/vmprof/vmprof-python/issues/163
92 SAMPLING_INTERVAL = 1/250.0
93
94 @pytest.fixture
95 def init(self, tmpdir):
96 self.tmpdir = tmpdir
97 self.tmpfile = tmpdir.join('profile.vmprof')
98 self.tmpfilename = str(self.tmpfile)
99 super(RVMProfSamplingTest, self).init()
100
101 ENTRY_POINT_ARGS = (int, float, int)
102 def entry_point(self, value, delta_t, memory=0):
103 code = self.MyCode('py:code:52:test_enable')
104 rvmprof.register_code(code, self.MyCode.get_name)
105 fd = os.open(self.tmpfilename, os.O_WRONLY | os.O_CREAT, 0666)
106 rvmprof.enable(fd, self.SAMPLING_INTERVAL, memory=memory)
107 start = time.time()
108 res = 0
109 while time.time() < start+delta_t:
110 res = self.main(code, value)
111 rvmprof.disable()
112 os.close(fd)
113 return res
114
115 def approx_equal(self, a, b, tolerance=0.15):
116 max_diff = (a+b)/2.0 * tolerance
117 return abs(a-b) < max_diff
118
119
120 class TestEnable(RVMProfSamplingTest):
121
122 @rvmprof.vmprof_execute_code("xcode1", lambda self, code, count: code)
123 def main(self, code, count):
124 s = 0
125 for i in range(count):
126 s += (i << 1)
127 return s
128
129 def test(self):
130 from vmprof import read_profile
131 assert self.entry_point(10**4, 0.1, 0) == 99990000
132 assert self.tmpfile.check()
133 self.tmpfile.remove()
134 #
135 assert self.rpy_entry_point(10**4, 0.5, 0) == 99990000
136 assert self.tmpfile.check()
137 prof = read_profile(self.tmpfilename)
138 tree = prof.get_tree()
139 assert tree.name == 'py:code:52:test_enable'
140 assert self.approx_equal(tree.count, 0.5/self.SAMPLING_INTERVAL)
141
142 def test_mem(self):
143 from vmprof import read_profile
144 assert self.rpy_entry_point(10**4, 0.5, 1) == 99990000
145 assert self.tmpfile.check()
146 prof = read_profile(self.tmpfilename)
147 assert prof.profile_memory
148 assert all(p[-1] > 0 for p in prof.profiles)
149
150
151 class TestNative(RVMProfSamplingTest):
152
153 @pytest.fixture
154 def init(self, tmpdir):
155 eci = ExternalCompilationInfo(compile_extra=['-g','-O0', '-Werror'],
156 post_include_bits = ['int native_func(int);'],
157 separate_module_sources=["""
158 RPY_EXTERN int native_func(int d) {
159 int j = 0;
160 if (d > 0) {
161 return native_func(d-1);
162 } else {
163 for (int i = 0; i < 42000; i++) {
164 j += 1;
165 }
166 }
167 return j;
168 }
169 """])
170 self.native_func = rffi.llexternal("native_func", [rffi.INT], rffi.INT,
171 compilation_info=eci)
172 super(TestNative, self).init(tmpdir)
173
174 @rvmprof.vmprof_execute_code("xcode1", lambda self, code, count: code)
175 def main(self, code, count):
176 code = self.MyCode('py:main:3:main')
177 rvmprof.register_code(code, self.MyCode.get_name)
178 code = self.MyCode('py:code:7:native_func')
179 rvmprof.register_code(code, self.MyCode.get_name)
180 if count > 0:
181 return self.main(code, count-1)
182 else:
183 return self.native_func(100)
184
185 def test(self):
186 from vmprof import read_profile
187 # from vmprof.show import PrettyPrinter
188 assert self.rpy_entry_point(3, 0.5, 0) == 42000
189 assert self.tmpfile.check()
190
191 prof = read_profile(self.tmpfilename)
192 tree = prof.get_tree()
193 # p = PrettyPrinter()
194 # p._print_tree(tree)
195 def walk(tree, symbols):
196 symbols.append(tree.name)
197 if len(tree.children) == 0:
198 return
199 for child in tree.children.values():
200 walk(child, symbols)
201 symbols = []
202 walk(tree, symbols)
203 not_found = ['py:code:7:native_func']
204 for sym in symbols:
205 for i,name in enumerate(not_found):
206 if sym.startswith(name):
207 del not_found[i]
208 break
209 assert not_found == []