-
Notifications
You must be signed in to change notification settings - Fork 11
/
bench.py
60 lines (51 loc) · 1.59 KB
/
bench.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import json
import glob
import subprocess
from os.path import splitext, basename
import matplotlib.pyplot as plt
def run_benchmark(name):
print(f"Running benchmark for {name}")
proc = subprocess.run(
f"cargo criterion --bench {name} --message-format json --features {name}",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
text=True,
check=True,
)
res = []
for line in proc.stdout.splitlines():
x = json.loads(line)
if "id" in x:
res.append(x)
return res
# Compile webassembly modules
subprocess.run(
"rustc --target wasm32-unknown-unknown -Cpanic=abort -O --crate-name sort_userdata scripts/sort_userdata.wasm.rs -o scripts/sort_userdata.wasm",
shell=True,
check=True,
)
benches = dict()
ids = set()
for f in glob.glob("benches/*.rs"):
name = splitext(basename(f))[0]
benches[name] = dict()
results = run_benchmark(name)
for res in results:
benches[name][res["id"]] = res
ids.add(res["id"])
for id in ids:
fig, ax = plt.subplots()
ymax = 0
for (name, bench) in sorted(benches.items(), key=lambda x: x[1][id]["typical"]["estimate"]):
val = round(bench[id]["typical"]["estimate"] / 1000000, 2)
rect = ax.bar(name, val, width=0.3)
ax.bar_label(rect, padding=3)
ymax = max(ymax, val)
ax.set_title("lower is better")
ax.set_ylabel("time (ms)", fontweight="bold")
ax.set_ylim(0, ymax * 1.2)
fig.autofmt_xdate()
fig.suptitle(id, fontsize=18)
fig.tight_layout()
plt.savefig(f"{id}.png", dpi=300)