Debian Codegraph @

pypy/module/_vmprof/test/test__vmprof.py

raw symbols SCIP
1 import pytest
2 import sys
3 from rpython.rlib import rvmprof
4 from rpython.tool.udir import udir
5
6 class AppTestVMProf(object):
7 spaceconfig = {'usemodules': ['_vmprof', 'struct']}
8
9 def setup_class(cls):
10 cls.w_tmpfilename = cls.space.wrap(str(udir.join('test__vmprof.1')))
11 cls.w_tmpfilename2 = cls.space.wrap(str(udir.join('test__vmprof.2')))
12 cls.w_plain = cls.space.wrap(not cls.runappdirect and
13 '__pypy__' not in sys.builtin_module_names)
14
15 def test_import_vmprof(self):
16 tmpfile = open(self.tmpfilename, 'wb')
17 tmpfileno = tmpfile.fileno()
18 tmpfile2 = open(self.tmpfilename2, 'wb')
19 tmpfileno2 = tmpfile2.fileno()
20
21 import struct, sys, gc
22
23 WORD = struct.calcsize('l')
24
25 def count(s):
26 i = 0
27 count = 0
28 i += 5 * WORD # header
29 assert s[i ] == 5 # MARKER_HEADER
30 assert s[i + 1] == 0 # 0
31 assert s[i + 2] == 6 # VERSION_TIMESTAMP
32 assert s[i + 3] == 8 # PROFILE_RPYTHON
33 assert s[i + 4] == 4 # len('pypy')
34 assert s[i + 5: i + 9] == b'pypy'
35 i += 9
36 while i < len(s):
37 if s[i] == 3:
38 break
39 elif s[i] == 1:
40 i += 1
41 _, size = struct.unpack("ll", s[i:i + 2 * WORD])
42 i += 2 * WORD + size * struct.calcsize("P")
43 i += WORD # thread id
44 elif s[i] == 2:
45 i += 1
46 _, size = struct.unpack("ll", s[i:i + 2 * WORD])
47 count += 1
48 i += 2 * WORD + size
49 elif s[i] == 6:
50 print(s[i:i+24])
51 i += 1+8+8+8
52 elif s[i] == 7:
53 i += 1
54 # skip string
55 size, = struct.unpack("l", s[i:i + WORD])
56 i += WORD+size
57 # skip string
58 size, = struct.unpack("l", s[i:i + WORD])
59 i += WORD+size
60 else:
61 raise AssertionError(s[i])
62 return count
63
64 import _vmprof
65 gc.collect() # try to make the weakref list deterministic
66 gc.collect() # by freeing all dead code objects
67 _vmprof.enable(tmpfileno, 0.01, 0, 0, 0, 0)
68 _vmprof.disable()
69 s = open(self.tmpfilename, 'rb').read()
70 no_of_codes = count(s)
71 assert no_of_codes > 10
72 d = {}
73
74 def exec_(code, d):
75 exec(code, d)
76
77 exec_("""def foo():
78 pass
79 """, d)
80
81 gc.collect()
82 gc.collect()
83 _vmprof.enable(tmpfileno2, 0.01, 0, 0, 0, 0)
84
85 exec_("""def foo2():
86 pass
87 """, d)
88
89 _vmprof.disable()
90 s = open(self.tmpfilename2, 'rb').read()
91 no_of_codes2 = count(s)
92 assert b"py:foo:" in s
93 assert b"py:foo2:" in s
94 assert no_of_codes2 >= no_of_codes + 2 # some extra codes from tests
95
96 def test_enable_ovf(self):
97 import _vmprof
98 raises(_vmprof.VMProfError, _vmprof.enable, 2, 0, 0, 0, 0, 0)
99 raises(_vmprof.VMProfError, _vmprof.enable, 2, -2.5, 0, 0, 0, 0)
100 raises(_vmprof.VMProfError, _vmprof.enable, 2, 1e300, 0, 0, 0, 0)
101 raises(_vmprof.VMProfError, _vmprof.enable, 2, 1e300 * 1e300, 0, 0, 0, 0)
102 NaN = (1e300*1e300) / (1e300*1e300)
103 raises(_vmprof.VMProfError, _vmprof.enable, 2, NaN, 0, 0, 0, 0)
104
105 def test_is_enabled(self):
106 import _vmprof
107 tmpfile = open(self.tmpfilename, 'wb')
108 assert _vmprof.is_enabled() is False
109 _vmprof.enable(tmpfile.fileno(), 0.01, 0, 0, 0, 0)
110 assert _vmprof.is_enabled() is True
111 _vmprof.disable()
112 assert _vmprof.is_enabled() is False
113
114 @pytest.mark.xfail(sys.platform.startswith('freebsd'), reason = "not implemented")
115 def test_get_profile_path(self):
116 import _vmprof
117 with open(self.tmpfilename, "wb") as tmpfile:
118 assert _vmprof.get_profile_path() is None
119 _vmprof.enable(tmpfile.fileno(), 0.01, 0, 0, 0, 0)
120 path = _vmprof.get_profile_path()
121 _vmprof.disable()
122
123 if path != tmpfile.name:
124 with open(path, "rb") as fd1:
125 with open(self.tmpfilename, "rb") as fd2:
126 assert fd1.read() == fd2.read()
127
128 assert _vmprof.get_profile_path() is None
129
130 def test_stop_sampling(self):
131 if not self.plain:
132 skip("unreliable test except on CPython without -A")
133 import os
134 import _vmprof
135 tmpfile = open(self.tmpfilename, 'wb')
136 native = 1
137 def f():
138 import sys
139 import math
140 j = sys.maxsize
141 for i in range(500):
142 j = math.sqrt(j)
143 _vmprof.enable(tmpfile.fileno(), 0.01, 0, native, 0, 0)
144 # get_vmprof_stack() always returns 0 here!
145 # see vmprof_common.c and assume RPYTHON_LL2CTYPES is defined!
146 f()
147 fileno = _vmprof.stop_sampling()
148 pos = os.lseek(fileno, 0, os.SEEK_CUR)
149 f()
150 pos2 = os.lseek(fileno, 0, os.SEEK_CUR)
151 assert pos == pos2
152 _vmprof.start_sampling()
153 f()
154 fileno = _vmprof.stop_sampling()
155 pos3 = os.lseek(fileno, 0, os.SEEK_CUR)
156 assert pos3 > pos
157 _vmprof.disable()