datasette/perf/request-response/tools/summarize_all.py
Claude d1dbb40362
Add request/response profiling harness and proposed optimisation patches
Tools for profiling Datasette's request/response cycle in-process with
Python 3.15's profiling.sampling, plus the patches proposed by the study
(one per optimisation, and ALL-combined.diff). The patches are not
applied to the code. A report with measurements follows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F8mEgRS8swZfYqwqrVMeo5
2026-10-13 00:12:35 +00:00

26 lines
817 B
Python

import collections
import json
import sys
import analyze_collapsed as A
eps = sys.argv[1].split(",")
prefix = sys.argv[2] if len(sys.argv) > 2 else "profiles/"
out = {}
for ep in eps:
stacks = A.load(
f"{prefix}{ep}.collapsed",
"_run_once",
r"(Driver\.setup|invoke_startup|_startup_sequence|make_ds)",
)
total = sum(c for _, c in stacks)
b = collections.Counter()
for f, c in stacks:
b[A.bucket_for(f)] += c
idle = b.pop(A.IDLE, 0)
busy = total - idle
out[ep] = {"idle_pct": idle / total, "busy": {k: v / busy for k, v in b.items()}}
top = ", ".join(f"{k} {v/busy:.0%}" for k, v in b.most_common(7))
print(f"{ep:18s} idle {idle/total:5.1%} | {top}")
with open(prefix.rstrip("/") + "_buckets.json", "w") as fp:
json.dump(out, fp, indent=1)