Files

169 lines
6.5 KiB
Python

"""Schema 2 for `results.jsonl`: what a row means, written into the row.
Take 4's rows (schema 1, and no `schema` key) said `phase`, `label`, `row`,
`ms`, `n`, `titles`, `note` and nothing else, so a reader had to know from
prose that `rss.ms` held KiB, that `offer-queue.ms` held a leader and a
follower rather than repeats, and that a request which *failed* was recorded
as a fast success. Take 5's §P0 makes each of those explicit:
- `schema` — 2 here. A row without the key is schema 1; read it with take 4's
prose. Nothing is rewritten, so both live in one file.
- `units` — what `ms` (or the row's own value fields) is counted in. Latency
is `ms` and only `ms`; memory and size get their own fields (`rss_kib`,
`peak_rss_kib`, `store_kib`) which are never `ms`.
- `status` — `ok`, `error`, `refused` or `capture`, plus `message` for
anything but `ok`. **No document may quote a row whose status is not `ok`,
`capture`, or exactly its own `expected_status`** — the last is for the one
row whose designed outcome *is* a rejection (the transport floor asks for a
method that does not exist). [`quotable`] is that rule in code.
- `result_count` — how many things the answer carried, so a timing that moved
because the *answer* moved is not read as a timing change.
- `client` — the capability shape that asked (`resolveSupport`, `applyEdit`,
`createFiles`), because the same cursor answers differently to each.
- `fresh_process` — whether each try was its own server, or repeats in one.
- `fingerprint` (edit-bearing rows) — sha256 over the sorted
`(uri, start, end, newText)` edits, the sorted created paths and the sorted
warnings, so two runs' edits are compared without quoting the corpus.
Python 3 standard library only, like the rest of `tools/perf`.
"""
import hashlib
import json
#: The version stamped into every row this harness writes.
SCHEMA = 2
#: `ok` answered; `error` failed; `refused` is the engine's own refusal
#: sentence (JSON-RPC `RequestFailed`); `capture` is *this client* declining
#: an edit it was pushed, which is a benchmark decision and not a server one.
STATUSES = ("ok", "error", "refused", "capture")
#: LSP's `RequestFailed`. The server returns a refusal sentence with this
#: code (a declined rename, an engine "cannot inline that"); any other code
#: is a real error.
REQUEST_FAILED = -32803
#: What this client tells the server when it declines a pushed edit. The
#: corpus is read-only: an edit is measured, never applied.
CAPTURE_REASON = "perf capture: the benchmark client never writes"
def status_of(message):
"""`(status, message_text)` for one JSON-RPC response."""
error = message.get("error") if isinstance(message, dict) else None
if not error:
return "ok", None
text = error.get("message") or json.dumps(error)
if error.get("code") == REQUEST_FAILED:
return "refused", text
return "error", "code %s: %s" % (error.get("code"), text)
def quotable(row):
"""Whether a document may quote this row's numbers.
`ok` answered and `capture` is this client declining an edit it asked to
be shown — both are measurements. An `error` or `refused` row measured a
rejection, and its time is not the operation's time; the sole exception is
a row whose `expected_status` names the rejection it was built to get.
"""
status = row.get("status", "ok")
return status in ("ok", "capture") or status == row.get("expected_status")
def worst(statuses):
"""The status a row of several tries reports: any failure outranks `ok`."""
for rank in ("error", "refused", "capture", "ok"):
if rank in statuses:
return rank
return "ok"
def result_count(message):
"""How many things an answer carried — a list, an item list, or one."""
result = message.get("result") if isinstance(message, dict) else None
if result is None:
return 0
if isinstance(result, list):
return len(result)
if isinstance(result, dict) and "items" in result:
return len(result["items"])
return 1
def _one(uri, edit):
"""One text edit as a comparable tuple."""
where = edit.get("range") or {}
start, end = where.get("start") or {}, where.get("end") or {}
return [
uri,
start.get("line"), start.get("character"),
end.get("line"), end.get("character"),
edit.get("newText"),
]
def normalized(edit):
"""`(edits, created)` from a `WorkspaceEdit`, both sorted.
Both shapes are read: the `changes` map every client understands, and the
`documentChanges` list a resource operation forces (an extraction that
creates a file cannot use the map).
"""
edits, created = [], []
if not isinstance(edit, dict):
return edits, created
for uri, ones in (edit.get("changes") or {}).items():
edits.extend(_one(uri, one) for one in ones or [])
for change in edit.get("documentChanges") or []:
if not isinstance(change, dict):
continue
kind = change.get("kind")
if kind in ("create", "rename", "delete"):
created.append("%s %s" % (kind, change.get("uri") or change.get("newUri") or ""))
continue
uri = (change.get("textDocument") or {}).get("uri", "")
edits.extend(_one(uri, one) for one in change.get("edits") or [])
edits.sort(key=json.dumps)
created.sort()
return edits, created
def counts(edit):
"""`{edits, files, created}` for a `WorkspaceEdit`."""
edits, created = normalized(edit)
return {
"edits": len(edits),
"files": len({one[0] for one in edits}),
"created": len(created),
}
def fingerprint(edit, warnings=()):
"""sha256 over the sorted edits, created paths and warnings.
Two runs that computed the same refactoring have the same fingerprint,
and no corpus text has to be copied out to say so.
"""
payload = json.dumps(
{"edits": normalized(edit)[0], "created": normalized(edit)[1],
"warnings": sorted(warnings)},
sort_keys=True,
)
return hashlib.sha256(payload.encode("utf-8")).hexdigest()
def edit_of(message):
"""The `WorkspaceEdit` in a response, whichever request produced it.
`textDocument/rename` answers with the edit itself; `codeAction/resolve`
answers with a code action carrying one.
"""
result = message.get("result") if isinstance(message, dict) else None
if not isinstance(result, dict):
return {}
if "edit" in result:
return result.get("edit") or {}
return result