Skip to content

Commit 96bb064

Browse files
Add CoreMark benchmark setup
1 parent f545707 commit 96bb064

File tree

4 files changed

+135
-2
lines changed

4 files changed

+135
-2
lines changed

Benchmarks/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Benchmarks
2+
3+
This directory contains a set of benchmarks that can be used to compare the performance of the WasmKit runtime with other WebAssembly runtimes.
4+
5+
## Prerequisites
6+
7+
To build benchmarks, you need to install [wasi-sdk](https://github.com/WebAssembly/wasi-sdk) and set the `WASI_SDK_PATH` environment variable to the installation directory.
8+
9+
## Running the Benchmarks
10+
11+
Run all benchmarks:
12+
13+
```console
14+
$ ./bench.py
15+
```
16+
17+
Filtering examples:
18+
19+
```console
20+
$ ./bench.py --benchmark CoreMark
21+
$ ./bench.py --engine WasmKit
22+
```
23+
24+
See `./bench.py --help` for more options.

Benchmarks/bench.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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()

Vendor/.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
/wasi-testsuite
2-
/testsuite
1+
/*
2+
!checkout-dependency
3+
!dependencies.json

Vendor/dependencies.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@
66
"wasi-testsuite": {
77
"repository": "https://github.com/WebAssembly/wasi-testsuite.git",
88
"revision": "c9c751586fd86b321d595bbef13f2c7403cfdbc5"
9+
},
10+
"coremark": {
11+
"repository": "https://github.com/eembc/coremark.git",
12+
"revision": "d5fad6bd094899101a4e5fd53af7298160ced6ab"
913
}
1014
}

0 commit comments

Comments
 (0)