[pypy-svn] r31882 - in pypy/dist/pypy/jit: codegen codegen/i386/test timeshifter/test

arigo at codespeak.net arigo at codespeak.net
Thu Aug 31 15:48:50 CEST 2006


Author: arigo
Date: Thu Aug 31 15:48:48 2006
New Revision: 31882

Added:
   pypy/dist/pypy/jit/codegen/conftest.py   (contents, props changed)
Modified:
   pypy/dist/pypy/jit/codegen/i386/test/test_genc_ts.py
   pypy/dist/pypy/jit/timeshifter/test/test_tlr.py
Log:
Add a conftest providing a --brenchmark option.  With this option, the
test_genc_* tests print iterations/second stats when running the
assembler code that they just generated.



Added: pypy/dist/pypy/jit/codegen/conftest.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/jit/codegen/conftest.py	Thu Aug 31 15:48:48 2006
@@ -0,0 +1,55 @@
+import py, time, os
+
+#  Usage:  py.test --brenchmark
+#
+#  This module provides an RPython class to use as follows:
+#
+#      bench = Benchmark()
+#      while 1:
+#          do_something
+#          if bench.stop():
+#              break
+
+Option = py.test.Config.Option
+
+option = py.test.Config.addoptions("pypy options", 
+        Option('--benchmark', action="store_true",
+               dest="benchmark", default=False,
+               help="give benchmarks in tests that support it"),
+    )
+option._freeze_ = lambda: True
+
+
+class Benchmark(object):
+    RUN_TIME = 2.0    # repeat the benchmarked loop for two seconds
+
+    def __init__(self):
+        self.starttime = time.time()
+        self.iterations = 0
+        self.million_iterations = 0
+        self.nextcheck = 0
+
+    def stop(self):
+        iterations = self.iterations = self.iterations + 1
+        if not option.benchmark:    # only run once if not benchmarking
+            return True
+        if iterations < self.nextcheck:
+            return False    # continue, don't call time.time() too often
+        now = time.time()
+        if now - self.starttime < self.RUN_TIME:
+            if iterations > 1000000:   # avoid wrap-around trouble
+                self.million_iterations += 1
+                self.iterations -= 1000000
+                self.nextcheck = 200000
+            else:
+                self.nextcheck = iterations * 5 // 4
+            return False    # continue looping
+        self.endtime = now
+        self.print_report()
+        return True
+
+    def print_report(self):
+        elapsed = self.endtime - self.starttime
+        iterations = float(self.million_iterations) * 1000000.0
+        iterations += self.iterations
+        os.write(1, '{%f iterations/second}\n' % (iterations / elapsed))

Modified: pypy/dist/pypy/jit/codegen/i386/test/test_genc_ts.py
==============================================================================
--- pypy/dist/pypy/jit/codegen/i386/test/test_genc_ts.py	(original)
+++ pypy/dist/pypy/jit/codegen/i386/test/test_genc_ts.py	Thu Aug 31 15:48:48 2006
@@ -1,4 +1,4 @@
-import os
+import os, sys
 from pypy.annotation import model as annmodel
 from pypy.annotation.listdef import s_list_of_strings
 from pypy.rpython.objectmodel import keepalive_until_here
@@ -6,6 +6,7 @@
 from pypy.translator.c.genc import CStandaloneBuilder
 from pypy.rpython.unroll import unrolling_iterable
 from pypy.jit.codegen.i386.ri386genop import RI386GenOp
+from pypy.jit.codegen.conftest import Benchmark
 
 
 class I386TimeshiftingTestMixin(object):
@@ -77,7 +78,11 @@
             rgenop = RGenOp()
             generated = ml_generate_code(rgenop, *mainargs)
             os.write(1, SEPLINE)
-            res = generated(*residualargs)
+            bench = Benchmark()
+            while 1:
+                res = generated(*residualargs)
+                if bench.stop():
+                    break
             os.write(1, convert_result(res) + '\n')
             keepalive_until_here(rgenop)    # to keep the code blocks alive
             return 0
@@ -109,8 +114,14 @@
         mainargs = ' '.join([str(arg) for arg in mainargs])
 
         output = self.main_cbuilder.cmdexec(mainargs)
-        assert output.startswith(self.SEPLINE)
-        lastline = output[len(self.SEPLINE):].strip()
+        lines = output.splitlines()
+        assert lines[0] == self.SEPLINE[:-1]
+        if (lines[1].startswith('{') and
+            lines[1].endswith('iterations/second}')):
+            testname = sys._getframe(1).f_code.co_name
+            os.write(2, '\n{%s: %s' % (testname, lines.pop(1)[1:]))
+        assert len(lines) == 2
+        lastline = lines[1]
         if hasattr(ll_function, 'convert_result'):
             return lastline
         else:

Modified: pypy/dist/pypy/jit/timeshifter/test/test_tlr.py
==============================================================================
--- pypy/dist/pypy/jit/timeshifter/test/test_tlr.py	(original)
+++ pypy/dist/pypy/jit/timeshifter/test/test_tlr.py	Thu Aug 31 15:48:48 2006
@@ -18,6 +18,6 @@
             return LLSupport.to_rstr(result)
         tlr_interpret.convert_arguments = [build_bytecode, int]
 
-        res = self.timeshift(tlr_interpret, [bytecode, 9], [0],
+        res = self.timeshift(tlr_interpret, [bytecode, 1764], [0],
                              policy=P_OOPSPEC)
-        assert res == 81
+        assert res == 3111696



More information about the Pypy-commit mailing list