~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/rpython/microbench/microbench.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
import sys
 
4
import autopath
 
5
from time import clock
 
6
from py.compat import subprocess
 
7
from pypy.translator.interactive import Translation
 
8
 
 
9
LOOPS = 10000000
 
10
 
 
11
class MetaBench(type):
 
12
    def __new__(self, cls_name, bases, cls_dict):
 
13
        loop = cls_dict['loop']
 
14
        loop.dont_inline = True
 
15
        myglob = {
 
16
            'init': cls_dict['init'],
 
17
            'loop': loop,
 
18
            'LOOPS': cls_dict.get('LOOPS', LOOPS),
 
19
            'clock': clock,
 
20
            }
 
21
        args = ', '.join(cls_dict['args'])
 
22
        source = """
 
23
def %(cls_name)s():
 
24
    obj = init()
 
25
    start = clock()
 
26
    for i in xrange(LOOPS):
 
27
        loop(%(args)s)
 
28
    return clock() - start
 
29
""" % locals()
 
30
        exec source in myglob
 
31
        func = myglob[cls_name]
 
32
        func.benchmark = True
 
33
        return func
 
34
 
 
35
 
 
36
def run_benchmark(exe):
 
37
    from pypy.translator.cli.test.runtest import CliFunctionWrapper
 
38
    if isinstance(exe, CliFunctionWrapper):
 
39
        stdout, stderr, retval = exe.run()
 
40
    else:
 
41
        assert isinstance(exe, str)
 
42
        bench = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
43
        stdout, stderr = bench.communicate()
 
44
        retval = bench.wait()
 
45
 
 
46
    if retval != 0:
 
47
        print 'Running benchmark failed'
 
48
        print 'Standard Output:'
 
49
        print stdout
 
50
        print '-' * 40
 
51
        print 'Standard Error:'
 
52
        print stderr
 
53
        raise SystemExit(-1)
 
54
 
 
55
    mydict = {}
 
56
    for line in stdout.splitlines():
 
57
        name, res = line.split(':')
 
58
        mydict[name.strip()] = float(res)
 
59
    return mydict
 
60
 
 
61
def import_benchmarks():
 
62
    modules = sys.argv[1:]
 
63
    if len(modules) == 0:
 
64
        # import all the microbenchs
 
65
        from glob import glob
 
66
        for module in glob('*.py'):
 
67
            if module not in ('__init__.py', 'autopath.py', 'microbench.py'):
 
68
                modules.append(module)
 
69
 
 
70
    for module in modules:
 
71
        module = module.rstrip('.py')
 
72
        exec 'from %s import *' % module in globals()
 
73
 
 
74
def main():
 
75
    import_benchmarks()
 
76
    benchmarks = []
 
77
    for name, thing in globals().iteritems():
 
78
        if getattr(thing, 'benchmark', False):
 
79
            benchmarks.append((name, thing))
 
80
    benchmarks.sort()
 
81
    
 
82
    def entry_point(argv):
 
83
        for name, func in benchmarks:
 
84
            print name, ':', func()
 
85
        return 0
 
86
 
 
87
    t = Translation(entry_point, standalone=True, backend='c')
 
88
    c_exe = t.compile()
 
89
    t = Translation(entry_point, standalone=True, backend='cli')
 
90
    cli_exe = t.compile()
 
91
 
 
92
    c_res = run_benchmark(c_exe)
 
93
    cli_res = run_benchmark(cli_exe)
 
94
 
 
95
    print 'benchmark                              genc     gencli       ratio'
 
96
    print
 
97
    for name, _ in benchmarks:
 
98
        c_time = c_res[name]
 
99
        cli_time = cli_res[name]
 
100
        if c_time == 0:
 
101
            ratio = '%10s' % '---'
 
102
        else:
 
103
            ratio = '%10.2f' % (cli_time/c_time)
 
104
        print '%-32s %10.2f %10.2f %s' % (name, c_time, cli_time, ratio)
 
105
 
 
106
if __name__ == '__main__':
 
107
    main()