diff --git a/README.md b/README.md index 4d83bed..70b5160 100644 --- a/README.md +++ b/README.md @@ -43,8 +43,12 @@ Alias Calc must beat omacalc and GNOME Calculator on startup, memory and idle wa ```sh OMACALC=/path/to/omacalc bench/run # 10 runs each bench/run --runs 3 # quicker +bench/run --bin /usr/bin/alias-calc # the installed package, not this checkout +bench/run --cold # drop the page cache before each launch (sudo) ``` +`--cold` writes 3 to `/proc/sys/vm/drop_caches`, which empties the page, dentry and inode caches. A library another running app has mapped stays in memory, so a cold start is as cold as the machine allows, not a first boot. + For each app it times exec until Hyprland reports the window open, reads PSS, RSS and threads 2 s later, and counts wakeups over the next 5 idle seconds. It prints the medians as one table. The windows open on the current workspace, so leave the machine alone while it runs. omacalc builds from with `bin/build`. Baselines (2026-11-25, RTX 4090, 4K at scale 1, median of 10): diff --git a/bench/calcbench.py b/bench/calcbench.py index 5895e26..f377bf3 100755 --- a/bench/calcbench.py +++ b/bench/calcbench.py @@ -5,9 +5,10 @@ window opened (`openwindow` on its event socket, sent once the app's first buffer maps the toplevel). After the window settles, it reads PSS, RSS and the thread count for the process tree, then counts context switches (wakeups) over -an idle period. It prints the medians as one JSON line. +an idle period. It prints the medians as one JSON line. With --cold, it +drops the page cache before each run (sudo -n: ask for sudo first). -Usage: calcbench.py [--runs N] -- cmd args... +Usage: calcbench.py [--runs N] [--cold] -- cmd args... calcbench.py --table < results.jsonl The windows open on the current workspace, so leave the machine alone while it @@ -26,6 +27,7 @@ SETTLE = 2.0 # seconds after map before reading memory IDLE = 5.0 # seconds of idle to count wakeups over MAP_TIMEOUT = 10.0 +COLD_MAP_TIMEOUT = 30.0 # a start that reads everything from disk def event_socket(): @@ -162,6 +164,15 @@ def run_once(cmd, wclass, path): wakeups_s=idle_wakeups) +def drop_caches(): + """Empty the page cache, so the next start reads from disk. bench/run + asked for sudo once, so this doesn't prompt.""" + subprocess.run(["sync"], check=True) + subprocess.run(["sudo", "-n", "tee", "/proc/sys/vm/drop_caches"], input=b"3\n", + stdout=subprocess.DEVNULL, check=True) + time.sleep(SETTLE) + + def stop(proc): """SIGTERM the app's process group, then SIGKILL it if it lingers (a lingering GNOME Calculator would take over the next run's launch).""" @@ -193,7 +204,8 @@ def table(): cells = [row[k] / 1024 if k.endswith("_kb") else row[k] for k, _ in cols] print(f"{row['name']:18}" + "".join(f"{v:>11.1f}" for v in cells)) if rows: - print(f"median of {rows[0]['runs']} runs each") + cold = ", from a cold page cache" if any(r.get("cold") for r in rows) else "" + print(f"median of {rows[0]['runs']} runs each{cold}") def main(): @@ -205,22 +217,34 @@ def main(): split = argv.index("--") parser = argparse.ArgumentParser() parser.add_argument("--runs", type=int, default=10, choices=range(1, 1001), metavar="N") + parser.add_argument("--cold", action="store_true") parser.add_argument("name") parser.add_argument("wclass") args = parser.parse_args(argv[:split]) cmd = argv[split + 1:] path = event_socket() + global MAP_TIMEOUT + if args.cold: + MAP_TIMEOUT = COLD_MAP_TIMEOUT try: - results = [run_once(cmd, args.wclass.lower(), path) for _ in range(args.runs)] + results = [] + for _ in range(args.runs): + if args.cold: + drop_caches() + results.append(run_once(cmd, args.wclass.lower(), path)) except (TimeoutError, socket.timeout): sys.exit(f"calcbench: {args.name}: no window within {MAP_TIMEOUT:.0f} s") + except subprocess.CalledProcessError as err: + why = "; --cold needs sudo, asked for first by bench/run" if err.cmd[0] == "sudo" else "" + sys.exit(f"calcbench: {args.name}: couldn't drop the caches ({' '.join(err.cmd)}" + f" exited {err.returncode}){why}") except (OSError, RuntimeError) as err: sys.exit(f"calcbench: {args.name}: {err}") median = {k: round(statistics.median(r[k] for r in results), 1) for k in results[0]} median["map_ms_min"] = round(min(r["map_ms"] for r in results), 1) - print(json.dumps({"name": args.name, "runs": args.runs, **median})) + print(json.dumps({"name": args.name, "runs": args.runs, "cold": args.cold, **median})) if __name__ == "__main__": diff --git a/bench/run b/bench/run index ab1ab0f..40218a7 100755 --- a/bench/run +++ b/bench/run @@ -2,7 +2,12 @@ # Benchmark Alias Calc against omacalc and GNOME Calculator and print one # table. Needs a Hyprland session; the windows open on the current workspace. # -# bench/run [--runs N] +# bench/run [--runs N] [--bin PATH] [--cold] +# +# --bin times that Alias Calc (the installed /usr/bin/alias-calc, say) +# instead of building this checkout. --cold drops the page cache before +# every launch (sudo, asked for once up front), so each start reads its +# binary and libraries from disk. # # omacalc comes from $OMACALC, else `omacalc` on PATH, else it's skipped. # Build it from https://github.com/omacom/omacalc with `bin/build`. @@ -10,19 +15,38 @@ # running copy instead of starting). set -euo pipefail -cd "$(dirname "$0")/.." +root=$(cd "$(dirname "$0")/.." && pwd) runs=10 +bin= +cold=() while (($#)); do case $1 in --runs) runs=${2:?--runs needs a number}; shift 2 ;; --runs=*) runs=${1#--runs=}; shift ;; - *) echo "Usage: bench/run [--runs N]" >&2; exit 2 ;; + --bin) bin=${2:?--bin needs a path}; shift 2 ;; + --bin=*) bin=${1#--bin=}; shift ;; + --cold) cold=(--cold); shift ;; + *) echo "Usage: bench/run [--runs N] [--bin PATH] [--cold]" >&2; exit 2 ;; esac done +# --bin is relative to where it was typed, or a name on PATH. +if [[ -n $bin ]]; then + [[ $bin == */* ]] || bin=$(command -v "$bin" || echo "$bin") + [[ -f $bin && -x $bin ]] || { echo "Not an executable file: $bin" >&2; exit 2; } + bin=$(realpath "$bin") +fi +cd "$root" -cargo build --release --locked --quiet -target=$(cargo metadata --format-version 1 --no-deps | python3 -I -c 'import json, sys; print(json.load(sys.stdin)["target_directory"])') +if [[ -z $bin ]]; then + cargo build --release --locked --quiet + target=$(cargo metadata --format-version 1 --no-deps | python3 -I -c 'import json, sys; print(json.load(sys.stdin)["target_directory"])') + bin=$target/release/alias-calc +fi +if ((${#cold[@]})); then + echo "Dropping the page cache before each launch needs sudo." >&2 + sudo -v +fi # Measure every app as the floating window it is in daily use (aliasOS # floats GNOME Calculator already). The rule lasts until Hyprland reloads @@ -35,12 +59,12 @@ bench() { local name=$1 class=$2 shift 2 local row - if row=$(python3 -I bench/calcbench.py --runs "$runs" "$name" "$class" -- "$@"); then + if row=$(python3 -I bench/calcbench.py --runs "$runs" "${cold[@]}" "$name" "$class" -- "$@"); then results+=("$row") fi } -bench alias-calc com.aliasproject.calc "$target/release/alias-calc" +bench alias-calc com.aliasproject.calc "$bin" # The table still prints the others, but a run without Alias Calc's own row # is a failure. alias_calc_measured=${#results[@]}