"""What every phase shares: the registry, the session factory, the recorder. `bench.py` owns the command line and the take-3 table's phases; `phases_new.py` owns the phases for what 0.44–0.49 added. Both import from here, so the run's configuration lives in exactly one place. `ARGS` is a `Namespace` that is *mutated* by [`configure`], never rebound, so `from harness import ARGS` in another module keeps seeing the live values. """ import argparse import json import os import time import sites from lspclient import Session, find_position, summarize, uri # noqa: F401 PHASES = {} #: The run's configuration. Filled by `configure()`; never reassigned. ARGS = argparse.Namespace( server=None, label=None, opentitan=None, storage=None, out=None, tries=3, probe="", ) def configure(parsed): """Adopt a parsed command line, and point `sites` at the corpus.""" for key, value in vars(parsed).items(): setattr(ARGS, key, value) ARGS.opentitan = os.path.abspath(ARGS.opentitan) ARGS.out = os.path.abspath(ARGS.out) sites.ROOT = ARGS.opentitan os.makedirs(ARGS.storage, exist_ok=True) def phase(name): """Register a phase under the name `bench.py ` runs.""" def register(fn): PHASES[name] = fn return fn return register def record(**fields): """Append one row to results.jsonl and echo it as a readable line.""" fields.setdefault("ts", time.strftime("%Y-%m-%dT%H:%M:%S")) fields.setdefault("label", ARGS.label) with open(ARGS.out, "a", encoding="utf-8") as handle: handle.write(json.dumps(fields) + "\n") ms = fields.get("ms") shown = " ".join("%.3f" % v for v in ms) if ms else "" print("%-11s %-52s %s %s" % (fields["phase"], fields.get("row", ""), shown, fields.get("note", "")), flush=True) def session(scope="workspace", cold=False, resolve_support=True, verbose=False, storage_name=None): """A session at one scope, its index store under `--storage/`. Never the default store: the corpus must not be written near, and cache warmth has to be controllable per cell. """ store = os.path.join(ARGS.storage, storage_name or scope) return Session( server=ARGS.server, root=sites.ROOT, storage=store, scope=sites.SCOPES[scope], resolve_support=resolve_support, verbose=verbose, cold=cold, ) def settled(s, open_file=sites.TB): """Initialize, open a buffer, and wait for the scan to go quiet.""" s.initialize() s.did_open(sites.path(open_file)) s.wait_quiet(1.5) s.take_notifications() return s def tries(s, method, params, n=None): """`n` identical requests; returns the millisecond list.""" n = n or ARGS.tries return [s.ms(method, params) for _ in range(n)] def pos(relative, needle, occurrence=1, offset=0): """0-based `{line, character}` of a token, resolved from the file text.""" line, character = find_position(sites.path(relative), needle, occurrence, offset) return {"line": line, "character": character} def doc(relative): return {"uri": uri(sites.path(relative))} def at(relative, needle, occurrence=1, offset=0): """A position request's params at a token.""" return {"textDocument": doc(relative), "position": pos(relative, needle, occurrence, offset)} def code_action_params(relative, needle, occurrence=1, offset=0): """A zero-width `textDocument/codeAction` range at a token.""" where = pos(relative, needle, occurrence, offset) return { "textDocument": doc(relative), "range": {"start": where, "end": where}, "context": {"diagnostics": []}, } def result_count(message): """How many things an answer carried — a list, an item list, or one thing.""" result = message.get("result") 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