[pypy-svn] r58209 - pypy/build/benchmem

hpk at codespeak.net hpk at codespeak.net
Wed Sep 17 21:37:12 CEST 2008


Author: hpk
Date: Wed Sep 17 21:37:10 2008
New Revision: 58209

Added:
   pypy/build/benchmem/
   pypy/build/benchmem/benchtool.py   (contents, props changed)
   pypy/build/benchmem/microbench.py
      - copied, changed from r58176, pypy/branch/cross-compilation/pypy/translator/benchmark/microbench_mem.py
Log:
beginnings of memory benchmark tool and benchmarks (motivated but not yet fully incorporating what fijal did in the cross-compilation branch)


Added: pypy/build/benchmem/benchtool.py
==============================================================================
--- (empty file)
+++ pypy/build/benchmem/benchtool.py	Wed Sep 17 21:37:10 2008
@@ -0,0 +1,93 @@
+import py
+import os, sys
+import microbench
+class BenchRunner(object):
+    def __init__(self, executable, resultdir):
+        self.resultdir = py.path.local(resultdir)
+        self.executable = executable
+
+    def log(self, *args):
+        print " ".join(map(str, args))
+
+    def _preparebench(self, name, args):
+        arglist = ",".join(map(str, args))
+        source = py.code.Source(microbench, """
+            def checkpoint():
+                sys.stdout.write(".")
+                sys.stdin.read(1)
+            if __name__ == "__main__":
+                import os, sys
+                pid = os.getpid()
+                sys.stdout.write(str(pid))
+                sys.stdout.write("\\n")
+                %s(checkpoint, %s)
+        """ %(name, arglist))
+        p = self.resultdir.join("%s.py" %(name,)) 
+        p.write(source)
+        return p
+
+    def run_checkpointed_bench(self, name, args):
+        benchpyfile = self._preparebench(name, args)
+        self.log("created", benchpyfile)
+        cmd = "%s -u %s" %(self.executable, benchpyfile)
+        self.log("exec", cmd)
+        stdout, stdin = os.popen2(cmd)
+        pid = int(stdin.readline())
+        logpath = self.resultdir.join("%s.smaps" %(name,))
+        smaps = SmapsRecorder(pid, logpath)
+        stdout.write(".")
+        stdout.flush()
+        while not stdin.closed:
+            c = stdin.read(1)
+            if not c:
+                break
+            smaps.snapshot()
+            stdout.write(".")
+            stdout.flush()
+        self.log("finished", cmd)
+
+class SmapsRecorder:
+    SEPLINE = "="*80 + "\n"
+
+    def __init__(self, pid, logpath):
+        self.logpath = py.path.local(logpath)
+        self._file = self.logpath.open("a")
+        self.pid = pid
+        self.smapspath = py.path.local("/proc/%d/smaps" %(pid,))
+        assert self.smapspath.check()
+        self.snapshot()
+
+    def snapshot(self):
+        s = self.smapspath.read()
+        self._file.write(s)
+        self._file.write("\n")
+        self._file.write(self.SEPLINE)
+        self._file.flush()
+             
+if __name__ == '__main__':
+    pass
+
+# tests
+
+def setup_module(mod):
+    if sys.platform.find("linux") == -1:
+        py.test.skip("linux required")
+    mod.tmpdir = py.test.ensuretemp(mod.__name__)
+       
+def test_smapsrecorder():
+    tmpdir = py.test.ensuretemp("smapsrecorder")
+    logpath = tmpdir.join("logfile")
+    pid = os.getpid()
+    rec = SmapsRecorder(pid=pid, logpath=logpath)
+    s = logpath.read()
+    assert s.count(SmapsRecorder.SEPLINE) == 1
+    rec.snapshot()
+    rec.snapshot()
+    del rec
+    s = logpath.read()
+    assert s.count(SmapsRecorder.SEPLINE) == 3
+
+def test_benchrunner():
+    tmpdir = py.test.ensuretemp("benchrunner")
+    runner = BenchRunner(executable="python2.5", resultdir=tmpdir)
+    smapsfile = runner.run_checkpointed_bench("create_recursive_tuples", (10, 10))

Copied: pypy/build/benchmem/microbench.py (from r58176, pypy/branch/cross-compilation/pypy/translator/benchmark/microbench_mem.py)
==============================================================================
--- pypy/branch/cross-compilation/pypy/translator/benchmark/microbench_mem.py	(original)
+++ pypy/build/benchmem/microbench.py	Wed Sep 17 21:37:10 2008
@@ -1,59 +1,34 @@
-#!/usr/bin/env python
-import autopath
-""" This file attempts to measure how much memory is taken by various objects.
+"""
+    this file contains microbenchmarks.
 """
 
-from pypy.translator.benchmark.bench_mem import measure, smaps_measure_func
-import py
-
-def measure_func(num, pid):
-    res = smaps_measure_func(pid)
-    print num, res.private
-
-def tuples(read, write, coeff):
-    import gc
+def create_recursive_tuples(checkpoint, iter1, iter2):
     x = ()
-    for i in range(1000 * coeff):
-        x = (x,)
-    gc.collect()
-    write('x')
-    read()
-    write('e')
-
-def linked_list(read, write, coeff):
-    import gc
+    for i in range(iter1):
+        checkpoint()
+        for j in range(iter2):
+            x = (x,)
+
+class A(object):
+    def __init__(self, other):
+        self.other = other
 
+def linked_list(checkpoint, iter1, iter2):
     class A(object):
         def __init__(self, other):
             self.other = other
 
     x = None
-    for i in range(1000 * coeff):
-        x = A(x)
-    gc.collect()
-    write('x')
-    read()
-    write('e')
-
-def list_of_instances_with_int(read, write, coeff):
-    import gc
+    for i in range(iter1):
+        checkpoint()
+        for j in range(iter2):
+            x = A(x)
 
+def list_of_instances_with_int(checkpoint, iter1, iter2):
     class A(object):
         def __init__(self, x):
             self.x = x
-
-    x = [A(i) for i in range(1000 * coeff)]
-    gc.collect()
-    write('x')
-    read()
-    write('e')
-
-if __name__ == '__main__':
-    coeff = 1
-    i = 0
-    funcs = []
-    while i < 10:
-        funcs.append(lambda r, w, coeff=coeff: list_of_instances_with_int(r, w, coeff))
-        coeff *= 2
-        i += 1
-    res = measure(measure_func, funcs)
+    for i in range(iter1):
+        checkpoint()
+        for j in range(iter2):
+            x = A(x)



More information about the Pypy-commit mailing list