This commit is contained in:
@@ -8,7 +8,7 @@ on:
|
|||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: write
|
contents: write
|
||||||
actions: read
|
actions: write
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
release:
|
release:
|
||||||
@@ -34,6 +34,9 @@ jobs:
|
|||||||
- name: Last-attempt badge checks
|
- name: Last-attempt badge checks
|
||||||
run: python3 ci/test-last-attempt-badge.py
|
run: python3 ci/test-last-attempt-badge.py
|
||||||
|
|
||||||
|
- name: Benchmark history checks
|
||||||
|
run: python3 ci/test-bench-history.py
|
||||||
|
|
||||||
- name: Install build dependencies
|
- name: Install build dependencies
|
||||||
run: |
|
run: |
|
||||||
apt-get update -qq
|
apt-get update -qq
|
||||||
@@ -74,6 +77,11 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
cargo test -p alkane-gui --offline
|
cargo test -p alkane-gui --offline
|
||||||
|
|
||||||
|
- name: Load previous main benchmark measurement
|
||||||
|
env:
|
||||||
|
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||||
|
run: python3 ci/bench_history.py prepare
|
||||||
|
|
||||||
- name: Microbenchmarks
|
- name: Microbenchmarks
|
||||||
run: |
|
run: |
|
||||||
# Capture the bench stdout for the weighted-average delta badge (round 19
|
# Capture the bench stdout for the weighted-average delta badge (round 19
|
||||||
@@ -83,6 +91,16 @@ jobs:
|
|||||||
cargo test -p alkane-gui --release --offline -- --ignored --nocapture bench_ \
|
cargo test -p alkane-gui --release --offline -- --ignored --nocapture bench_ \
|
||||||
| tee bench-current.txt
|
| tee bench-current.txt
|
||||||
|
|
||||||
|
- name: Record private benchmark history
|
||||||
|
env:
|
||||||
|
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||||
|
run: python3 ci/bench_history.py publish
|
||||||
|
|
||||||
|
- uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: alkane-toolkit-benchmark-site-preview
|
||||||
|
path: bench-site/index.html
|
||||||
|
|
||||||
- name: Headless UI self-test
|
- name: Headless UI self-test
|
||||||
run: |
|
run: |
|
||||||
apt-get update -qq && apt-get install -y -qq --no-install-recommends \
|
apt-get update -qq && apt-get install -y -qq --no-install-recommends \
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
# SPDX-License-Identifier: MIT
|
||||||
|
"""Write small badge artifacts to the private Gitea CI badge branch."""
|
||||||
|
|
||||||
|
import base64
|
||||||
|
import json
|
||||||
|
from urllib.error import HTTPError
|
||||||
|
from urllib.parse import quote
|
||||||
|
from urllib.request import Request, urlopen
|
||||||
|
|
||||||
|
BADGE_BRANCH = "ci-badges"
|
||||||
|
|
||||||
|
|
||||||
|
def request_json(url: str, token: str, method: str = "GET",
|
||||||
|
payload: dict | None = None) -> dict:
|
||||||
|
body = None if payload is None else json.dumps(payload).encode()
|
||||||
|
request = Request(url, data=body, method=method,
|
||||||
|
headers={"Authorization": f"token {token}",
|
||||||
|
"Content-Type": "application/json"})
|
||||||
|
with urlopen(request, timeout=20) as response:
|
||||||
|
return json.load(response)
|
||||||
|
|
||||||
|
|
||||||
|
def publish_file(api: str, repo: str, filename: str, data: bytes,
|
||||||
|
token: str, message: str, max_bytes: int = 65_536) -> None:
|
||||||
|
if not filename or filename.startswith("/") or any(
|
||||||
|
part in ("", ".", "..") for part in filename.split("/")):
|
||||||
|
raise ValueError("invalid badge path")
|
||||||
|
if len(data) > max_bytes:
|
||||||
|
raise ValueError("artifact exceeds size limit")
|
||||||
|
path = f"{api}/repos/{repo}/contents/{quote(filename, safe='/')}"
|
||||||
|
encoded = base64.b64encode(data).decode("ascii")
|
||||||
|
for attempt in range(3):
|
||||||
|
try:
|
||||||
|
current = request_json(path + "?ref=" + BADGE_BRANCH, token)
|
||||||
|
if base64.b64decode(current["content"]) == data:
|
||||||
|
return
|
||||||
|
sha = current["sha"]
|
||||||
|
method = "PUT"
|
||||||
|
except HTTPError as error:
|
||||||
|
if error.code != 404:
|
||||||
|
raise
|
||||||
|
sha = None
|
||||||
|
method = "POST"
|
||||||
|
payload = {"branch": BADGE_BRANCH, "content": encoded,
|
||||||
|
"message": message + " [skip ci]"}
|
||||||
|
if sha is not None:
|
||||||
|
payload["sha"] = sha
|
||||||
|
try:
|
||||||
|
request_json(path, token, method, payload)
|
||||||
|
return
|
||||||
|
except HTTPError as error:
|
||||||
|
if error.code not in (409, 422) or attempt == 2:
|
||||||
|
raise
|
||||||
|
raise RuntimeError("badge write did not complete")
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# SPDX-License-Identifier: MIT
|
||||||
|
"""Keep measured main-branch benchmarks in the private CI artifact branch."""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import base64
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
import html
|
||||||
|
import json
|
||||||
|
import math
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import re
|
||||||
|
import subprocess
|
||||||
|
import importlib.util
|
||||||
|
from urllib.error import HTTPError
|
||||||
|
from urllib.parse import quote
|
||||||
|
|
||||||
|
from badge_store import BADGE_BRANCH, publish_file, request_json
|
||||||
|
|
||||||
|
bench_spec = importlib.util.spec_from_file_location(
|
||||||
|
"bench_badge", Path(__file__).with_name("bench-badge.py"))
|
||||||
|
bench_badge = importlib.util.module_from_spec(bench_spec)
|
||||||
|
bench_spec.loader.exec_module(bench_badge)
|
||||||
|
|
||||||
|
INDEX = "history/index.json"
|
||||||
|
MAX_INDEX_ENTRIES = 50
|
||||||
|
|
||||||
|
|
||||||
|
def read_artifact(api: str, repo: str, token: str, filename: str):
|
||||||
|
url = f"{api}/repos/{repo}/contents/{quote(filename, safe='/')}?ref={BADGE_BRANCH}"
|
||||||
|
try:
|
||||||
|
value = request_json(url, token)
|
||||||
|
except HTTPError as error:
|
||||||
|
if error.code == 404:
|
||||||
|
return None
|
||||||
|
raise
|
||||||
|
return json.loads(base64.b64decode(value["content"]))
|
||||||
|
|
||||||
|
|
||||||
|
def valid_benches(metrics: dict) -> dict[str, float]:
|
||||||
|
values = metrics.get("bench_ns_per_op")
|
||||||
|
if not isinstance(values, dict) or not values:
|
||||||
|
raise ValueError("benchmark measurements are missing")
|
||||||
|
if any(not isinstance(key, str) or not re.fullmatch(r"[A-Za-z0-9_]+", key)
|
||||||
|
or not isinstance(value, (float, int)) or isinstance(value, bool)
|
||||||
|
or not math.isfinite(value) or value <= 0 for key, value in values.items()):
|
||||||
|
raise ValueError("benchmark measurements are invalid")
|
||||||
|
return values
|
||||||
|
|
||||||
|
|
||||||
|
def git_distance(ancestor: str, sha: str) -> int | None:
|
||||||
|
result = subprocess.run(("git", "rev-list", "--count", f"{ancestor}..{sha}"),
|
||||||
|
capture_output=True, text=True, check=False)
|
||||||
|
if result.returncode != 0:
|
||||||
|
return None
|
||||||
|
ancestor_check = subprocess.run(("git", "merge-base", "--is-ancestor", ancestor, sha),
|
||||||
|
capture_output=True, check=False)
|
||||||
|
return int(result.stdout.strip()) if ancestor_check.returncode == 0 else None
|
||||||
|
|
||||||
|
|
||||||
|
def previous_measurement(index: dict, sha: str, distance=None) -> dict:
|
||||||
|
distance = distance or git_distance
|
||||||
|
choices = []
|
||||||
|
for entry in index.get("entries", []):
|
||||||
|
ancestor = entry.get("sha")
|
||||||
|
if ancestor == sha or not isinstance(ancestor, str):
|
||||||
|
continue
|
||||||
|
gap = distance(ancestor, sha)
|
||||||
|
if gap is not None and gap > 0:
|
||||||
|
choices.append((gap, entry))
|
||||||
|
if not choices:
|
||||||
|
return {}
|
||||||
|
return valid_benches(min(choices, key=lambda item: item[0])[1]["metrics"])
|
||||||
|
|
||||||
|
|
||||||
|
def updated_index(index: dict, sha: str, metrics: dict, timestamp: str) -> dict:
|
||||||
|
if not re.fullmatch(r"[0-9a-f]{40}", sha):
|
||||||
|
raise ValueError("invalid commit SHA")
|
||||||
|
valid_benches(metrics)
|
||||||
|
entries = [entry for entry in index.get("entries", []) if entry.get("sha") != sha]
|
||||||
|
entries.append({"sha": sha, "timestamp": timestamp, "metrics": metrics})
|
||||||
|
return {"schema": 1, "entries": entries[-MAX_INDEX_ENTRIES:]}
|
||||||
|
|
||||||
|
|
||||||
|
def render_html(index: dict) -> str:
|
||||||
|
entries = list(reversed(index.get("entries", [])))
|
||||||
|
names = sorted({name for entry in entries
|
||||||
|
for name in valid_benches(entry["metrics"])})
|
||||||
|
rows = []
|
||||||
|
for entry in entries:
|
||||||
|
sha = entry["sha"]
|
||||||
|
if not re.fullmatch(r"[0-9a-f]{40}", sha):
|
||||||
|
raise ValueError("invalid commit SHA in history")
|
||||||
|
cells = [f'<td><a href="https://gitea.bluefla.re/blueflare/alkane-toolkit/commit/{sha}">{sha[:10]}</a></td>',
|
||||||
|
f'<td>{html.escape(entry["timestamp"])}</td>']
|
||||||
|
values = valid_benches(entry["metrics"])
|
||||||
|
cells += [f'<td>{values[name]:.3f}</td>' if name in values else '<td>new</td>'
|
||||||
|
for name in names]
|
||||||
|
rows.append("<tr>" + "".join(cells) + "</tr>")
|
||||||
|
headings = "".join(f"<th scope=\"col\">{html.escape(name)} (ns/op)</th>" for name in names)
|
||||||
|
return ("<!doctype html>\n<html lang=\"en\"><head><meta charset=\"utf-8\">"
|
||||||
|
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
|
||||||
|
"<title>Alkane Toolkit benchmarks</title><style>"
|
||||||
|
"body{font:16px system-ui,sans-serif;max-width:1100px;margin:2rem auto;padding:0 1rem;color:#17212b}"
|
||||||
|
"h1{font-size:2rem}p{max-width:70ch;line-height:1.5}"
|
||||||
|
"table{border-collapse:collapse;min-width:100%}th,td{padding:.5rem;border-bottom:1px solid #ccd3da;text-align:right}"
|
||||||
|
"th:first-child,td:first-child{text-align:left}th:nth-child(2),td:nth-child(2){text-align:left}"
|
||||||
|
"th{background:#edf2f6;position:sticky;top:0}a{color:#145a9a}"
|
||||||
|
".scroll{overflow-x:auto}small{color:#55616d}</style></head><body>"
|
||||||
|
"<h1>Alkane Toolkit microbenchmarks</h1>"
|
||||||
|
"<p>Measured on main CI. Lower ns/op is faster. Results depend on runner load and are "
|
||||||
|
"not board performance. A new benchmark has no earlier comparison.</p>"
|
||||||
|
f"<p><small>Most recent {len(entries)} measured commits. Full per-commit JSON is retained "
|
||||||
|
"in the private ci-badges branch.</small></p>"
|
||||||
|
f"<div class=\"scroll\"><table><thead><tr><th scope=\"col\">Commit</th>"
|
||||||
|
f"<th scope=\"col\">UTC</th>{headings}</tr></thead><tbody>{''.join(rows)}"
|
||||||
|
"</tbody></table></div></body></html>\n")
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument("action", choices=("prepare", "publish"))
|
||||||
|
args = parser.parse_args()
|
||||||
|
api = os.environ["GITHUB_API_URL"].rstrip("/")
|
||||||
|
repo = os.environ["GITHUB_REPOSITORY"]
|
||||||
|
token = os.environ["GITEA_TOKEN"]
|
||||||
|
sha = os.environ["GITHUB_SHA"]
|
||||||
|
index = read_artifact(api, repo, token, INDEX) or {"schema": 1, "entries": []}
|
||||||
|
if index.get("schema") != 1 or not isinstance(index.get("entries"), list):
|
||||||
|
raise ValueError("unsupported benchmark history index")
|
||||||
|
if args.action == "prepare":
|
||||||
|
baseline = previous_measurement(index, sha)
|
||||||
|
Path("badges").mkdir(exist_ok=True)
|
||||||
|
Path("badges/bench-baseline.json").write_text(json.dumps(baseline) + "\n")
|
||||||
|
print(f"Loaded {len(baseline)} prior benchmark measurements")
|
||||||
|
return
|
||||||
|
metrics = {"bench_ns_per_op": bench_badge.parse_bench(
|
||||||
|
Path("bench-current.txt").read_text())}
|
||||||
|
timestamp = datetime.now(timezone.utc).isoformat(timespec="seconds")
|
||||||
|
index = updated_index(index, sha, metrics, timestamp)
|
||||||
|
record = json.dumps(index["entries"][-1], indent=2).encode() + b"\n"
|
||||||
|
publish_file(api, repo, f"history/{sha}.json", record, token,
|
||||||
|
"Record Alkane Toolkit benchmarks", max_bytes=1_048_576)
|
||||||
|
publish_file(api, repo, INDEX, json.dumps(index, indent=2).encode() + b"\n",
|
||||||
|
token, "Update Alkane Toolkit benchmark index", max_bytes=1_048_576)
|
||||||
|
Path("bench-site").mkdir(exist_ok=True)
|
||||||
|
Path("bench-site/index.html").write_text(render_html(index))
|
||||||
|
print(f"Recorded benchmarks for {sha[:10]}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# SPDX-License-Identifier: MIT
|
||||||
|
"""Exercise benchmark history selection and safe site rendering."""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||||
|
from bench_history import previous_measurement, render_html, updated_index
|
||||||
|
|
||||||
|
|
||||||
|
class HistoryTests(unittest.TestCase):
|
||||||
|
def test_retry_uses_previous_commit(self):
|
||||||
|
first = "a" * 40
|
||||||
|
second = "b" * 40
|
||||||
|
index = {"schema": 1, "entries": [
|
||||||
|
{"sha": first, "timestamp": "2026-01-01T00:00:00+00:00",
|
||||||
|
"metrics": {"bench_ns_per_op": {"hash": 10.0}}},
|
||||||
|
{"sha": second, "timestamp": "2026-01-02T00:00:00+00:00",
|
||||||
|
"metrics": {"bench_ns_per_op": {"hash": 8.0}}}]}
|
||||||
|
self.assertEqual(previous_measurement(index, second,
|
||||||
|
lambda ancestor, sha: 1 if ancestor == first else None),
|
||||||
|
{"hash": 10.0})
|
||||||
|
updated = updated_index(index, second, {"bench_ns_per_op": {"hash": 7.5}},
|
||||||
|
"2026-01-03T00:00:00+00:00")
|
||||||
|
self.assertEqual(len(updated["entries"]), 2)
|
||||||
|
self.assertEqual(updated["entries"][-1]["metrics"]["bench_ns_per_op"]["hash"], 7.5)
|
||||||
|
|
||||||
|
def test_invalid_measurement_and_sha_rejected(self):
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
updated_index({}, "bad", {"bench_ns_per_op": {"hash": 1}}, "now")
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
updated_index({}, "a" * 40, {"bench_ns_per_op": {"hash": -1}}, "now")
|
||||||
|
|
||||||
|
def test_site_escapes_history_content(self):
|
||||||
|
entry = {"sha": "a" * 40, "timestamp": "<script>alert(1)</script>",
|
||||||
|
"metrics": {"bench_ns_per_op": {"hash": 10.0}}}
|
||||||
|
page = render_html({"entries": [entry]})
|
||||||
|
self.assertIn("<script>", page)
|
||||||
|
self.assertNotIn("<script>", page)
|
||||||
|
self.assertIn("10.000", page)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user