| 1 |
import time, os, sys |
| 2 |
if __name__ == '__main__': |
| 3 |
sys.path += ['../../../..'] |
| 4 |
import pytest |
| 5 |
from rpython.tool.udir import udir |
| 6 |
from rpython.rlib import rvmprof, rthread |
| 7 |
from rpython.translator.c.test.test_genc import compile |
| 8 |
from rpython.rlib.nonconst import NonConstant |
| 9 |
|
| 10 |
class MyCode: |
| 11 |
def __init__(self, count): |
| 12 |
self.count = count |
| 13 |
rvmprof.register_code(self, MyCode.get_name) |
| 14 |
|
| 15 |
def get_name(self): |
| 16 |
return 'test:mycode%d:%d:test_ztranslation' % (self.count, self.count) |
| 17 |
|
| 18 |
|
| 19 |
def setup_module(mod): |
| 20 |
try: |
| 21 |
rvmprof.register_code_object_class(MyCode, MyCode.get_name) |
| 22 |
except rvmprof.VMProfPlatformUnsupported as e: |
| 23 |
pytest.skip(str(e)) |
| 24 |
|
| 25 |
|
| 26 |
@rvmprof.vmprof_execute_code("interp", lambda code: code) |
| 27 |
def interpret(code): |
| 28 |
n = code.count |
| 29 |
while n > 0: |
| 30 |
n = one_less(n) |
| 31 |
return 42 |
| 32 |
|
| 33 |
def one_less(n): |
| 34 |
return n - 1 |
| 35 |
one_less._dont_inline_ = True |
| 36 |
|
| 37 |
|
| 38 |
PROF_FILE = str(udir.join('test_ztranslation.prof')) |
| 39 |
|
| 40 |
def main(argv=[]): |
| 41 |
rthread.get_ident() |
| 42 |
if NonConstant(False): |
| 43 |
|
| 44 |
os.open('foo', 1, 1) |
| 45 |
code1 = MyCode(6500) |
| 46 |
fd = os.open(PROF_FILE, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0666) |
| 47 |
rvmprof.enable(fd, 0.01) |
| 48 |
|
| 49 |
code2 = MyCode(9100) |
| 50 |
stop = time.time() + 1 |
| 51 |
while time.time() < stop: |
| 52 |
interpret(code1) |
| 53 |
interpret(code2) |
| 54 |
|
| 55 |
rvmprof.disable() |
| 56 |
os.close(fd) |
| 57 |
return 0 |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
def target(driver, args): |
| 62 |
return main |
| 63 |
|
| 64 |
def test_interpreted(): |
| 65 |
|
| 66 |
import subprocess |
| 67 |
me = os.path.basename(__file__) |
| 68 |
if me.endswith('pyc') or me.endswith('pyo'): |
| 69 |
me = me[:-1] |
| 70 |
env = os.environ.copy() |
| 71 |
env['PYTHONPATH'] = '' |
| 72 |
subprocess.check_call([sys.executable, me], |
| 73 |
cwd=(os.path.dirname(__file__) or '.'), |
| 74 |
env=env) |
| 75 |
|
| 76 |
def test_compiled(): |
| 77 |
fn = compile(main, [], gcpolicy="minimark") |
| 78 |
if os.path.exists(PROF_FILE): |
| 79 |
os.unlink(PROF_FILE) |
| 80 |
fn() |
| 81 |
assert os.path.exists(PROF_FILE) |
| 82 |
|
| 83 |
if __name__ == '__main__': |
| 84 |
setup_module(None) |
| 85 |
res = main() |
| 86 |
assert res == 0 |