def main(): import_benchmarks() benchmarks = [] for name, thing in globals().iteritems(): if getattr(thing, 'benchmark', False): benchmarks.append((name, thing)) benchmarks.sort() def entry_point(argv): for name, func in benchmarks: print name, ':', func() return 0 t = Translation(entry_point, standalone=True, backend='c') c_exe = t.compile() t = Translation(entry_point, standalone=True, backend='cli') cli_exe = t.compile() c_res = run_benchmark(c_exe) cli_res = run_benchmark(cli_exe) print 'benchmark genc gencli ratio' print for name, _ in benchmarks: c_time = c_res[name] cli_time = cli_res[name] if c_time == 0: ratio = '%10s' % '---' else: ratio = '%10.2f' % (cli_time/c_time) print '%-32s %10.2f %10.2f %s' % (name, c_time, cli_time, ratio)
def test_prof_inline(): if sys.platform == 'win32': py.test.skip("instrumentation support is unix only for now") def add(a,b): return a + b - b + b - b + b - b + b - b + b - b + b - b + b def entry_point(argv): tot = 0 x = int(argv[1]) while x > 0: tot = add(tot, x) x -= 1 os.write(1, str(tot)) return 0 from pypy.translator.interactive import Translation t = Translation(entry_point, backend='c', standalone=True) # no counters t.backendopt(inline_threshold=100, profile_based_inline="500") exe = t.compile() out = py.process.cmdexec("%s 500" % exe) assert int(out) == 500*501/2 t = Translation(entry_point, backend='c', standalone=True) # counters t.backendopt(inline_threshold=all.INLINE_THRESHOLD_FOR_TEST*0.5, profile_based_inline="500") exe = t.compile() out = py.process.cmdexec("%s 500" % exe) assert int(out) == 500*501/2
def test_profopt(self): def add(a,b): return a + b - b + b - b + b - b + b - b + b - b + b - b + b def entry_point(argv): tot = 0 x = int(argv[1]) while x > 0: tot = add(tot, x) x -= 1 os.write(1, str(tot)) return 0 from pypy.translator.interactive import Translation # XXX this is mostly a "does not crash option" t = Translation(entry_point, backend='c', standalone=True, profopt="100") # no counters t.backendopt() exe = t.compile() out = py.process.cmdexec("%s 500" % exe) assert int(out) == 500*501/2 t = Translation(entry_point, backend='c', standalone=True, profopt="100", noprofopt=True) # no counters t.backendopt() exe = t.compile() out = py.process.cmdexec("%s 500" % exe) assert int(out) == 500*501/2
def test_profopt(): if sys.platform == 'win32': py.test.skip("instrumentation support is unix only for now") def add(a,b): return a + b - b + b - b + b - b + b - b + b - b + b - b + b def entry_point(argv): tot = 0 x = int(argv[1]) while x > 0: tot = add(tot, x) x -= 1 os.write(1, str(tot)) return 0 from pypy.translator.interactive import Translation # XXX this is mostly a "does not crash option" t = Translation(entry_point, backend='c', standalone=True, profopt="") # no counters t.backendopt() exe = t.compile() out = py.process.cmdexec("%s 500" % exe) assert int(out) == 500*501/2 t = Translation(entry_point, backend='c', standalone=True, profopt="", noprofopt=True) # no counters t.backendopt() exe = t.compile() out = py.process.cmdexec("%s 500" % exe) assert int(out) == 500*501/2
def test_prof_inline(self): if sys.platform == 'win32': py.test.skip("instrumentation support is unix only for now") def add(a, b): return a + b - b + b - b + b - b + b - b + b - b + b - b + b def entry_point(argv): tot = 0 x = int(argv[1]) while x > 0: tot = add(tot, x) x -= 1 os.write(1, str(tot)) return 0 from pypy.translator.interactive import Translation t = Translation(entry_point, backend='c', standalone=True) # no counters t.backendopt(inline_threshold=100, profile_based_inline="500") exe = t.compile() out = py.process.cmdexec("%s 500" % exe) assert int(out) == 500 * 501 / 2 t = Translation(entry_point, backend='c', standalone=True) # counters t.backendopt(inline_threshold=all.INLINE_THRESHOLD_FOR_TEST * 0.5, profile_based_inline="500") exe = t.compile() out = py.process.cmdexec("%s 500" % exe) assert int(out) == 500 * 501 / 2
def test_profopt(self): def add(a, b): return a + b - b + b - b + b - b + b - b + b - b + b - b + b def entry_point(argv): tot = 0 x = int(argv[1]) while x > 0: tot = add(tot, x) x -= 1 os.write(1, str(tot)) return 0 from pypy.translator.interactive import Translation # XXX this is mostly a "does not crash option" t = Translation(entry_point, backend='c', standalone=True, profopt="") # no counters t.backendopt() exe = t.compile() out = py.process.cmdexec("%s 500" % exe) assert int(out) == 500 * 501 / 2 t = Translation(entry_point, backend='c', standalone=True, profopt="", noprofopt=True) # no counters t.backendopt() exe = t.compile() out = py.process.cmdexec("%s 500" % exe) assert int(out) == 500 * 501 / 2
def test_profopt_mac_osx_bug(self): if sys.platform == 'win32': py.test.skip("no profopt on win32") def entry_point(argv): import os pid = os.fork() if pid: os.waitpid(pid, 0) else: os._exit(0) return 0 from pypy.translator.interactive import Translation # XXX this is mostly a "does not crash option" t = Translation(entry_point, backend='c', standalone=True, profopt="") # no counters t.backendopt() exe = t.compile() #py.process.cmdexec(exe) t = Translation(entry_point, backend='c', standalone=True, profopt="", noprofopt=True) # no counters t.backendopt() exe = t.compile()
def _makefunc_str_int(cls, f): def main(argv): arg0 = argv[1] arg1 = int(argv[2]) try: res = f(arg0, arg1) except MemoryError: print "MEMORY-ERROR" else: print res return 0 t = Translation(main, standalone=True, gc=cls.gcpolicy, policy=annpolicy.StrictAnnotatorPolicy(), taggedpointers=cls.taggedpointers, gcremovetypeptr=cls.removetypeptr) t.disable(['backendopt']) t.set_backend_extra_options(c_debug_defines=True) t.rtype() if conftest.option.view: t.viewcg() exename = t.compile() def run(s, i): data = py.process.cmdexec("%s %s %d" % (exename, s, i)) data = data.strip() if data == 'MEMORY-ERROR': raise MemoryError return data return run
def test_lib(): def entry_point(argv): fd = os.open("/tmp/foobar", os.O_RDONLY, 0777) assert fd == 77 res = os.read(fd, 123) assert res == "he\x00llo" count = os.write(fd, "world\x00!\x00") assert count == 42 for arg in argv: count = os.write(fd, arg) assert count == 61 os.close(fd) return 0 t = Translation(entry_point, backend='c', standalone=True, sandbox=True) exe = t.compile() proc = MySandboxedProc([exe, 'x1', 'y2'], expected=[ ("open", ("/tmp/foobar", os.O_RDONLY, 0777), 77), ("read", (77, 123), "he\x00llo"), ("write", (77, "world\x00!\x00"), 42), ("write", (77, exe), 61), ("write", (77, "x1"), 61), ("write", (77, "y2"), 61), ("close", (77, ), None), ]) proc.handle_forever() assert proc.seen == len(proc.expected)
def test_lib(): def entry_point(argv): fd = os.open("/tmp/foobar", os.O_RDONLY, 0777) assert fd == 77 res = os.read(fd, 123) assert res == "he\x00llo" count = os.write(fd, "world\x00!\x00") assert count == 42 for arg in argv: count = os.write(fd, arg) assert count == 61 os.close(fd) return 0 t = Translation(entry_point, backend='c', standalone=True, sandbox=True) exe = t.compile() proc = MySandboxedProc([exe, 'x1', 'y2'], expected = [ ("open", ("/tmp/foobar", os.O_RDONLY, 0777), 77), ("read", (77, 123), "he\x00llo"), ("write", (77, "world\x00!\x00"), 42), ("write", (77, exe), 61), ("write", (77, "x1"), 61), ("write", (77, "y2"), 61), ("close", (77,), None), ]) proc.handle_forever() assert proc.seen == len(proc.expected)
def _makefunc_str_int(cls, f): def main(argv): arg0 = argv[1] arg1 = int(argv[2]) try: res = f(arg0, arg1) except MemoryError: print "MEMORY-ERROR" else: print res return 0 t = Translation(main, standalone=True, gc=cls.gcpolicy, policy=annpolicy.StrictAnnotatorPolicy(), taggedpointers=cls.taggedpointers, gcremovetypeptr=cls.removetypeptr) t.disable(['backendopt']) t.set_backend_extra_options(c_debug_defines=True) t.rtype() if conftest.option.view: t.viewcg() exename = t.compile() def run(s, i): data = py.process.cmdexec("%s %s %d" % (exename, s, i)) data = data.strip() if data == 'MEMORY-ERROR': raise MemoryError return data return run
def run(self, entry_point, args, expected): t = Translation(entry_point, backend='c', standalone=True, sandbox=True) exe = t.compile() from pypy.translator.sandbox.sandlib import SimpleIOSandboxedProc proc = SimpleIOSandboxedProc([exe] + args) output, error = proc.communicate() assert error == '' assert output == expected
def main(): import_benchmarks() benchmarks = [] for name, thing in globals().iteritems(): if getattr(thing, 'benchmark', False): benchmarks.append((name, thing)) benchmarks.sort() def entry_point(argv): for name, func in benchmarks: print name, ':', func() return 0 t = Translation(entry_point, standalone=True, backend='c') c_exe = t.compile() t = Translation(entry_point, standalone=True, backend='cli') cli_exe = t.compile() t = Translation(entry_point, standalone=True, backend='jvm') jvm_exe = t.compile() c_res = run_benchmark(c_exe) cli_res = run_benchmark(cli_exe) jvm_res = run_benchmark(jvm_exe) print 'benchmark genc gencli cli_ratio genjvm jvm_ratio' print for name, _ in benchmarks: c_time = c_res[name] cli_time = cli_res[name] jvm_time = jvm_res[name] if c_time == 0: cli_ratio = '%10s' % '---' else: cli_ratio = '%10.2f' % (cli_time / c_time) if c_time == 0: jvm_ratio = '%10s' % '---' else: jvm_ratio = '%10.2f' % (jvm_time / c_time) print '%-32s %10.2f %10.2f %s %10.2f %s' % ( name, c_time, cli_time, cli_ratio, jvm_time, jvm_ratio)
def main(): import_benchmarks() benchmarks = [] for name, thing in globals().iteritems(): if getattr(thing, "benchmark", False): benchmarks.append((name, thing)) benchmarks.sort() def entry_point(argv): for name, func in benchmarks: print name, ":", func() return 0 t = Translation(entry_point, standalone=True, backend="c") c_exe = t.compile() t = Translation(entry_point, standalone=True, backend="cli") cli_exe = t.compile() t = Translation(entry_point, standalone=True, backend="jvm") jvm_exe = t.compile() c_res = run_benchmark(c_exe) cli_res = run_benchmark(cli_exe) jvm_res = run_benchmark(jvm_exe) print "benchmark genc gencli cli_ratio genjvm jvm_ratio" print for name, _ in benchmarks: c_time = c_res[name] cli_time = cli_res[name] jvm_time = jvm_res[name] if c_time == 0: cli_ratio = "%10s" % "---" else: cli_ratio = "%10.2f" % (cli_time / c_time) if c_time == 0: jvm_ratio = "%10s" % "---" else: jvm_ratio = "%10.2f" % (jvm_time / c_time) print "%-32s %10.2f %10.2f %s %10.2f %s" % (name, c_time, cli_time, cli_ratio, jvm_time, jvm_ratio)
def test_profopt_mac_osx_bug(self): if sys.platform == 'win32': py.test.skip("no profopt on win32") def entry_point(argv): import os pid = os.fork() if pid: os.waitpid(pid, 0) else: os._exit(0) return 0 from pypy.translator.interactive import Translation # XXX this is mostly a "does not crash option" t = Translation(entry_point, backend='c', standalone=True, profopt="") # no counters t.backendopt() exe = t.compile() #py.process.cmdexec(exe) t = Translation(entry_point, backend='c', standalone=True, profopt="", noprofopt=True) # no counters t.backendopt() exe = t.compile()
def test_dup2_access(): def entry_point(argv): os.dup2(34, 56) y = os.access("spam", 77) return 1 - y t = Translation(entry_point, backend='c', standalone=True, sandbox=True) exe = t.compile() g, f = os.popen2(exe, "t", 0) expect(f, g, "ll_os.ll_os_dup2", (34, 56), None) expect(f, g, "ll_os.ll_os_access", ("spam", 77), True) g.close() tail = f.read() f.close() assert tail == ""
def test_time(): def entry_point(argv): t = time.time() os.dup(int(t*1000)) return 0 t = Translation(entry_point, backend='c', standalone=True, sandbox=True) exe = t.compile() g, f = os.popen2(exe, "t", 0) expect(f, g, "ll_time.ll_time_time", (), 3.141592) expect(f, g, "ll_os.ll_os_dup", (3141,), 3) g.close() tail = f.read() f.close() assert tail == ""
def test_foobar(): py.test.skip("to be updated") foobar = rffi.llexternal("foobar", [rffi.CCHARP], rffi.LONG) def entry_point(argv): s = rffi.str2charp(argv[1]); n = foobar(s); rffi.free_charp(s) s = rffi.str2charp(argv[n]); n = foobar(s); rffi.free_charp(s) return n t = Translation(entry_point, backend='c', standalone=True, sandbox=True) exe = t.compile() proc = MySandboxedProc([exe, 'spam', 'egg'], expected = [ ("foobar", ("spam",), 2), ("foobar", ("egg",), 0), ]) proc.handle_forever() assert proc.seen == len(proc.expected)
def test_simple_compile_c(): def f(x, y): return x + y t = Translation(f, [int, int]) t.source(backend='c') t_f = t.compile() res = t_f(2, 3) assert res == 5 t = Translation(f, [int, int]) t_f = t.compile_c() res = t_f(2, 3) assert res == 5
def test_simple_compile_c(): def f(x,y): return x+y t = Translation(f, [int, int]) t.source(backend='c') t_f = t.compile() res = t_f(2,3) assert res == 5 t = Translation(f, [int, int]) t_f = t.compile_c() res = t_f(2,3) assert res == 5
def test_socketio(): class SocketProc(VirtualizedSocketProc, SimpleIOSandboxedProc): def build_virtual_root(self): pass def entry_point(argv): fd = os.open("tcp://google.com:80", os.O_RDONLY, 0777) os.write(fd, 'GET /\n') print os.read(fd, 30) return 0 t = Translation(entry_point, backend='c', standalone=True, sandbox=True) exe = t.compile() proc = SocketProc([exe]) output, error = proc.communicate("") assert output.startswith('<HTML><HEAD>')
def test_open_dup(): def entry_point(argv): fd = os.open("/tmp/foobar", os.O_RDONLY, 0777) assert fd == 77 fd2 = os.dup(fd) assert fd2 == 78 return 0 t = Translation(entry_point, backend='c', standalone=True, sandbox=True) exe = t.compile() g, f = os.popen2(exe, "t", 0) expect(f, g, "ll_os.ll_os_open", ("/tmp/foobar", os.O_RDONLY, 0777), 77) expect(f, g, "ll_os.ll_os_dup", (77,), 78) g.close() tail = f.read() f.close() assert tail == ""
def test_simple_compile_c_isolate(): from pypy.tool import isolate def f(x, y): return x + y t = Translation(f, [int, int]) t.set_backend_extra_options(c_isolated=True) t_f = t.compile() assert isinstance(t_f, isolate.IsolateInvoker) res = t_f(2, 3) assert res == 5 # cleanup t_f.close_isolate()
def test_stat_ftruncate(): from pypy.rpython.module.ll_os_stat import s_StatResult from pypy.rlib.rarithmetic import r_longlong r0x12380000007 = r_longlong(0x12380000007) def entry_point(argv): st = os.stat("somewhere") os.ftruncate(st.st_mode, st.st_size) # nonsense, just to see outside return 0 t = Translation(entry_point, backend='c', standalone=True, sandbox=True) exe = t.compile() g, f = os.popen2(exe, "t", 0) st = os.stat_result((55, 0, 0, 0, 0, 0, 0x12380000007, 0, 0, 0)) expect(f, g, "ll_os.ll_os_stat", ("somewhere",), st, resulttype = s_StatResult) expect(f, g, "ll_os.ll_os_ftruncate", (55, 0x12380000007), None) g.close() tail = f.read() f.close() assert tail == ""
def test_simpleio(): def entry_point(argv): print "Please enter a number:" buf = "" while True: t = os.read(0, 1) # 1 character from stdin if not t: raise EOFError if t == '\n': break buf += t num = int(buf) print "The double is:", num * 2 return 0 t = Translation(entry_point, backend='c', standalone=True, sandbox=True) exe = t.compile() proc = SimpleIOSandboxedProc([exe, 'x1', 'y2']) output, error = proc.communicate("21\n") assert output == "Please enter a number:\nThe double is: 42\n" assert error == ""
def test_simpleio(): def entry_point(argv): print "Please enter a number:" buf = "" while True: t = os.read(0, 1) # 1 character from stdin if not t: raise EOFError if t == '\n': break buf += t num = int(buf) print "The double is:", num * 2 return 0 t = Translation(entry_point, backend='c', standalone=True, sandbox=True) exe = t.compile() proc = SimpleIOSandboxedProc([exe, 'x1', 'y2']) output, error = proc.communicate("21\n") assert output == "Please enter a number:\nThe double is: 42\n" assert error == ""
def test_read_write(): def entry_point(argv): fd = os.open("/tmp/foobar", os.O_RDONLY, 0777) assert fd == 77 res = os.read(fd, 123) assert res == "he\x00llo" count = os.write(fd, "world\x00!\x00") assert count == 42 os.close(fd) return 0 t = Translation(entry_point, backend='c', standalone=True, sandbox=True) exe = t.compile() g, f = os.popen2(exe, "t", 0) expect(f, g, "ll_os.ll_os_open", ("/tmp/foobar", os.O_RDONLY, 0777), 77) expect(f, g, "ll_os.ll_os_read", (77, 123), "he\x00llo") expect(f, g, "ll_os.ll_os_write", (77, "world\x00!\x00"), 42) expect(f, g, "ll_os.ll_os_close", (77,), None) g.close() tail = f.read() f.close() assert tail == ""
def test_foobar(): py.test.skip("to be updated") foobar = rffi.llexternal("foobar", [rffi.CCHARP], rffi.LONG) def entry_point(argv): s = rffi.str2charp(argv[1]) n = foobar(s) rffi.free_charp(s) s = rffi.str2charp(argv[n]) n = foobar(s) rffi.free_charp(s) return n t = Translation(entry_point, backend='c', standalone=True, sandbox=True) exe = t.compile() proc = MySandboxedProc([exe, 'spam', 'egg'], expected=[ ("foobar", ("spam", ), 2), ("foobar", ("egg", ), 0), ]) proc.handle_forever() assert proc.seen == len(proc.expected)
print os.read(fd, 30) return 0 t = Translation(entry_point, backend='c', standalone=True, sandbox=True) exe = t.compile() proc = SocketProc([exe]) output, error = proc.communicate("") assert output.startswith('<HTML><HEAD>') def test_oserror(): def entry_point(argv): try: os.open("/tmp/foobar", os.O_RDONLY, 0777) except OSError, e: os.close(e.errno) # nonsense, just to see outside return 0 t = Translation(entry_point, backend='c', standalone=True, sandbox=True) exe = t.compile() proc = MySandboxedProc([exe], expected=[ ("open", ("/tmp/foobar", os.O_RDONLY, 0777), OSError(-42, "baz")), ("close", (-42, ), None), ]) proc.handle_forever() assert proc.seen == len(proc.expected)
def setup_module(mod): t = Translation(mini_pypy_like_entry_point, backend='c', standalone=True, sandbox=True) mod.executable = str(t.compile())
def compile(f, gc='ref'): t = Translation(f, backend='c', standalone=True, sandbox=True, gc=gc) return str(t.compile())
def setup_module(mod): t = Translation(mini_pypy_like_entry_point, backend='c', standalone=True, sandbox=True) mod.executable = str(t.compile())
def compile(f): t = Translation(f, backend="c", standalone=True, sandbox=True, gc="ref") return str(t.compile())
expect(f, g, "ll_os.ll_os_dup", (3141,), 3) g.close() tail = f.read() f.close() assert tail == "" def test_oserror(): def entry_point(argv): try: os.stat("somewhere") except OSError, e: os.close(e.errno) # nonsense, just to see outside return 0 t = Translation(entry_point, backend='c', standalone=True, sandbox=True) exe = t.compile() g, f = os.popen2(exe, "t", 0) expect(f, g, "ll_os.ll_os_stat", ("somewhere",), OSError(6321, "egg")) expect(f, g, "ll_os.ll_os_close", (6321,), None) g.close() tail = f.read() f.close() assert tail == "" class TestPrintedResults: def run(self, entry_point, args, expected): t = Translation(entry_point, backend='c', standalone=True, sandbox=True) exe = t.compile() from pypy.translator.sandbox.sandlib import SimpleIOSandboxedProc
def compile(f): t = Translation(f, backend='c', standalone=True, sandbox=True, gc='ref') return str(t.compile())