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
|
2019-07-01 12:38:18 +00:00
|
|
|
from time import time
|
2018-04-05 22:07:33 +00:00
|
|
|
|
2020-06-28 18:24:40 +00:00
|
|
|
sys.path.insert(0, str((Path(__file__).resolve().parents[1] / "test")))
|
|
|
|
sys.path.insert(0, str((Path(__file__).resolve().parents[1])))
|
2018-08-03 16:48:22 +00:00
|
|
|
|
2018-10-04 11:24:57 +00:00
|
|
|
import conftest # noqa: E402
|
2018-04-05 22:07:33 +00:00
|
|
|
|
|
|
|
|
2020-06-28 18:24:40 +00:00
|
|
|
SKIP = set(["fft", "hyantes", "README"])
|
2018-04-05 22:07:33 +00:00
|
|
|
|
|
|
|
|
2019-07-01 12:38:18 +00:00
|
|
|
def print_entry(name, res):
|
|
|
|
print(" - ", name)
|
2020-06-28 18:24:40 +00:00
|
|
|
print(" " * 4, end="")
|
2019-07-01 12:38:18 +00:00
|
|
|
for name, dt in res.items():
|
|
|
|
print("{}: {:.6f} ".format(name, dt), end="")
|
2020-06-28 18:24:40 +00:00
|
|
|
print("")
|
2019-07-01 12:38:18 +00:00
|
|
|
|
|
|
|
|
2018-04-05 22:07:33 +00:00
|
|
|
def run_native(hostpython, code):
|
2020-12-07 23:19:16 +00:00
|
|
|
root = Path(__file__).resolve().parents[1]
|
2018-04-05 22:07:33 +00:00
|
|
|
output = subprocess.check_output(
|
2020-06-28 18:24:40 +00:00
|
|
|
[hostpython.resolve(), "-c", code],
|
2018-08-03 17:10:18 +00:00
|
|
|
cwd=Path(__file__).resolve().parent,
|
2020-12-07 23:19:16 +00:00
|
|
|
env={
|
|
|
|
"PYTHONPATH": str(root / "src")
|
|
|
|
+ ":"
|
|
|
|
+ str(root / "packages" / ".artifacts" / "lib" / "python")
|
|
|
|
},
|
2018-04-05 22:07:33 +00:00
|
|
|
)
|
|
|
|
return float(output.strip().split()[-1])
|
|
|
|
|
|
|
|
|
2019-07-01 12:38:18 +00:00
|
|
|
def run_wasm(code, selenium):
|
|
|
|
selenium.run(code)
|
2018-04-09 14:39:52 +00:00
|
|
|
try:
|
2020-06-28 18:24:40 +00:00
|
|
|
runtime = float(selenium.logs.split("\n")[-1])
|
2019-07-01 12:38:18 +00:00
|
|
|
except ValueError:
|
|
|
|
print(selenium.logs)
|
|
|
|
raise
|
2018-04-05 22:07:33 +00:00
|
|
|
return runtime
|
|
|
|
|
|
|
|
|
2019-07-01 12:38:18 +00:00
|
|
|
def run_all(hostpython, selenium_backends, code):
|
2018-04-05 22:07:33 +00:00
|
|
|
a = run_native(hostpython, code)
|
2020-06-28 18:24:40 +00:00
|
|
|
result = {"native": a}
|
2019-07-01 12:38:18 +00:00
|
|
|
for browser_name, selenium in selenium_backends.items():
|
|
|
|
dt = run_wasm(code, selenium)
|
|
|
|
result[browser_name] = dt
|
2018-04-05 22:07:33 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def get_pystone_benchmarks():
|
2020-06-28 18:24:40 +00:00
|
|
|
yield "pystone", ("import pystone\n" "pystone.main(pystone.LOOPS)\n")
|
2018-04-05 22:07:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def parse_numpy_benchmark(filename):
|
|
|
|
lines = []
|
|
|
|
with open(filename) as fp:
|
|
|
|
for line in fp:
|
2020-06-28 18:24:40 +00:00
|
|
|
m = re.match(r"^#\s*(setup|run): (.*)$", line)
|
2018-04-05 22:07:33 +00:00
|
|
|
if m:
|
2020-06-28 18:24:40 +00:00
|
|
|
line = "{} = {!r}\n".format(m.group(1), m.group(2))
|
2018-04-05 22:07:33 +00:00
|
|
|
lines.append(line)
|
2020-06-28 18:24:40 +00:00
|
|
|
return "".join(lines)
|
2018-04-05 22:07:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_numpy_benchmarks():
|
2020-06-28 18:24:40 +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"
|
2020-06-28 18:24:40 +00:00
|
|
|
"print(np.mean(r))\n".format(name)
|
|
|
|
)
|
2018-04-05 22:07:33 +00:00
|
|
|
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 = {}
|
2019-07-01 12:38:18 +00:00
|
|
|
selenium_backends = {}
|
|
|
|
|
2020-06-28 18:24:40 +00:00
|
|
|
b = {"native": float("NaN")}
|
|
|
|
browser_cls = [
|
|
|
|
("firefox", conftest.FirefoxWrapper),
|
|
|
|
("chrome", conftest.ChromeWrapper),
|
|
|
|
]
|
2019-07-01 12:38:18 +00:00
|
|
|
for name, cls in browser_cls:
|
|
|
|
t0 = time()
|
|
|
|
selenium_backends[name] = cls(port)
|
|
|
|
b[name] = time() - t0
|
|
|
|
# pre-load numpy for the selenium instance used in benchmarks
|
|
|
|
selenium_backends[name].load_package("numpy")
|
2020-06-28 18:24:40 +00:00
|
|
|
results["selenium init"] = b
|
2019-07-01 12:38:18 +00:00
|
|
|
print_entry("selenium init", b)
|
|
|
|
|
|
|
|
# load packages
|
2020-06-27 10:24:28 +00:00
|
|
|
for package_name in ["numpy"]:
|
2020-06-28 18:24:40 +00:00
|
|
|
b = {"native": float("NaN")}
|
2019-07-01 12:38:18 +00:00
|
|
|
for browser_name, cls in browser_cls:
|
|
|
|
selenium = cls(port)
|
|
|
|
try:
|
|
|
|
t0 = time()
|
|
|
|
selenium.load_package(package_name)
|
|
|
|
b[browser_name] = time() - t0
|
|
|
|
finally:
|
|
|
|
selenium.driver.quit()
|
2020-06-28 18:24:40 +00:00
|
|
|
results["load " + package_name] = b
|
|
|
|
print_entry("load " + package_name, b)
|
2019-07-01 12:38:18 +00:00
|
|
|
|
|
|
|
for name, content in get_benchmarks():
|
|
|
|
results[name] = run_all(hostpython, selenium_backends, content)
|
|
|
|
print_entry(name, results[name])
|
|
|
|
for selenium in selenium_backends.values():
|
|
|
|
selenium.driver.quit()
|
2018-04-05 22:07:33 +00:00
|
|
|
return results
|
|
|
|
|
|
|
|
|
2020-06-28 18:24:40 +00:00
|
|
|
if __name__ == "__main__":
|
2018-08-03 17:10:18 +00:00
|
|
|
results = main(Path(sys.argv[-2]).resolve())
|
2020-06-28 18:24:40 +00:00
|
|
|
with open(sys.argv[-1], "w") as fp:
|
2018-04-05 22:07:33 +00:00
|
|
|
json.dump(results, fp)
|