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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
#!/usr/bin/env python3
"""the benchmark suite behind `make bench`, `make bench-save` and `make bench-check`.
usage: tests/bench/suite.py run [--zy PATH] [--out FILE]
tests/bench/suite.py check [--zy PATH] [--baseline FILE] [--out FILE]
each program is a kernel of tests/programs/bench.zc (the kernels bench.sh times, plain
and o3) or of tests/bench/sweep.zc (a 256-word pointer sweep, checked and unbounded),
run on one core (build/bin/zy by default, the host's jit where there is one). a kernel
is selected by a letter and a repetition count on stdin, after the source. the time per
repetition is the slope between 1 and N repetitions, so starting the vm and compiling
the program cancel out; N grows until the N run takes at least MIN_S seconds. each
cell is the median of TRIALS slopes.
columns: ms per repetition; checks, the number of bounds checks the compiler keeps
(from --explain-checks, which does not exist yet: '-'); peak heap words and gc mark
time, which the runtime does not expose: '-'.
check compares with the baseline (tests/bench.baseline): it fails when a program is
more than TOLERANCE (10%) slower, or when both sides have a checks count and it rose.
a program that looks slower is measured again, up to RETRIES more times, and its best
median is kept, so one noisy trial does not fail the check.
"""
import argparse
import os
import statistics
import subprocess
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent.parent
TRIALS = int(os.environ.get("TRIALS", "5"))
MIN_S = float(os.environ.get("MIN_S", "0.4"))
TOLERANCE = float(os.environ.get("TOLERANCE", "0.10"))
RETRIES = int(os.environ.get("RETRIES", "2"))
# name, source, kernel letter
PROGRAMS = [
("collatz", "tests/programs/bench.zc", "a"),
("collatz.o3", "tests/programs/bench.zc", "b"),
("sieve", "tests/programs/bench.zc", "c"),
("sieve.o3", "tests/programs/bench.zc", "d"),
("digits", "tests/programs/bench.zc", "e"),
("digits.o3", "tests/programs/bench.zc", "f"),
("fib", "tests/programs/bench.zc", "g"),
("fib.o3", "tests/programs/bench.zc", "h"),
("sweep.checked", "tests/bench/sweep.zc", "c"),
("sweep.unbounded", "tests/bench/sweep.zc", "u"),
]
COLUMNS = ["program", "ms", "checks", "peak_heap_words", "gc_mark_ms"]
def run_once(zy, src, letter, reps):
data = src + b"`" + letter.encode() + str(reps).encode()
t = time.perf_counter()
r = subprocess.run([zy], input=data, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
dt = time.perf_counter() - t
if r.returncode != 0:
sys.exit("bench: %s %s failed: %s" % (zy, letter, r.stderr.decode(errors="replace")[:300]))
return dt
def slope_ms(zy, path, letter):
src = (ROOT / path).read_bytes()
n = 4
while True:
one = min(run_once(zy, src, letter, 1) for _ in range(2))
many = run_once(zy, src, letter, n)
if many - one >= MIN_S or n >= 1 << 16:
break
n *= 4
slopes = []
for _ in range(TRIALS):
one = run_once(zy, src, letter, 1)
many = run_once(zy, src, letter, n)
slopes.append((many - one) / (n - 1) * 1000)
return statistics.median(slopes)
def measure(zy, name):
for p, path, letter in PROGRAMS:
if p == name:
return slope_ms(zy, path, letter)
raise KeyError(name)
def fmt_row(cells):
return "%-16s %10s %8s %16s %11s" % tuple(cells)
def write_table(path, rows):
lines = ["# tinyvm bench: ms per repetition (median of %d slopes); '-' where a figure is not available" % TRIALS,
fmt_row(COLUMNS)]
for r in rows:
lines.append(fmt_row([r["program"], "%.3f" % r["ms"], r["checks"], r["peak"], r["gc"]]))
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text("\n".join(lines) + "\n")
def read_table(path):
rows = {}
for line in path.read_text().splitlines():
if not line.strip() or line.startswith("#") or line.split()[0] == "program":
continue
w = line.split()
rows[w[0]] = {"program": w[0], "ms": float(w[1]), "checks": w[2], "peak": w[3], "gc": w[4]}
return rows
def run_all(zy):
rows = []
for name, _, _ in PROGRAMS:
ms = measure(zy, name)
print("bench: %-16s %10.3f ms" % (name, ms), file=sys.stderr, flush=True)
rows.append({"program": name, "ms": ms, "checks": "-", "peak": "-", "gc": "-"})
return rows
def main():
ap = argparse.ArgumentParser(description="tinyvm benchmark suite")
ap.add_argument("mode", choices=["run", "check"])
ap.add_argument("--zy", default=str(ROOT / "build/bin/zy"))
ap.add_argument("--out", default=str(ROOT / "build/bench.txt"))
ap.add_argument("--baseline", default=str(ROOT / "tests/bench.baseline"))
a = ap.parse_args()
rows = run_all(a.zy)
if a.mode == "run":
write_table(Path(a.out), rows)
print(Path(a.out).read_text(), end="")
return 0
base_path = Path(a.baseline)
if not base_path.exists():
sys.exit("bench: no baseline at %s (run make bench-save)" % base_path)
base = read_table(base_path)
bad = 0
print("%-16s %10s %10s %8s %9s %9s %s" % ("program", "base ms", "now ms", "change", "base chk", "now chk", "verdict"))
for r in rows:
b = base.get(r["program"])
if not b:
print("%-16s %10s %10.3f %8s %9s %9s new" % (r["program"], "-", r["ms"], "-", "-", r["checks"]))
continue
tries = 0
while r["ms"] > b["ms"] * (1 + TOLERANCE) and tries < RETRIES:
r["ms"] = min(r["ms"], measure(a.zy, r["program"]))
tries += 1
change = (r["ms"] - b["ms"]) / b["ms"] * 100 if b["ms"] > 0 else 0.0
verdict = "ok"
if r["ms"] > b["ms"] * (1 + TOLERANCE):
verdict = "SLOWER"
if b["checks"] != "-" and r["checks"] != "-" and int(r["checks"]) > int(b["checks"]):
verdict = "MORE CHECKS" if verdict == "ok" else verdict + ", MORE CHECKS"
if verdict != "ok":
bad += 1
print("%-16s %10.3f %10.3f %+7.1f%% %9s %9s %s" % (r["program"], b["ms"], r["ms"], change, b["checks"], r["checks"], verdict))
for name in base:
if name not in {r["program"] for r in rows}:
print("%-16s %10.3f %10s %8s %9s %9s gone" % (name, base[name]["ms"], "-", "-", base[name]["checks"], "-"))
write_table(Path(a.out), rows)
print("bench-check: %s (%d of %d programs over the limit; tolerance %d%%)" % ("FAIL" if bad else "ok", bad, len(rows), round(TOLERANCE * 100)))
return 1 if bad else 0
if __name__ == "__main__":
sys.exit(main())
|