Files
Triforce-Source/tests/test_bench_pan.py
ruckus7382sfgsgdf b527e29adb Triforce v0.8.5
A Randomizer page in the launcher, a new title screen with Mods and Save Select tabs, scaling changes applied in game, the Jabu-Jabu bars fixed, renderer frame-time work and the brightness-flash fix, and fixes from the 0.8.4 reports.
2026-11-28 10:54:41 -06:00

147 lines
7.5 KiB
Python

"""(Triforce, 11-26) The benchmark's camera turn as a measurement switch (host/hos_bench.c HOS_BENCH_PAN). No game, no GPU:
the source is read, and bench_pan is cut out of it and compiled with a C compiler.
- HOS_BENCH_PAN=<percent> is read once at the benchmark's start into B.pan. bench_pan, compiled, gives PAN (the right stick
at 35%) unset or empty, and for a value that is not a number (nan too) or outside 0-100, which it marks rejected; the
leading number counts (35% is 35), rounded up as PAN is, so 35 gives PAN;
- exactly two places push the stick, one in the settling and one in the measuring, and both push B.pan; the boot and the
travel push none;
- when the variable is set, the push in force is logged once (a rejected value too) and the results have "pan";
- docs/development.md has the row, under "Time and profiles", with HOS_BENCH_SCENES' form and the default scenes, which
are the source's.
python -I -B tests/test_bench_pan.py [--cc clang]
"""
import argparse
import os
import re
import subprocess
import tempfile
from pathlib import Path
RUN_ALL_ARGS = ["--cc", "clang"]
ROOT = Path(__file__).resolve().parents[1]
PAN = 11469
# bench_pan cut from the source, given each value of stdin's lines (unset first): the push and whether it was rejected
HARNESS = r"""
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PAN %(pan)d
%(bench_pan)s
int main(void) {
char line[256];
int rejected = -1, pan = bench_pan(NULL, &rejected);
printf("%%d %%d\n", pan, rejected);
while (fgets(line, sizeof line, stdin)) {
line[strcspn(line, "\r\n")] = 0;
rejected = -1, pan = bench_pan(line, &rejected);
printf("%%d %%d\n", pan, rejected);
}
return 0;
}
"""
# (HOS_BENCH_PAN's value, the push, rejected)
CASES = [
("", PAN, 0),
(" ", PAN, 1), ("x", PAN, 1), ("off", PAN, 1), ('"0"', PAN, 1),
("-1", PAN, 1), ("101", PAN, 1), ("100.5", PAN, 1), ("inf", PAN, 1), ("-inf", PAN, 1),
("nan", PAN, 1), ("-nan", PAN, 1), ("NAN", PAN, 1), ("nan(1)", PAN, 1),
("0", 0, 0), ("-0", 0, 0), ("100", 32767, 0), ("35", PAN, 0), ("35%", PAN, 0), (" 35", PAN, 0),
("50", 16384, 0), ("10", 3277, 0), ("0.5", 164, 0),
]
def check(ok, what):
if not ok:
raise SystemExit("FAIL: " + what)
def function(text, head):
start = text.find(head)
check(start >= 0, "%s is missing" % head.rstrip(" {("))
return text[start:text.index("\n}\n", start) + 3]
def compiled(cc, parse):
"""[(push, rejected)] for unset, then for each of CASES"""
flags = subprocess.CREATE_NO_WINDOW | subprocess.BELOW_NORMAL_PRIORITY_CLASS if os.name == "nt" else 0
with tempfile.TemporaryDirectory(prefix="bench-pan-") as tmp:
src, exe = Path(tmp) / "bench_pan.c", Path(tmp) / "bench_pan.exe"
src.write_text(HARNESS % {"pan": PAN, "bench_pan": parse}, encoding="utf-8")
build = subprocess.run([cc, "-std=gnu11", "-O2", "-Wall", "-Wextra", "-Werror", str(src), "-o", str(exe), "-lm"],
creationflags=flags, capture_output=True, text=True, timeout=120)
check(build.returncode == 0, "bench_pan does not compile:\n" + build.stdout + build.stderr)
run = subprocess.run([str(exe)], input="".join(v + "\n" for v, _, _ in CASES), creationflags=flags,
capture_output=True, text=True, timeout=60)
check(run.returncode == 0, "the bench_pan harness failed:\n" + run.stderr)
return [tuple(int(x) for x in line.split()) for line in run.stdout.splitlines()]
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cc", default="clang")
args = parser.parse_args()
src = (ROOT / "host" / "hos_bench.c").read_text(encoding="utf-8")
pan = re.search(r"#define PAN (\d+)", src)
check(pan and int(pan.group(1)) == PAN, "PAN is the right stick at 35% (11469)")
parse = function(src, "static int bench_pan(const char *e, int *rejected) {")
got = compiled(args.cc, parse)
want = [(PAN, 0)] + [(p, r) for _, p, r in CASES]
check(len(got) == len(want), "the harness printed %d results for %d values" % (len(got), len(want)))
bad = ["%s: %s, not %s" % (v, g, w) for v, g, w in zip(["(unset)"] + [repr(c[0]) for c in CASES], got, want) if g != w]
check(not bad, "bench_pan (push, rejected):\n " + "\n ".join(bad))
init = function(src, "void hos_bench_init(void) {")
for s in ('const char *pan = getenv("HOS_BENCH_PAN");', "B.pan = bench_pan(pan, &B.pan_rejected);",
"B.pan_set = pan && *pan;"):
check(s in init, "hos_bench_init: %r" % s)
check(init.find("B.pan_set =") < init.find("B.on = 1;"), "B.pan is set before the benchmark turns on")
check('"1001:Hyrule Field,1003:Castle Town Market,1000:Kokiri Forest,1007:Lake Hylia"' in init,
"the default scenes are the four the docs row names")
check("int pan;" in src and "int pan_set, pan_rejected;" in src, "the benchmark's state has pan, pan_set, pan_rejected")
# every push: the prototype aside, two, each of B.pan; in the present, one in the settling and one in the measuring
pushes = [a for a in re.findall(r"\bhos_hid_stick\(([^;]*)\);", src) if "int lx" not in a]
check(len(pushes) == 2, "two stick pushes (settling, measuring), found %d: %s" % (len(pushes), pushes))
check(all([x.strip() for x in a.split(",")][2] == "B.pan" for a in pushes), "every stick push uses B.pan, found %s" % pushes)
present = function(src, "void hos_bench_present(int64_t now) {")
marks = ["case MKWVK_BENCH_BOOT:", "case MKWVK_BENCH_TRAVEL:", "case MKWVK_BENCH_SETTLE:", "case MKWVK_BENCH_MEASURE:",
"default:"]
at = [present.find(m) for m in marks]
check(all(i >= 0 for i in at) and at == sorted(at), "hos_bench_present has its phases in order: %s" % marks)
for (a, b), n in zip(zip(at, at[1:]), (0, 0, 1, 1)):
found = present[a:b].count("hos_hid_stick(")
check(found == n, "%s pushes the stick %d times, not %d" % (present[a:a + 40].split(":")[0], found, n))
# the push in force, once with the start's line when the variable is set; and the results' "pan"
told = present[present.find("if (!told) {"):present.find("switch (B.phase)")]
check("if (B.pan_set) {" in told, "the start's line is followed by the push when HOS_BENCH_PAN is set")
for s in ('"benchmark: HOS_BENCH_PAN=%.40s is not a percent from 0 to 100: the right stick at 35%%, as unset"',
'"benchmark: HOS_BENCH_PAN=%.40s: the right stick at %.2f%% (%d of 32767)"'):
check(s in told, "the start logs %s" % s)
results = function(src, "static void write_results(int stopped) {")
check(r'if (B.pan_set) fprintf(f, ",\"pan\":%.2f", B.pan * 100.0 / 32767.0);' in results,
'the results have "pan" when HOS_BENCH_PAN is set')
docs = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
row = docs.find("| `HOS_BENCH_PAN=<percent>` |")
check(row >= 0, "docs/development.md has the HOS_BENCH_PAN row")
check(docs.find("### Time and profiles") < row < docs.find("### The GPU"), "the row is under Time and profiles")
text = docs[row:docs.index("\n", row)]
for s in ("`HOS_BENCHMARK=<results.json>`", "`HOS_BENCH_SECONDS`", "`HOS_BENCH_SCENES=<action>:<name>,...`",
"unset: Hyrule Field, the Market, Kokiri Forest and Lake Hylia", "one view per scene",
"about 18 s at Camera speed 100", "dead zone", "not a number (`nan` too)", "`benchmark: HOS_BENCH_PAN=`", '`"pan"`'):
check(s in text, "the HOS_BENCH_PAN row says %s" % s)
print("ok: HOS_BENCH_PAN")
if __name__ == "__main__":
main()