Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/omacom/omacalc> with `bin/build`.

Baselines (2026-11-25, RTX 4090, 4K at scale 1, median of 10):
Expand Down
34 changes: 29 additions & 5 deletions bench/calcbench.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] <name> <window-class> -- cmd args...
Usage: calcbench.py [--runs N] [--cold] <name> <window-class> -- cmd args...
calcbench.py --table < results.jsonl

The windows open on the current workspace, so leave the machine alone while it
Expand All @@ -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():
Expand Down Expand Up @@ -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)."""
Expand Down Expand Up @@ -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():
Expand All @@ -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__":
Expand Down
38 changes: 31 additions & 7 deletions bench/run
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,51 @@
# 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`.
# GNOME Calculator must not already be running (it would hand off to the
# 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
Expand All @@ -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[@]}
Expand Down
Loading