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

« back to all changes in this revision

Viewing changes to pypy/translator/benchmark/benchmarks.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
import os, sys, time, pickle, re, py
 
2
 
 
3
class BenchmarkFailed(Exception):
 
4
    pass
 
5
 
 
6
PYSTONE_CMD = 'from test import pystone;pystone.main(%s)'
 
7
PYSTONE_PATTERN = 'This machine benchmarks at'
 
8
PYSTONE_ASCENDING_GOOD = True
 
9
 
 
10
RICHARDS_CMD = 'from richards import *;main(iterations=%d)'
 
11
RICHARDS_PATTERN = 'Average time per iteration:'
 
12
RICHARDS_ASCENDING_GOOD = False
 
13
 
 
14
def get_result(txt, pattern):
 
15
    for line in txt.split('\n'):
 
16
        if line.startswith(pattern):
 
17
            break
 
18
    else:
 
19
        raise BenchmarkFailed
 
20
    return float(line.split()[len(pattern.split())])
 
21
 
 
22
class Benchmark(object):
 
23
    def __init__(self, name, runner, asc_good, units, check=lambda:True):
 
24
        self.name = name
 
25
        self._run = runner
 
26
        self.asc_good = asc_good
 
27
        self.units = units
 
28
        self.check = check
 
29
    def run(self, exe):
 
30
        try:
 
31
            return self._run(exe)
 
32
        except BenchmarkFailed:
 
33
            return '-FAILED-'
 
34
 
 
35
def external_dependency(dirname, svnurl, revision):
 
36
    """Check out (if necessary) a given fixed revision of a svn url."""
 
37
    dirpath = py.magic.autopath().dirpath().join(dirname)
 
38
    revtag = dirpath.join('-svn-rev-')
 
39
    if dirpath.check():
 
40
        if not revtag.check() or int(revtag.read()) != revision:
 
41
            print >> sys.stderr, ("Out-of-date benchmark checkout!"
 
42
                                  " I won't update it automatically.")
 
43
            print >> sys.stderr, ("To continue, move away or remove the "
 
44
                                  "%r directory." % (dirname,))
 
45
            sys.exit(1)
 
46
        return True
 
47
    CMD = "svn co -r%d %s@%d %s" % (revision, svnurl, revision, dirpath)
 
48
    print >> sys.stderr, CMD
 
49
    err = os.system(CMD)
 
50
    if err != 0:
 
51
        print >> sys.stderr, "* checkout failed, skipping this benchmark"
 
52
        return False
 
53
    revtag.write(str(revision))
 
54
    return True
 
55
 
 
56
def run_cmd(cmd):
 
57
    #print "running", cmd
 
58
    pipe = os.popen(cmd + ' 2>&1')
 
59
    r = pipe.read()
 
60
    status = pipe.close()
 
61
    if status:
 
62
        raise BenchmarkFailed(status)
 
63
    return r
 
64
 
 
65
def run_pystone(executable='/usr/local/bin/python', n=''):
 
66
    distdir = py.magic.autopath().dirpath().dirpath().dirpath().dirpath()
 
67
    pystone = distdir.join('lib-python').join('2.4.1').join('test').join('pystone.py')
 
68
    txt = run_cmd('"%s" "%s" %s' % (executable, pystone, n))
 
69
    return get_result(txt, PYSTONE_PATTERN)
 
70
 
 
71
def run_richards(executable='/usr/local/bin/python', n=5):
 
72
    richards = py.magic.autopath().dirpath().dirpath().join('goal').join('richards.py')
 
73
    txt = run_cmd('"%s" %s %s' % (executable, richards, n))
 
74
    return get_result(txt, RICHARDS_PATTERN)
 
75
 
 
76
def run_translate(executable='/usr/local/bin/python'):
 
77
    translate = py.magic.autopath().dirpath().dirpath().join('goal').join('translate.py')
 
78
    target = py.magic.autopath().dirpath().dirpath().join('goal').join('targetrpystonedalone.py')
 
79
    argstr = '%s %s --text --batch --backendopt --no-compile %s > /dev/null 2> /dev/null'
 
80
    T = time.time()
 
81
    status = os.system(argstr%(executable, translate, target))
 
82
    r = time.time() - T
 
83
    if status:
 
84
        raise BenchmarkFailed(status)
 
85
    return r
 
86
 
 
87
def run_docutils(executable='/usr/local/bin/python'):
 
88
    docutilssvnpath = 'docutils'    # subdir of the local dir
 
89
    translatetxt = py.magic.autopath().dirpath().dirpath().dirpath().join('doc').join('translation.txt')
 
90
    command = """import sys
 
91
sys.modules['unicodedata'] = sys # docutils need 'import unicodedata' to work, but no more...
 
92
sys.path[0:0] = ['%s', '%s/extras']
 
93
from docutils.core import publish_cmdline
 
94
publish_cmdline(writer_name='html')
 
95
"""%(docutilssvnpath, docutilssvnpath)
 
96
    T = time.time()
 
97
    pid = os.fork()
 
98
    if not pid:
 
99
        davenull = os.open('/dev/null', os.O_RDWR)
 
100
        os.dup2(davenull, 0)
 
101
        os.dup2(davenull, 1)
 
102
        os.dup2(davenull, 2)
 
