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

hpk at codespeak.net hpk at codespeak.net
Wed Sep 17 22:21:16 CEST 2008


Author: hpk
Date: Wed Sep 17 22:21:14 2008
New Revision: 58210

Added:
   pypy/build/benchmem/benchmark/
   pypy/build/benchmem/benchmark/create_recursive_tuples.py
      - copied, changed from r58209, pypy/build/benchmem/microbench.py
   pypy/build/benchmem/benchmark/list_of_instances_with_ints.py
      - copied, changed from r58209, pypy/build/benchmem/microbench.py
   pypy/build/benchmem/benchmark/simple_linked_instances.py
      - copied, changed from r58209, pypy/build/benchmem/microbench.py
   pypy/build/benchmem/testing/
   pypy/build/benchmem/testing/__init__.py
   pypy/build/benchmem/testing/test_benchtool.py
      - copied, changed from r58209, pypy/build/benchmem/benchtool.py
Removed:
   pypy/build/benchmem/microbench.py
Modified:
   pypy/build/benchmem/benchtool.py
Log:
cleanups, move benchmark to own directory, create testing dir


Copied: pypy/build/benchmem/benchmark/create_recursive_tuples.py (from r58209, pypy/build/benchmem/microbench.py)
==============================================================================
--- pypy/build/benchmem/microbench.py	(original)
+++ pypy/build/benchmem/benchmark/create_recursive_tuples.py	Wed Sep 17 22:21:14 2008
@@ -1,34 +1,8 @@
-"""
-    this file contains microbenchmarks.
-"""
 
-def create_recursive_tuples(checkpoint, iter1, iter2):
+def bench_create_recursive_tuples(checkpoint, iter1, iter2):
     x = ()
     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(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
-    for i in range(iter1):
-        checkpoint()
-        for j in range(iter2):
-            x = A(x)

Copied: pypy/build/benchmem/benchmark/list_of_instances_with_ints.py (from r58209, pypy/build/benchmem/microbench.py)
==============================================================================
--- pypy/build/benchmem/microbench.py	(original)
+++ pypy/build/benchmem/benchmark/list_of_instances_with_ints.py	Wed Sep 17 22:21:14 2008
@@ -1,34 +1,12 @@
-"""
-    this file contains microbenchmarks.
-"""
-
-def create_recursive_tuples(checkpoint, iter1, iter2):
-    x = ()
-    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(iter1):
-        checkpoint()
-        for j in range(iter2):
-            x = A(x)
+    def __init__(self, x, y):
+        self.x = x
+        self.y = y
 
-def list_of_instances_with_int(checkpoint, iter1, iter2):
-    class A(object):
-        def __init__(self, x):
-            self.x = x
+def bench_list_of_instances_with_ints(checkpoint, iter1, iter2):
+    l = []
     for i in range(iter1):
         checkpoint()
         for j in range(iter2):
-            x = A(x)
+            l.append(A(i, j))

Copied: pypy/build/benchmem/benchmark/simple_linked_instances.py (from r58209, pypy/build/benchmem/microbench.py)
==============================================================================
--- pypy/build/benchmem/microbench.py	(original)
+++ pypy/build/benchmem/benchmark/simple_linked_instances.py	Wed Sep 17 22:21:14 2008
@@ -1,34 +1,10 @@
-"""
-    this file contains microbenchmarks.
-"""
-
-def create_recursive_tuples(checkpoint, iter1, iter2):
-    x = ()
-    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
-
+def bench_linked_list(checkpoint, iter1, iter2):
     x = None
     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
-    for i in range(iter1):
-        checkpoint()
-        for j in range(iter2):
-            x = A(x)

Modified: pypy/build/benchmem/benchtool.py
==============================================================================
--- pypy/build/benchmem/benchtool.py	(original)
+++ pypy/build/benchmem/benchtool.py	Wed Sep 17 22:21:14 2008
@@ -1,6 +1,8 @@
 import py
 import os, sys
-import microbench
+mydir = py.magic.autopath().dirpath()
+benchmarkdir = mydir.join("benchmark")
+ 
 class BenchRunner(object):
     def __init__(self, executable, resultdir):
         self.resultdir = py.path.local(resultdir)
@@ -9,9 +11,19 @@
     def log(self, *args):
         print " ".join(map(str, args))
 
-    def _preparebench(self, name, args):
+    def getbenchpath(self, filename):
+        return benchmarkdir.join(filename)
+
+    def _preparebench(self, filename, args):
+        path = self.getbenchpath(filename)
+        for name, obj in vars(path.pyimport()).items():
+            if name.startswith("bench") and callable(obj):
+                break
+        else:
+            raise LookupError("no benchmark found in %s" %(path,))
+            
         arglist = ",".join(map(str, args))
-        source = py.code.Source(microbench, """
+        source = py.code.Source(path.read(), """
             def checkpoint():
                 sys.stdout.write(".")
                 sys.stdin.read(1)
@@ -26,14 +38,14 @@
         p.write(source)
         return p
 
-    def run_checkpointed_bench(self, name, args):
-        benchpyfile = self._preparebench(name, args)
+    def run_checkpointed_bench(self, filename, args):
+        benchpyfile = self._preparebench(filename, 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,))
+        logpath = benchpyfile.new(ext="smaps")
         smaps = SmapsRecorder(pid, logpath)
         stdout.write(".")
         stdout.flush()
@@ -45,6 +57,7 @@
             stdout.write(".")
             stdout.flush()
         self.log("finished", cmd)
+        return logpath
 
 class SmapsRecorder:
     SEPLINE = "="*80 + "\n"
@@ -67,27 +80,3 @@
 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))

Added: pypy/build/benchmem/testing/__init__.py
==============================================================================
--- (empty file)
+++ pypy/build/benchmem/testing/__init__.py	Wed Sep 17 22:21:14 2008
@@ -0,0 +1 @@
+#

Copied: pypy/build/benchmem/testing/test_benchtool.py (from r58209, pypy/build/benchmem/benchtool.py)
==============================================================================
--- pypy/build/benchmem/benchtool.py	(original)
+++ pypy/build/benchmem/testing/test_benchtool.py	Wed Sep 17 22:21:14 2008
@@ -1,78 +1,11 @@
 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
+from benchtool import SmapsRecorder, BenchRunner, benchmarkdir
 
 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")
@@ -90,4 +23,11 @@
 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))
+    def checker(name, *args):
+        smapsfile = runner.run_checkpointed_bench(name, args)
+        assert smapsfile.check()
+
+    for name in benchmarkdir.listdir("*.py"):
+        if name.basename[0] != "_": 
+            yield checker, name.basename, 10, 10
+



More information about the Pypy-commit mailing list