1
import os, sys, time, pickle, re, py
3
class BenchmarkFailed(Exception):
6
PYSTONE_CMD = 'from test import pystone;pystone.main(%s)'
7
PYSTONE_PATTERN = 'This machine benchmarks at'
8
PYSTONE_ASCENDING_GOOD = True
10
RICHARDS_CMD = 'from richards import *;main(iterations=%d)'
11
RICHARDS_PATTERN = 'Average time per iteration:'
12
RICHARDS_ASCENDING_GOOD = False
14
def get_result(txt, pattern):
15
for line in txt.split('\n'):
16
if line.startswith(pattern):
20
return float(line.split()[len(pattern.split())])
22
class Benchmark(object):
23
def __init__(self, name, runner, asc_good, units, check=lambda:True):
26
self.asc_good = asc_good
32
except BenchmarkFailed:
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-')
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,))
47
CMD = "svn co -r%d %s@%d %s" % (revision, svnurl, revision, dirpath)
48
print >> sys.stderr, CMD
51
print >> sys.stderr, "* checkout failed, skipping this benchmark"
53
revtag.write(str(revision))
58
pipe = os.popen(cmd + ' 2>&1')
62
raise BenchmarkFailed(status)
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)
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)
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'
81
status = os.system(argstr%(executable, translate, target))
84
raise BenchmarkFailed(status)
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)
99
davenull = os.open('/dev/null', os.O_RDWR)
103
status = os.spawnv(os.P_WAIT, executable, [executable, '-c', command, str(translatetxt)])
106
status = os.waitpid(pid, 0)[1]
109
raise BenchmarkFailed(status)
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',
119
def run_templess(executable='/usr/local/bin/python'):
120
""" run some script in the templess package
122
templess is some simple templating language, to check out use
123
'svn co -r100 http://johnnydebris.net/templess/trunk templess'
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)
133
result = float([line for line in txt.split('\n') if line.strip()][-1])
135
raise BenchmarkFailed
138
def check_templess():
139
return external_dependency('templess',
140
'http://johnnydebris.net/templess/trunk',
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
157
result = float(lastword[:-1])
159
raise BenchmarkFailed
163
return external_dependency('gadfly',
164
'http://codespeak.net/svn/user/arigo/hack/pypy-hack/gadflyZip',
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
180
result = float(words[1])
182
raise BenchmarkFailed
186
return external_dependency('mako',
187
'http://codespeak.net/svn/user/arigo/hack/pypy-hack/mako',
190
def check_translate():
191
return False # XXX what should we do about the dependency on ctypes?
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,
202
Benchmark('mako', run_mako, RICHARDS_ASCENDING_GOOD,
206
BENCHMARKS_BY_NAME = {}
207
for _b in BENCHMARKS:
208
BENCHMARKS_BY_NAME[_b.name] = _b