File: test_ztranslation.py

package info (click to toggle)
pypy 5.6.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 97,040 kB
  • ctags: 185,069
  • sloc: python: 1,147,862; ansic: 49,642; cpp: 5,245; asm: 5,169; makefile: 529; sh: 481; xml: 232; lisp: 45
file content (86 lines) | stat: -rw-r--r-- 2,293 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import time, os, sys
if __name__ == '__main__':
    sys.path += ['../../../..']    # for subprocess in test_interpreted
import py
from rpython.tool.udir import udir
from rpython.rlib import rvmprof, rthread
from rpython.translator.c.test.test_genc import compile
from rpython.rlib.nonconst import NonConstant

class MyCode:
    def __init__(self, count):
        self.count = count
        rvmprof.register_code(self, MyCode.get_name)

    def get_name(self):
        return 'test:mycode%d:%d:test_ztranslation' % (self.count, self.count)


def setup_module(mod):
    try:
        rvmprof.register_code_object_class(MyCode, MyCode.get_name)
    except rvmprof.VMProfPlatformUnsupported as e:
        py.test.skip(str(e))


@rvmprof.vmprof_execute_code("interp", lambda code: code)
def interpret(code):
    n = code.count
    while n > 0:
        n = one_less(n)
    return 42

def one_less(n):
    return n - 1
one_less._dont_inline_ = True


PROF_FILE = str(udir.join('test_ztranslation.prof'))

def main(argv=[]):
    rthread.get_ident() # force TLOFS_thread_ident
    if NonConstant(False):
        # Hack to give os.open() the correct annotation
        os.open('foo', 1, 1)
    code1 = MyCode(6500)
    fd = os.open(PROF_FILE, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0666)
    rvmprof.enable(fd, 0.01)
    #
    code2 = MyCode(9100)
    stop = time.time() + 1
    while time.time() < stop:
        interpret(code1)
        interpret(code2)
    #
    rvmprof.disable()
    os.close(fd)
    return 0

# ____________________________________________________________

def target(driver, args):
    return main

def test_interpreted():
    # takes forever if the Python process is already big...
    import subprocess
    me = os.path.basename(__file__)
    if me.endswith('pyc') or me.endswith('pyo'):
        me = me[:-1]
    env = os.environ.copy()
    env['PYTHONPATH'] = ''
    subprocess.check_call([sys.executable, me],
                          cwd=(os.path.dirname(__file__) or '.'),
                          env=env)

def test_compiled():
    fn = compile(main, [], gcpolicy="minimark")
    if os.path.exists(PROF_FILE):
        os.unlink(PROF_FILE)
    fn()
    assert os.path.exists(PROF_FILE)

if __name__ == '__main__':
    setup_module(None)
    res = main()
    assert res == 0