2018-04-05 22:07:33 +00:00
|
|
|
import json
|
2018-08-03 16:48:22 +00:00
|
|
|
from pathlib import Path
|
2018-04-05 22:07:33 +00:00
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2018-08-03 17:10:18 +00:00
|
|
|
sys.path.insert(
|
2018-08-06 14:01:28 +00:00
|
|
|
0, str((Path(__file__).resolve().parents[1] / 'test')))
|
2018-08-03 16:48:22 +00:00
|
|
|
|
2018-04-05 22:07:33 +00:00
|
|
|
import conftest
|
|
|
|
|
|
|
|
|
|
|
|
SKIP = set(['fft', 'hyantes'])
|
|
|
|
|
|
|
|
|
|
|
|
def run_native(hostpython, code):
|
|
|
|
output = subprocess.check_output(
|
2018-08-03 16:48:22 +00:00
|
|
|
[hostpython.resolve(), '-c', code],
|
2018-08-03 17:10:18 +00:00
|
|
|
cwd=Path(__file__).resolve().parent,
|
2018-04-05 22:07:33 +00:00
|
|
|
env={
|
|
|
|
'PYTHONPATH':
|
2018-08-06 14:01:28 +00:00
|
|
|
str(Path(__file__).resolve().parents[1] / 'src')
|
2018-08-03 16:48:22 +00:00
|
|
|
}
|
2018-04-05 22:07:33 +00:00
|
|
|
)
|
|
|
|
return float(output.strip().split()[-1])
|
|
|
|
|
|
|
|
|
2018-10-01 20:57:08 +00:00
|
|
|
def run_wasm(code, cls, port):
|
|
|
|
s = cls(port)
|
2018-04-09 14:39:52 +00:00
|
|
|
try:
|
2018-06-24 16:05:21 +00:00
|
|
|
s.load_package('numpy')
|
|
|
|
s.run(code)
|
|
|
|
try:
|
2018-10-01 20:57:08 +00:00
|
|
|
runtime = float(s.logs.split('\n')[-1])
|
2018-06-24 16:05:21 +00:00
|
|
|
except ValueError:
|
2018-10-01 20:57:08 +00:00
|
|
|
print(s.logs)
|
2018-06-24 16:05:21 +00:00
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
s.driver.quit()
|
2018-04-05 22:07:33 +00:00
|
|
|
return runtime
|
|
|
|
|
|
|
|
|
2018-10-01 20:57:08 +00:00
|
|
|
def run_all(hostpython, port, code):
|
2018-04-05 22:07:33 +00:00
|
|
|
a = run_native(hostpython, code)
|
2018-07-09 19:09:49 +00:00
|
|
|
print("native:", a)
|
2018-10-01 20:57:08 +00:00
|
|
|
b = run_wasm(code, conftest.FirefoxWrapper, port)
|
2018-07-09 19:09:49 +00:00
|
|
|
print("firefox:", b)
|
2018-10-01 20:57:08 +00:00
|
|
|
c = run_wasm(code, conftest.ChromeWrapper, port)
|
2018-07-09 19:09:49 +00:00
|
|
|
print("chrome:", c)
|
|
|
|
result = {
|
|
|
|
'native': a,
|
|
|
|
'firefox': b,
|
|
|
|
'chrome': c
|
|
|
|
}
|
2018-04-05 22:07:33 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def get_pystone_benchmarks():
|
|
|
|
yield 'pystone', (
|
|
|
|
"import pystone\n"
|
|
|
|
"pystone.main(pystone.LOOPS)\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_numpy_benchmark(filename):
|
|
|
|
lines = []
|
|
|
|
with open(filename) as fp:
|
|
|
|
for line in fp:
|
|
|
|
m = re.match('^#(setup|run): (.*)$', line)
|
|
|
|
if m:
|
|
|
|
line = '{} = {!r}\n'.format(m.group(1), m.group(2))
|
|
|
|
lines.append(line)
|
|
|
|
return ''.join(lines)
|
|
|
|
|
|
|
|
|
|
|
|
def get_numpy_benchmarks():
|
2018-10-02 17:26:42 +00:00
|
|
|
root = Path(__file__).resolve().parent / 'benchmarks'
|
2018-08-03 16:48:22 +00:00
|
|
|
for filename in root.iterdir():
|
2018-10-01 20:57:08 +00:00
|
|
|
name = filename.stem
|
2018-04-05 22:07:33 +00:00
|
|
|
if name in SKIP:
|
|
|
|
continue
|
2018-08-03 17:10:18 +00:00
|
|
|
content = parse_numpy_benchmark(filename)
|
2018-04-05 22:07:33 +00:00
|
|
|
content += (
|
2018-04-11 12:51:41 +00:00
|
|
|
"import numpy as np\n"
|
|
|
|
"_ = np.empty(())\n"
|
2018-04-05 22:07:33 +00:00
|
|
|
"setup = setup + '\\nfrom __main__ import {}'\n"
|
|
|
|
"from timeit import Timer\n"
|
|
|
|
"t = Timer(run, setup)\n"
|
|
|
|
"r = t.repeat(11, 40)\n"
|
2018-10-01 20:57:08 +00:00
|
|
|
"r.remove(min(r))\n"
|
|
|
|
"r.remove(max(r))\n"
|
2018-04-05 22:07:33 +00:00
|
|
|
"print(np.mean(r))\n".format(name))
|
|
|
|
yield name, content
|
|
|
|
|
|
|
|
|
|
|
|
def get_benchmarks():
|
|
|
|
yield from get_pystone_benchmarks()
|
|
|
|
yield from get_numpy_benchmarks()
|
|
|
|
|
|
|
|
|
|
|
|
def main(hostpython):
|
2018-10-01 20:57:08 +00:00
|
|
|
with conftest.spawn_web_server() as (hostname, port, log_path):
|
|
|
|
results = {}
|
|
|
|
for k, v in get_benchmarks():
|
|
|
|
print(k)
|
|
|
|
results[k] = run_all(hostpython, port, v)
|
2018-04-05 22:07:33 +00:00
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2018-08-03 17:10:18 +00:00
|
|
|
results = main(Path(sys.argv[-2]).resolve())
|
2018-04-05 22:07:33 +00:00
|
|
|
with open(sys.argv[-1], 'w') as fp:
|
|
|
|
json.dump(results, fp)
|