|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import subprocess |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +from dataclasses import dataclass |
| 6 | + |
| 7 | +SOURCE_ROOT = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..") |
| 8 | + |
| 9 | +def available_engines(): |
| 10 | + engines = { |
| 11 | + "WasmKit": lambda runner, path: runner.run_command([ |
| 12 | + os.path.join(SOURCE_ROOT, ".build/release/wasmkit-cli"), "run", path, |
| 13 | + ]), |
| 14 | + } |
| 15 | + if shutil.which("wasmi_cli"): |
| 16 | + engines["wasmi"] = lambda runner, path: runner.run_command([ |
| 17 | + "wasmi_cli", path, |
| 18 | + ]) |
| 19 | + if shutil.which("wasmtime"): |
| 20 | + engines["wasmtime"] = lambda runner, path: runner.run_command([ |
| 21 | + "wasmtime", path, |
| 22 | + ]) |
| 23 | + return engines |
| 24 | + |
| 25 | +@dataclass |
| 26 | +class Benchmark: |
| 27 | + name: str |
| 28 | + path: str |
| 29 | + eta_sec: float |
| 30 | + |
| 31 | +def available_benchmarks(): |
| 32 | + benchmarks = [ |
| 33 | + Benchmark("CoreMark", os.path.join(SOURCE_ROOT, "Vendor", "coremark", "coremark.wasm"), 20.0), |
| 34 | + ] |
| 35 | + return {b.name: b for b in benchmarks} |
| 36 | + |
| 37 | +class Runner: |
| 38 | + |
| 39 | + def __init__(self, args, engines, benchmarks): |
| 40 | + self.verbose = args.verbose |
| 41 | + self.dry_run = args.dry_run |
| 42 | + |
| 43 | + def filter_dict(d, keys): |
| 44 | + if keys is None: |
| 45 | + return d |
| 46 | + return {k: v for k, v in d.items() if k in keys} |
| 47 | + |
| 48 | + self.engines = filter_dict(engines, args.engine) |
| 49 | + self.benchmarks = filter_dict(benchmarks, args.benchmark) |
| 50 | + |
| 51 | + def run_command(self, command): |
| 52 | + if self.verbose or self.dry_run: |
| 53 | + print(f"+ {command}") |
| 54 | + if not self.dry_run: |
| 55 | + subprocess.check_call(command) |
| 56 | + |
| 57 | + def build(self): |
| 58 | + """Build .wasm file to benchmark.""" |
| 59 | + |
| 60 | + wasi_sdk_path = os.getenv("WASI_SDK_PATH") |
| 61 | + if wasi_sdk_path is None: |
| 62 | + raise Exception("WASI_SDK_PATH environment variable not set") |
| 63 | + |
| 64 | + vendor_path = os.path.join(SOURCE_ROOT, "Vendor") |
| 65 | + |
| 66 | + self.run_command([ |
| 67 | + "make", |
| 68 | + "compile", "-C", os.path.join(vendor_path, "coremark"), |
| 69 | + "PORT_DIR=simple", f"CC={wasi_sdk_path}/bin/clang", |
| 70 | + "PORT_CFLAGS=-O3 -D_WASI_EMULATED_PROCESS_CLOCKS -lwasi-emulated-process-clocks", |
| 71 | + "EXE=.wasm" |
| 72 | + ]) |
| 73 | + |
| 74 | + self.run_command([ |
| 75 | + "swift", "build", "-c", "release", "--package-path", SOURCE_ROOT, "--product", "wasmkit-cli" |
| 76 | + ]) |
| 77 | + |
| 78 | + def run(self): |
| 79 | + for engine_name, engine in sorted(self.engines.items(), key=lambda x: x[0]): |
| 80 | + for benchmark_name, benchmark in self.benchmarks.items(): |
| 81 | + print(f"===== Running {benchmark_name} with {engine_name} (ETA: {benchmark.eta_sec} sec) =====") |
| 82 | + engine(self, benchmark.path) |
| 83 | + |
| 84 | +def main(): |
| 85 | + import argparse |
| 86 | + benchmarks = available_benchmarks() |
| 87 | + engines = available_engines() |
| 88 | + |
| 89 | + parser = argparse.ArgumentParser(description="Run benchmarks") |
| 90 | + parser.add_argument("--skip-build", action="store_true", help="Skip building the benchmark") |
| 91 | + parser.add_argument("--verbose", action="store_true", help="Print commands before running them") |
| 92 | + parser.add_argument("--dry-run", action="store_true", help="Print commands without running them") |
| 93 | + parser.add_argument("--engine", action="append", help="Engines to run", choices=engines.keys()) |
| 94 | + parser.add_argument("--benchmark", action="append", help="Benchmarks to run", choices=benchmarks.keys()) |
| 95 | + |
| 96 | + args = parser.parse_args() |
| 97 | + |
| 98 | + runner = Runner(args, engines, benchmarks) |
| 99 | + if not args.skip_build: |
| 100 | + runner.build() |
| 101 | + runner.run() |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + main() |
0 commit comments