|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""One-off: split site/data/<profile>-<conns>.json into per-framework files. |
| 3 | +
|
| 4 | +Every framework's results used to live in 52 shared arrays, so two pull |
| 5 | +requests saving results touched the same files and conflicted (#751). After |
| 6 | +this each framework owns exactly one file, and concurrent runs cannot collide. |
| 7 | +
|
| 8 | + site/data/baseline-512.json [ {actix...}, {hyper...}, ... ] -> |
| 9 | + site/data/results/actix.json { framework, results: { "baseline-512": {...} } } |
| 10 | +
|
| 11 | +Rows are copied verbatim, so the generated data.js is unchanged. Run once; |
| 12 | +rebuild_site_data.py writes the new layout from then on. |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | +import argparse |
| 17 | +import json |
| 18 | +import re |
| 19 | +import sys |
| 20 | +from pathlib import Path |
| 21 | + |
| 22 | +# Files in site/data that are not per-profile result arrays. |
| 23 | +NON_RESULT = {"frameworks.json", "current.json", "langcolors.json"} |
| 24 | + |
| 25 | + |
| 26 | +def slug(name: str) -> str: |
| 27 | + """Filename for a display name. Two entries ('aspnet-minimal + nginx' and |
| 28 | + '+ caddy') contain spaces and a plus, so names cannot be used directly.""" |
| 29 | + s = re.sub(r"[^A-Za-z0-9._-]+", "-", name).strip("-") |
| 30 | + return s.lower() or "unnamed" |
| 31 | + |
| 32 | + |
| 33 | +def collect(site_data: Path) -> dict[str, dict]: |
| 34 | + """{slug: {framework, results: {profile-conns: row}}} from the flat files.""" |
| 35 | + out: dict[str, dict] = {} |
| 36 | + for f in sorted(site_data.glob("*.json")): |
| 37 | + if f.name in NON_RESULT: |
| 38 | + continue |
| 39 | + try: |
| 40 | + rows = json.loads(f.read_text(encoding="utf-8")) |
| 41 | + except Exception as e: |
| 42 | + print(f"[warn] skipping {f.name}: {e}", file=sys.stderr) |
| 43 | + continue |
| 44 | + if not isinstance(rows, list): |
| 45 | + continue |
| 46 | + key = f.stem # e.g. "baseline-512" |
| 47 | + for row in rows: |
| 48 | + if not isinstance(row, dict): |
| 49 | + continue |
| 50 | + name = row.get("framework") |
| 51 | + if not name: |
| 52 | + continue |
| 53 | + entry = out.setdefault(slug(name), {"framework": name, "results": {}}) |
| 54 | + if entry["framework"] != name: |
| 55 | + print(f"[warn] slug collision: {entry['framework']!r} vs {name!r}", file=sys.stderr) |
| 56 | + entry["results"][key] = row |
| 57 | + return out |
| 58 | + |
| 59 | + |
| 60 | +def main() -> None: |
| 61 | + ap = argparse.ArgumentParser() |
| 62 | + ap.add_argument("--root", default=str(Path(__file__).resolve().parent.parent)) |
| 63 | + ap.add_argument("--dry-run", action="store_true") |
| 64 | + args = ap.parse_args() |
| 65 | + |
| 66 | + site_data = Path(args.root) / "site" / "data" |
| 67 | + results_dir = site_data / "results" |
| 68 | + |
| 69 | + frameworks = collect(site_data) |
| 70 | + n_rows = sum(len(e["results"]) for e in frameworks.values()) |
| 71 | + print(f"{len(frameworks)} frameworks, {n_rows} rows") |
| 72 | + |
| 73 | + if args.dry_run: |
| 74 | + for s, e in sorted(frameworks.items())[:5]: |
| 75 | + print(f" {s}.json <- {e['framework']} ({len(e['results'])} rows)") |
| 76 | + return |
| 77 | + |
| 78 | + results_dir.mkdir(parents=True, exist_ok=True) |
| 79 | + for s, entry in sorted(frameworks.items()): |
| 80 | + entry["results"] = dict(sorted(entry["results"].items())) |
| 81 | + (results_dir / f"{s}.json").write_text(json.dumps(entry, indent=2) + "\n", encoding="utf-8") |
| 82 | + |
| 83 | + removed = 0 |
| 84 | + for f in sorted(site_data.glob("*.json")): |
| 85 | + if f.name in NON_RESULT: |
| 86 | + continue |
| 87 | + f.unlink() |
| 88 | + removed += 1 |
| 89 | + print(f"wrote {len(frameworks)} files to {results_dir.relative_to(Path(args.root))}, " |
| 90 | + f"removed {removed} flat files") |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + main() |
0 commit comments