103
        status = os.spawnv(os.P_WAIT, executable, [executable, '-c', command, str(translatetxt)])
 
104
        os._exit(status)
 
105
    else:
 
106
        status = os.waitpid(pid, 0)[1]
 
107
    r = time.time() - T
 
108
    if status:
 
109
        raise BenchmarkFailed(status)
 
110
    return r
 
111
 
 
112
def check_docutils():
 
113
    return False     # useless benchmark - I've seen 15% of difference
 
114
                     # between two successive runs on the same machine!
 
115
    #return external_dependency('docutils',
 
116
    #                           'svn://svn.berlios.de/docutils/trunk/docutils',
 
117
    #                           4821)
 
118
 
 
119
def run_templess(executable='/usr/local/bin/python'):
 
120
    """ run some script in the templess package
 
121
 
 
122
        templess is some simple templating language, to check out use
 
123
        'svn co -r100 http://johnnydebris.net/templess/trunk templess'
 
124
    """
 
125
    here = py.magic.autopath().dirpath()
 
126
    pypath = py.__package__.getpath().dirpath()
 
127
    templessdir = here.join('templess')
 
128
    testscript = templessdir.join('test/oneshot.py')
 
129
    command = 'PYTHONPATH="%s:%s" "%s" "%s" 100' % (here, pypath,
 
130
                                                    executable, testscript)
 
131
    txt = run_cmd(command)
 
132
    try:
 
133
        result = float([line for line in txt.split('\n') if line.strip()][-1])
 
134
    except ValueError:
 
135
        raise BenchmarkFailed
 
136
    return result
 
137
 
 
138
def check_templess():
 
139
    return external_dependency('templess',
 
140
                               'http://johnnydebris.net/templess/trunk',
 
141
                               100)
 
142
 
 
143
def run_gadfly(executable='/usr/local/bin/python'):
 
144
    """ run some tests in the gadfly pure Python database """
 
145
    here = py.magic.autopath().dirpath()
 
146
    gadfly = here.join('gadfly')
 
147
    testscript = gadfly.join('test', 'testsubset.py')
 
148
    command = 'PYTHONPATH="%s" "%s" "%s"' % (gadfly, executable, testscript)
 
149
    txt = run_cmd(command)
 
150
    lines = [line for line in txt.split('\n') if line.strip()]
 
151
    if lines[-1].strip() != 'OK':
 
152
        raise BenchmarkFailed
 
153
    lastword = lines[-2].split()[-1]
 
154
    if not lastword.endswith('s'):
 
155
        raise BenchmarkFailed
 
156
    try:
 
157
        result = float(lastword[:-1])
 
158
    except ValueError:
 
159
        raise BenchmarkFailed
 
160
    return result
 
161
 
 
162
def check_gadfly():
 
163
    return external_dependency('gadfly',
 
164
              'http://codespeak.net/svn/user/arigo/hack/pypy-hack/gadflyZip',
 
165
              40406)
 
166
 
 
167
def run_mako(executable='/usr/local/bin/python'):
 
168
    """ run some tests in the mako templating system """
 
169
    here = py.magic.autopath().dirpath()
 
170
    mako = here.join('mako')
 
171
    testscript = mako.join('examples', 'bench', 'basic.py')
 
172
    command = 'PYTHONPATH="%s" "%s" "%s" mako' % (mako.join('lib'),
 
173
                                                  executable, testscript)
 
174
    txt = run_cmd(command)
 
175
    lines = [line for line in txt.split('\n') if line.strip()]
 
176
    words = lines[-1].split()
 
177
    if words[0] != 'Mako:':
 
178
        raise BenchmarkFailed
 
179
    try:
 
180
        result = float(words[1])
 
181
    except ValueError:
 
182
        raise BenchmarkFailed
 
183
    return result
 
184
 
 
185
def check_mako():
 
186
    return external_dependency('mako',
 
187
              'http://codespeak.net/svn/user/arigo/hack/pypy-hack/mako',
 
188
              40235)
 
189
 
 
190
def check_translate():
 
191
    return False   # XXX what should we do about the dependency on ctypes?
 
192
 
 
193
BENCHMARKS = [Benchmark('richards', run_richards, RICHARDS_ASCENDING_GOOD, 'ms'),
 
194
              Benchmark('pystone', run_pystone, PYSTONE_ASCENDING_GOOD, ''),
 
195
              Benchmark('translate', run_translate, RICHARDS_ASCENDING_GOOD, 'ms', check_translate),
 
196
              Benchmark('docutils', run_docutils, RICHARDS_ASCENDING_GOOD,
 
197
                        's', check_docutils),
 
198
              Benchmark('templess', run_templess, RICHARDS_ASCENDING_GOOD,
 
199
                        's', check_templess),
 
200
              Benchmark('gadfly2', run_gadfly, RICHARDS_ASCENDING_GOOD,
 
201
                        's', check_gadfly),
 
202
              Benchmark('mako', run_mako, RICHARDS_ASCENDING_GOOD,
 
203
                        's', check_mako),
 
204
             ]
 
205
 
 
206
BENCHMARKS_BY_NAME = {}
 
207
for _b in BENCHMARKS:
 
208
    BENCHMARKS_BY_NAME[_b.name] = _b