[pypy-svn] r59519 - in pypy/build/benchmem: . testing

fijal at codespeak.net fijal at codespeak.net
Wed Oct 29 16:02:20 CET 2008


Author: fijal
Date: Wed Oct 29 16:02:18 2008
New Revision: 59519

Added:
   pypy/build/benchmem/testing/test_tracing.py   (contents, props changed)
   pypy/build/benchmem/tracer.py   (contents, props changed)
Log:
Support for tracing


Added: pypy/build/benchmem/testing/test_tracing.py
==============================================================================
--- (empty file)
+++ pypy/build/benchmem/testing/test_tracing.py	Wed Oct 29 16:02:18 2008
@@ -0,0 +1,31 @@
+
+from tracer import run_with_tracing, trace_in_another_process
+import py
+
+def test_base_tracing():
+    def g():
+        pass
+    
+    def f():
+        for i in range(100):
+            g()
+
+    l = []
+    results = run_with_tracing(f, lambda : l.append('line'))
+    assert len(l) == 503
+
+def test_tracing_in_other_process():
+    tmpdir = py.test.ensuretemp('tracing_in_other_process')
+    filename = tmpdir.join('somefile.py')
+    source = py.code.Source("""
+    def g():
+        pass
+    
+    def f():
+        for i in range(100):
+            g()
+    """)
+    l = []
+    trace_in_another_process(filename, source, 'f', lambda : l.append(None))
+    assert len(l) == 503
+    

Added: pypy/build/benchmem/tracer.py
==============================================================================
--- (empty file)
+++ pypy/build/benchmem/tracer.py	Wed Oct 29 16:02:18 2008
@@ -0,0 +1,44 @@
+
+import sys, py, os
+
+def wrapper(func_to_run):
+    def f(frame, event, arg):
+        func_to_run()
+        return f
+    return f
+
+def run_with_tracing(f, func_to_run):
+    sys.settrace(wrapper(func_to_run))
+    f()
+    sys.settrace(None)
+
+def trace_in_another_process(filename, source_code, func_name, measure_func):
+    source = py.code.Source(source_code, """
+    import sys
+    sys.path.insert(0, '%s')
+    from tracer import run_with_tracing
+
+    def write():
+        sys.stdout.write('c')
+        sys.stdout.flush()
+        sys.stdin.read(1)
+
+    if __name__ == '__main__':
+        run_with_tracing(%s, write)
+        sys.stdout.write('F')
+        sys.stdout.flush()
+    """ % (py.magic.autopath().dirpath(), func_name))
+
+    f = py.path.local(filename)
+    f.write(source)
+    cmd = "%s -u %s" % (sys.executable, f)
+    stdout, stdin = os.popen2(cmd)
+    while 1:
+        ch = stdin.read(1)
+        if ch == 'F':
+            break
+        assert ch == 'c'
+        stdout.write('c')
+        stdout.flush()
+        measure_func()
+



More information about the Pypy-commit mailing list