Files
lucisv/tools/perf/phases_new.py
T

192 lines
8.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""The phases for what 0.44–0.49 added: the new offer sites, §49's
hierarchical surface, §46's `didOpen` fix on the wire, `lucisv/moduleHierarchy`
and the index's cost on disk and in memory.
Imported by `bench.py`, which owns the registry and the command line; nothing
here is run directly. Split out of `bench.py` so each file stays inside
CLAUDE.md's size rule and each has one sentence: `bench.py` is the take-3
table, this is what changed since it.
"""
import json
import os
import subprocess
import time
from harness import (
ARGS, Session, at, code_action_params, doc, phase, pos, record,
result_count, session, settled, sites, tries,
)
@phase("refactor")
def phase_refactor():
"""The offer sites 0.44–0.49 added: what is offered, and what it costs."""
for label, relative, needle, occurrence, offset in sites.REFACTOR_SITES:
with session("workspace", resolve_support=True) as s:
settled(s)
s.did_open(sites.path(relative))
s.wait_quiet(0.6)
s.take_notifications()
params = code_action_params(relative, needle, occurrence, offset)
ms = tries(s, "textDocument/codeAction", params, ARGS.tries + 1)
_, message = s.request("textDocument/codeAction", params)
offered = message.get("result") or []
record(phase="refactor", row="%s offer" % label, ms=ms, n=len(offered),
titles=[a.get("title") for a in offered],
file=relative, position=params["range"]["start"])
for action in offered:
if "edit" in action:
continue
resolves = [s.ms("codeAction/resolve", action) for _ in range(ARGS.tries + 1)]
_, resolved = s.request("codeAction/resolve", action)
edit = (resolved.get("result") or {}).get("edit") or {}
record(phase="refactor", row="%s resolve: %s" % (label, action.get("title")),
ms=resolves, n=len(edit.get("changes") or
edit.get("documentChanges") or []))
# `extract module` is a selection, not a point.
relative, first, first_n, last, last_n = sites.EXTRACT_SELECTION
start = pos(relative, first, first_n)
end = pos(relative, last, last_n)
end = {"line": end["line"], "character": end["character"] + len(last)}
with session("workspace", resolve_support=True) as s:
settled(s)
s.did_open(sites.path(relative))
s.wait_quiet(0.6)
s.take_notifications()
params = {"textDocument": doc(relative),
"range": {"start": start, "end": end},
"context": {"diagnostics": []}}
ms = tries(s, "textDocument/codeAction", params, ARGS.tries + 1)
_, message = s.request("textDocument/codeAction", params)
offered = message.get("result") or []
record(phase="refactor", row="extract-module offer (selection)", ms=ms, n=len(offered),
titles=[a.get("title") for a in offered], file=relative,
position=start, note="selection %d:%d-%d:%d" % (
start["line"], start["character"], end["line"], end["character"]))
for action in offered:
if "edit" in action:
continue
resolves = [s.ms("codeAction/resolve", action) for _ in range(ARGS.tries + 1)]
record(phase="refactor", row="extract-module resolve: %s" % action.get("title"),
ms=resolves)
@phase("tailrename")
def phase_tailrename():
"""§49 — a net renamed at its declaration, named through an instance tail."""
relative, needle, occurrence, offset = sites.TAIL_RENAME
with session("workspace") as s:
settled(s)
s.did_open(sites.path(relative))
s.did_open(sites.path(sites.TAIL_TAIL_FILE))
s.wait_quiet(0.8)
s.take_notifications()
where = at(relative, needle, occurrence, offset)
record(phase="tailrename", row="tail rename cursor", ms=[0.0],
note=json.dumps(where["position"]), file=relative)
params = dict(where, newName="state_q_x")
ms = tries(s, "textDocument/rename", params, ARGS.tries + 1)
_, message = s.request("textDocument/rename", params)
uris = edited_uris(message)
tail = os.path.basename(sites.TAIL_TAIL_FILE)
record(phase="tailrename",
row="rename state_q (tail through u_entropy_src_main_sm)",
ms=ms, n=len(uris),
note="tail edited" if any(tail in u for u in uris) else "TAIL NOT EDITED",
titles=sorted(os.path.basename(u) for u in uris))
def edited_uris(message):
"""The URIs a `WorkspaceEdit` touches, whichever shape it came in."""
edit = message.get("result") or {}
if not isinstance(edit, dict):
return []
if edit.get("changes"):
return list(edit["changes"].keys())
return [c.get("textDocument", {}).get("uri", "")
for c in edit.get("documentChanges") or []]
@phase("didopen")
def phase_didopen():
"""§46 — `didOpen` → the request behind it, workspace scope, warm store."""
for relative in sites.DIDOPEN_FILES:
runs, seconds = [], []
for _ in range(ARGS.tries):
with session("workspace") as s:
settled(s)
started = time.perf_counter()
s.did_open(sites.path(relative))
s.ms("textDocument/definition", at(relative, "module ", 1, 0))
runs.append((time.perf_counter() - started) * 1000.0)
seconds.append(s.ms("textDocument/definition", at(relative, "module ", 1, 0)))
lines = sum(1 for _ in open(sites.path(relative), encoding="utf-8", errors="replace"))
record(phase="didopen", row="didOpen %s -> definition" % os.path.basename(relative),
ms=runs, n=lines,
note="second definition " + " / ".join("%.2f" % v for v in seconds))
@phase("tailcompletion")
def phase_tailcompletion():
"""Completion after `u_x.` on an instance head (§49's surface)."""
relative, needle, occurrence, offset = sites.TAIL_HEAD_COMPLETION
with session("workspace") as s:
settled(s)
s.did_open(sites.path(relative))
s.wait_quiet(0.8)
s.take_notifications()
where = at(relative, needle, occurrence, offset)
ms = tries(s, "textDocument/completion", where, ARGS.tries + 1)
_, message = s.request("textDocument/completion", where)
items = (message.get("result") or {})
items = items.get("items", items) if isinstance(items, dict) else items
record(phase="tailcompletion",
row="completion after an instance head + '.' (%s)" % os.path.basename(
sites.path(relative)),
ms=ms, n=result_count(message),
titles=[i.get("label") for i in (items or [])[:8]])
@phase("hierarchy")
def phase_hierarchy():
with session("workspace") as s:
settled(s)
ms = tries(s, "lucisv/moduleHierarchy", {}, ARGS.tries + 1)
_, message = s.request("lucisv/moduleHierarchy", {})
result = message.get("result") or {}
payload = len(json.dumps(result))
record(phase="hierarchy", row="lucisv/moduleHierarchy", ms=ms, n=payload,
note="modules %d, files %d, schema %s" % (
len(result.get("modules") or []), len(result.get("files") or []),
result.get("schemaVersion")))
@phase("rss")
def phase_rss():
"""Server peak RSS after the workspace scan, and the index store on disk.
RSS is read with `ps -o rss=` on the server's own pid a second after the
scan settles, so it is the resident set of a server that has finished
indexing and is idle — what an editor session actually holds.
"""
store = os.path.join(ARGS.storage, "rss")
for cache in ("cold", "warm"):
rss, size = [], "0"
for _ in range(ARGS.tries):
s = Session(server=ARGS.server, root=sites.ROOT, storage=store,
scope=sites.SCOPES["workspace"], cold=(cache == "cold"))
with s:
settled(s)
time.sleep(1.0)
rss.append(float(subprocess.run(
["ps", "-o", "rss=", "-p", str(s.proc.pid)],
capture_output=True, text=True).stdout.strip()))
size = subprocess.run(["du", "-sk", store], capture_output=True,
text=True).stdout.split()[0]
record(phase="rss", row="server RSS after workspace scan, %s cache" % cache,
ms=rss, n=int(size),
note="RSS KB above; index store %s KB" % size)