CI: speed check on every pull request (#155)
This commit is contained in:
1 parent
d7d282e499
commit
dfe3ffa5f5
4 files changed
+375
No files matched your search
@@ -0,0 +1,64 @@
|
||||
# Kiri's speed check on every pull request: main and the change, built the
|
||||
# same way and run side by side on one machine (scripts/perf-run.sh), with
|
||||
# requests alternating between them. The table lands in the run's summary;
|
||||
# the check fails when the change is slower than main past the limits in
|
||||
# scripts/perf.py: docker run, API round trips, the daemon's memory and the
|
||||
# system calls one container costs.
|
||||
#
|
||||
# The limits come from main's copy of the script when it has one, so a pull
|
||||
# request can't loosen them for itself.
|
||||
#
|
||||
# Safe for pull requests from forks: it runs on GitHub's machines with a
|
||||
# read-only token and no secrets, and uses pull_request (never
|
||||
# pull_request_target), so a fork's code can't write to the repository.
|
||||
name: Speed
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
concurrency:
|
||||
group: speed-${{ github.event.pull_request.number }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
speed:
|
||||
runs-on: ubuntu-24.04
|
||||
timeout-minutes: 60
|
||||
steps:
|
||||
- name: Check out this change
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
path: head
|
||||
persist-credentials: false
|
||||
- name: Check out main
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
ref: ${{ github.event.pull_request.base.sha }}
|
||||
path: base
|
||||
persist-credentials: false
|
||||
- name: Build main and this change
|
||||
run: |
|
||||
# Static Linux builds, as `cargo xtask linux` makes them. Both use
|
||||
# one target folder, so the second only rebuilds what changed.
|
||||
for e in base head; do
|
||||
mkdir -p "$RUNNER_TEMP/$e-bin"
|
||||
docker run --rm -v "$PWD/$e:/src" -v "$RUNNER_TEMP/$e-bin:/out" \
|
||||
-v kiri-perf-target:/t -v kiri-perf-cargo:/usr/local/cargo/registry \
|
||||
-w /src -e CARGO_TARGET_DIR=/t rust:1-alpine sh -c \
|
||||
'apk add -q musl-dev && cargo build --release --locked -p kiri -p kiri-init && cp /t/release/kiri /t/release/kiri-init /out/'
|
||||
done
|
||||
ls -l "$RUNNER_TEMP"/base-bin "$RUNNER_TEMP"/head-bin
|
||||
- name: Install strace
|
||||
run: sudo apt-get install -y -qq strace >/dev/null
|
||||
- name: Compare
|
||||
run: |
|
||||
scripts=base/scripts
|
||||
[ -f "$scripts/perf.py" ] || scripts=head/scripts
|
||||
set +e
|
||||
sh "$scripts/perf-run.sh" "$RUNNER_TEMP/base-bin" "$RUNNER_TEMP/head-bin" "$RUNNER_TEMP/perf"
|
||||
status=$?
|
||||
[ -f "$RUNNER_TEMP/perf/report.md" ] && cat "$RUNNER_TEMP/perf/report.md" >> "$GITHUB_STEP_SUMMARY"
|
||||
exit $status
|
||||
@@ -36,6 +36,17 @@ once; see the README.
|
||||
maintainer's OK. It also runs on every pull request; on Linux, point
|
||||
`CHROME` at your Chrome or Chromium.
|
||||
|
||||
## Speed is a feature
|
||||
|
||||
Kiri is small and fast on purpose, and every pull request is checked for it.
|
||||
The Speed check builds main and your change, runs both engines side by side
|
||||
on one machine and compares them: `docker run`, API round trips, the daemon's
|
||||
memory, the system calls one container costs, and the binaries' size. It
|
||||
fails when the change is slower than main past the limits in
|
||||
`scripts/perf.py`; the table is in the check's summary. To run it yourself on
|
||||
Linux, build both with `cargo xtask linux` and run
|
||||
`scripts/perf-run.sh <main's build> <your build> <work folder>`.
|
||||
|
||||
## Pull requests
|
||||
|
||||
- Keep each pull request to one change, with a message that says what it does
|
||||
|
||||
Executable
+45
@@ -0,0 +1,45 @@
|
||||
#!/bin/sh
|
||||
# Runs Kiri's speed check (scripts/perf.py) for two Linux builds: main's and
|
||||
# a change's. Each engine runs in its own privileged container on this
|
||||
# machine's Docker, with its socket in WORK, and the check talks to both.
|
||||
#
|
||||
# scripts/perf-run.sh BASE_BIN HEAD_BIN WORK [RUNS]
|
||||
#
|
||||
# BASE_BIN and HEAD_BIN hold `kiri` and `kiri-init` (cargo xtask linux).
|
||||
# Needs docker, python3 and strace, and root for strace and the sockets
|
||||
# (it uses sudo when not root). Writes WORK/report.md; exits 1 when the
|
||||
# change is past a limit.
|
||||
set -eu
|
||||
BASE_BIN=$(cd "$1" && pwd); HEAD_BIN=$(cd "$2" && pwd); WORK=$3; RUNS=${4:-400}
|
||||
HERE=$(cd "$(dirname "$0")" && pwd)
|
||||
SUDO=; [ "$(id -u)" = 0 ] || SUDO=sudo
|
||||
mkdir -p "$WORK"; WORK=$(cd "$WORK" && pwd)
|
||||
|
||||
stop() {
|
||||
docker rm -f kiri-perf-base kiri-perf-head >/dev/null 2>&1 || true
|
||||
docker volume rm kiri-perf-base-lib kiri-perf-base-store kiri-perf-head-lib kiri-perf-head-store >/dev/null 2>&1 || true
|
||||
}
|
||||
trap stop EXIT
|
||||
stop
|
||||
for e in base head; do
|
||||
bin=$BASE_BIN; [ $e = head ] && bin=$HEAD_BIN
|
||||
$SUDO rm -rf "$WORK/$e-run"; mkdir -p "$WORK/$e-run"
|
||||
docker run -d --name kiri-perf-$e --privileged --tmpfs /run \
|
||||
-v kiri-perf-$e-lib:/var/lib/kiri -v kiri-perf-$e-store:/root/.local/share/kiri \
|
||||
-v "$bin:/opt/kiri:ro" -v "$WORK/$e-run:/root/.kiri/run" \
|
||||
alpine:3.21 sh -c 'exec /opt/kiri/kiri daemon' >/dev/null
|
||||
done
|
||||
pid() { docker inspect -f '{{.State.Pid}}' kiri-perf-$1; }
|
||||
for e in base head; do
|
||||
i=0; while [ ! -S "$WORK/$e-run/docker.sock" ]; do
|
||||
i=$((i + 1)); [ $i -gt 120 ] && { docker logs kiri-perf-$e 2>&1 | tail -20; echo "the $e engine made no socket"; exit 2; }
|
||||
sleep 0.5
|
||||
done
|
||||
done
|
||||
python3 "$HERE/perf.py" sizes --base "$BASE_BIN" --head "$HEAD_BIN" --out "$WORK/sizes.json"
|
||||
$SUDO python3 "$HERE/perf.py" bench --runs "$RUNS" --out "$WORK/results.json" \
|
||||
--base "$WORK/base-run/docker.sock:$(pid base)" --head "$WORK/head-run/docker.sock:$(pid head)"
|
||||
status=0
|
||||
python3 "$HERE/perf.py" report "$WORK/results.json" "$WORK/sizes.json" > "$WORK/report.md" || status=$?
|
||||
cat "$WORK/report.md"
|
||||
exit $status
|
||||
Executable
+255
@@ -0,0 +1,255 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Kiri's speed check: main against a pull request, on the same machine.
|
||||
|
||||
Two Linux engines run side by side, one built from main and one from the
|
||||
pull request, each in its own privileged container (see .github/workflows/
|
||||
perf.yml). Requests alternate between them, main then the change, so
|
||||
whatever else the machine is doing slows both alike, and the medians are
|
||||
compared. Some numbers don't wobble at all and are compared exactly: the
|
||||
binaries' size, the daemon's memory and the system calls one container
|
||||
costs.
|
||||
|
||||
perf.py bench --base SOCK:PID --head SOCK:PID [--runs N] --out results.json
|
||||
perf.py sizes --base DIR --head DIR --out sizes.json
|
||||
perf.py report results.json sizes.json (markdown to stdout; exit 1 past a limit)
|
||||
|
||||
Only the standard library, so it runs on any machine with Python 3.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import http.client
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import signal
|
||||
import socket
|
||||
import statistics
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
IMAGE, TAG = "alpine", "3.21"
|
||||
|
||||
# What fails a pull request: worse than main by more than both the share
|
||||
# and the amount. The amount keeps tiny numbers' noise from failing a
|
||||
# change; the share keeps big numbers honest.
|
||||
LIMITS = {
|
||||
"run": (0.10, 0.5), # docker run --rm alpine true: +10% and +0.5 ms
|
||||
"ping": (0.15, 0.05), # GET /_ping: +15% and +0.05 ms
|
||||
"list": (0.15, 0.10), # GET /containers/json: +15% and +0.1 ms
|
||||
"rss_idle": (0.10, 2048), # daemon memory at rest: +10% and +2 MiB (KiB)
|
||||
"rss_after": (0.10, 4096), # daemon memory after the runs: +10% and +4 MiB (KiB)
|
||||
"syscalls": (0.10, 50), # system calls per container: +10% and +50
|
||||
}
|
||||
|
||||
|
||||
class UnixHTTP(http.client.HTTPConnection):
|
||||
"""HTTP over the engine's unix socket, one connection kept open, as the
|
||||
docker CLI does."""
|
||||
|
||||
def __init__(self, path):
|
||||
super().__init__("localhost", timeout=60)
|
||||
self.path = path
|
||||
|
||||
def connect(self):
|
||||
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
self.sock.settimeout(60)
|
||||
self.sock.connect(self.path)
|
||||
|
||||
|
||||
class Engine:
|
||||
def __init__(self, spec):
|
||||
sock, pid = spec.rsplit(":", 1)
|
||||
self.sock, self.pid = sock, int(pid)
|
||||
self.conn = UnixHTTP(sock)
|
||||
|
||||
def call(self, method, path, body=None, want=(200, 201, 204, 304)):
|
||||
data = json.dumps(body).encode() if body is not None else None
|
||||
headers = {"Content-Type": "application/json"} if data is not None else {}
|
||||
for attempt in range(2):
|
||||
try:
|
||||
self.conn.request(method, path, body=data, headers=headers)
|
||||
r = self.conn.getresponse()
|
||||
out = r.read()
|
||||
break
|
||||
except (http.client.HTTPException, OSError):
|
||||
# The engine closed a kept connection: open a new one, once.
|
||||
self.conn.close()
|
||||
self.conn = UnixHTTP(self.sock)
|
||||
if attempt:
|
||||
raise
|
||||
if r.status not in want:
|
||||
raise RuntimeError(f"{method} {path}: {r.status} {out[:200]!r}")
|
||||
return out
|
||||
|
||||
def run_once(self):
|
||||
"""docker run --rm alpine true, as its API calls: create, start,
|
||||
wait for the exit, remove."""
|
||||
cid = json.loads(self.call("POST", "/containers/create", {"Image": f"{IMAGE}:{TAG}", "Cmd": ["true"]}))["Id"]
|
||||
self.call("POST", f"/containers/{cid}/start")
|
||||
self.call("POST", f"/containers/{cid}/wait")
|
||||
self.call("DELETE", f"/containers/{cid}?force=1")
|
||||
|
||||
def rss_kib(self):
|
||||
with open(f"/proc/{self.pid}/status") as f:
|
||||
m = re.search(r"VmRSS:\s+(\d+)", f.read())
|
||||
return int(m.group(1)) if m else 0
|
||||
|
||||
|
||||
def wait_ready(e, secs=60):
|
||||
end = time.time() + secs
|
||||
while time.time() < end:
|
||||
try:
|
||||
e.call("GET", "/_ping")
|
||||
return
|
||||
except Exception:
|
||||
e.conn.close()
|
||||
e.conn = UnixHTTP(e.sock)
|
||||
time.sleep(0.5)
|
||||
raise SystemExit(f"{e.sock}: the engine did not answer")
|
||||
|
||||
|
||||
def timed(f):
|
||||
t = time.perf_counter()
|
||||
f()
|
||||
return (time.perf_counter() - t) * 1000
|
||||
|
||||
|
||||
def summary(xs):
|
||||
xs = sorted(xs)
|
||||
return {"median": statistics.median(xs), "p95": xs[int(len(xs) * 0.95) - 1], "n": len(xs)}
|
||||
|
||||
|
||||
def syscalls_per_run(e, runs):
|
||||
"""System calls the daemon (every thread, and what it starts) makes for
|
||||
one container, from strace's count over `runs` of them."""
|
||||
out = f"/tmp/kiri-strace-{e.pid}.txt"
|
||||
st = subprocess.Popen(["strace", "-f", "-c", "-o", out, "-p", str(e.pid)], stderr=subprocess.DEVNULL)
|
||||
time.sleep(1.0)
|
||||
for _ in range(runs):
|
||||
e.run_once()
|
||||
time.sleep(0.5)
|
||||
st.send_signal(signal.SIGINT)
|
||||
st.wait(timeout=30)
|
||||
with open(out) as f:
|
||||
text = f.read()
|
||||
m = re.search(r"^\s*100\.00\s+\S+\s+\S+\s+(\d+)\s+(?:\d+\s+)?total\s*$", text, re.M)
|
||||
if not m:
|
||||
raise SystemExit(f"could not read strace's total:\n{text[-400:]}")
|
||||
return int(m.group(1)) / runs
|
||||
|
||||
|
||||
def bench(a):
|
||||
base, head = Engine(a.base), Engine(a.head)
|
||||
for e in (base, head):
|
||||
wait_ready(e)
|
||||
e.call("POST", f"/images/create?fromImage={IMAGE}&tag={TAG}")
|
||||
for _ in range(10): # warm: first runs fill caches
|
||||
e.run_once()
|
||||
time.sleep(3)
|
||||
res = {"base": {}, "head": {}}
|
||||
for name, e in (("base", base), ("head", head)):
|
||||
res[name]["rss_idle"] = e.rss_kib()
|
||||
samples = {k: {"base": [], "head": []} for k in ("run", "ping", "list")}
|
||||
for i in range(a.runs):
|
||||
# Alternate which goes first, so neither always gets the warmer turn.
|
||||
order = ((("base", base), ("head", head)) if i % 2 == 0 else (("head", head), ("base", base)))
|
||||
for name, e in order:
|
||||
samples["ping"][name].append(timed(lambda: e.call("GET", "/_ping")))
|
||||
samples["list"][name].append(timed(lambda: e.call("GET", "/containers/json?all=1")))
|
||||
samples["run"][name].append(timed(e.run_once))
|
||||
for k, v in samples.items():
|
||||
for name in ("base", "head"):
|
||||
res[name][k] = summary(v[name])
|
||||
for name, e in (("base", base), ("head", head)):
|
||||
res[name]["rss_after"] = e.rss_kib()
|
||||
if a.strace:
|
||||
for name, e in (("base", base), ("head", head)):
|
||||
res[name]["syscalls"] = syscalls_per_run(e, 20)
|
||||
with open(a.out, "w") as f:
|
||||
json.dump(res, f, indent=2)
|
||||
|
||||
|
||||
def sizes(a):
|
||||
def one(d):
|
||||
return {b: os.path.getsize(os.path.join(d, b)) for b in ("kiri", "kiri-init")}
|
||||
with open(a.out, "w") as f:
|
||||
json.dump({"base": one(a.base), "head": one(a.head)}, f, indent=2)
|
||||
|
||||
|
||||
def report(a):
|
||||
with open(a.results) as f:
|
||||
res = json.load(f)
|
||||
sz = {}
|
||||
if a.sizes and os.path.exists(a.sizes):
|
||||
with open(a.sizes) as f:
|
||||
sz = json.load(f)
|
||||
rows, failed = [], []
|
||||
|
||||
def row(label, key, base, head, unit, fmt):
|
||||
d = head - base
|
||||
pct = d / base * 100 if base else 0.0
|
||||
mark = ""
|
||||
if key in LIMITS:
|
||||
share, amount = LIMITS[key]
|
||||
if d > amount and (base == 0 or d / base > share):
|
||||
mark = " ❌"
|
||||
failed.append(label)
|
||||
elif d < -amount and base and -d / base > share:
|
||||
mark = " ✅"
|
||||
rows.append(f"| {label} | {fmt(base)} {unit} | {fmt(head)} {unit} | {'+' if d >= 0 else ''}{fmt(d)} {unit} ({pct:+.1f}%){mark} |")
|
||||
|
||||
ms = lambda v: f"{v:.3f}"
|
||||
for key, label in (("run", "docker run --rm alpine true"), ("ping", "GET /_ping"), ("list", "GET /containers/json")):
|
||||
b, h = res["base"][key], res["head"][key]
|
||||
row(f"{label}, median", key, b["median"], h["median"], "ms", ms)
|
||||
row(f"{label}, p95", key + "_p95", b["p95"], h["p95"], "ms", ms)
|
||||
mib = lambda v: f"{v / 1024:.1f}"
|
||||
row("Daemon memory at rest", "rss_idle", res["base"]["rss_idle"], res["head"]["rss_idle"], "MiB", mib)
|
||||
row("Daemon memory after the runs", "rss_after", res["base"]["rss_after"], res["head"]["rss_after"], "MiB", mib)
|
||||
if "syscalls" in res["base"]:
|
||||
row("System calls per container", "syscalls", res["base"]["syscalls"], res["head"]["syscalls"], "", lambda v: f"{v:.0f}")
|
||||
for b in ("kiri", "kiri-init"):
|
||||
if b in sz.get("base", {}):
|
||||
row(f"`{b}` binary", "size", sz["base"][b] / 1024, sz["head"][b] / 1024, "KiB", lambda v: f"{v:.0f}")
|
||||
n = res["base"]["run"]["n"]
|
||||
print("## Speed: this change against main\n")
|
||||
print(f"Both built and run on the same machine, requests alternating between them, {n} of each. ❌ is past the limit, ✅ is faster.\n")
|
||||
print("| | main | this change | difference |")
|
||||
print("|---|---|---|---|")
|
||||
print("\n".join(rows))
|
||||
print()
|
||||
if failed:
|
||||
print(f"**Slower than main:** {', '.join(failed)}. Limits are in `scripts/perf.py` (`LIMITS`).")
|
||||
else:
|
||||
print("**Within the limits.**")
|
||||
return 1 if failed else 0
|
||||
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
sub = p.add_subparsers(dest="cmd", required=True)
|
||||
b = sub.add_parser("bench")
|
||||
b.add_argument("--base", required=True, help="main's engine, SOCKET:PID")
|
||||
b.add_argument("--head", required=True, help="the change's engine, SOCKET:PID")
|
||||
b.add_argument("--runs", type=int, default=400)
|
||||
b.add_argument("--no-strace", dest="strace", action="store_false")
|
||||
b.add_argument("--out", required=True)
|
||||
s = sub.add_parser("sizes")
|
||||
s.add_argument("--base", required=True)
|
||||
s.add_argument("--head", required=True)
|
||||
s.add_argument("--out", required=True)
|
||||
r = sub.add_parser("report")
|
||||
r.add_argument("results")
|
||||
r.add_argument("sizes", nargs="?")
|
||||
a = p.parse_args()
|
||||
if a.cmd == "bench":
|
||||
bench(a)
|
||||
elif a.cmd == "sizes":
|
||||
sizes(a)
|
||||
else:
|
||||
sys.exit(report(a))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user