Files
Language-Server/tests/performance/document_sync.py
T
faumaray da8003f6d4 Implement the VLS roadmap on the native VBS build
Native VBS and CMake builds of the server and every registered suite:
protocol and document sync, VBS/compdb providers, stock GCC diagnostics
(including in-process default profile preparation), the stock occurrence
observer and hover/definition, the bounded Scheduler, C++ module graph,
scan and artifact cache with real CMI production, the context-aware
symbol index with schema-2 snapshot migration, navigation and guarded
rename, reflection provenance with splice-origin extraction, index-backed
completion and signature help, live GCC worker control with a registered
DAP suite, and the Neovim and VS Code clients.

VoidLibrary follows main. The private GCC 16.2.0 frontend is reproduced
from the versioned patches and verified stepping reflection/template code
with projected values 17/34 and 41/82.
2026-12-05 14:06:50 +03:00

118 lines
5.1 KiB
Python

"""Measure an incremental edit and the following JSON-RPC round trip on Linux."""
import argparse
import json
import math
from pathlib import Path
import resource
import signal
import statistics
import subprocess
import tempfile
import time
def send(server, message):
body = json.dumps({"jsonrpc": "2.0", **message}, separators=(",", ":")).encode()
server.stdin.write(f"Content-Length: {len(body)}\r\n\r\n".encode() + body)
server.stdin.flush()
def request(server, ident, method, params=None):
message = {"id": ident, "method": method}
if params is not None:
message["params"] = params
send(server, message)
header = server.stdout.readline(100)
if not header.startswith(b"Content-Length: ") or server.stdout.readline(3) != b"\r\n":
raise ValueError(f"unexpected response frame {header!r}")
length = int(header.split(b":", 1)[1])
if not 0 < length < 8192:
raise ValueError("unexpected response size")
response = json.loads(server.stdout.read(length))
if response.get("id") != ident:
raise ValueError("response ID mismatch")
return response
def measure(executable, size, samples, long_line=None):
with tempfile.TemporaryDirectory() as work, subprocess.Popen(
[str(executable), "--stdio"], cwd=work, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
) as server:
try:
result = request(server, 1, "initialize", {})
if result["result"]["capabilities"]["textDocumentSync"]["change"] != 2:
raise ValueError("server does not support incremental synchronization")
send(server, {"method": "initialized", "params": {}})
uri = "untitled:document-edit-benchmark"
column = 2
if long_line:
text = "a" * size
line_count = 1
if long_line == "end":
column = size - 3
else:
line = "a" * 63 + "\n"
line_count = size // len(line)
text = line * line_count
document = {"uri": uri, "languageId": "cpp", "version": 1, "text": text}
send(server, {"method": "textDocument/didOpen", "params": {"textDocument": document}})
# An unknown request is a synchronously observable barrier in this session.
def barrier(ident):
if request(server, ident, "void-benchmark/barrier")["error"]["code"] != -32601:
raise ValueError("unexpected barrier result")
barrier(2)
timings = []
for sample in range(samples):
index = (sample * 7919) % line_count
selection = {"start": {"line": index, "character": column},
"end": {"line": index, "character": column + 1}}
change = {"range": selection, "text": chr(65 + sample % 26)}
start = time.perf_counter_ns()
send(server, {"method": "textDocument/didChange", "params": {
"textDocument": {"uri": uri, "version": sample + 2}, "contentChanges": [change],
}})
barrier(sample + 3)
timings.append((time.perf_counter_ns() - start) / 1e6)
send(server, {"method": "textDocument/didClose", "params": {"textDocument": {"uri": uri}}})
if request(server, samples + 3, "shutdown")["result"] is not None:
raise ValueError("unexpected shutdown result")
send(server, {"method": "exit"})
server.stdin.close()
if server.wait(timeout=5) != 0 or server.stderr.read(4096) or server.stdout.read(1):
raise RuntimeError("server emitted diagnostics, extra output, or a nonzero exit")
finally:
if server.poll() is None:
server.kill()
server.wait()
result = {
"document_bytes": size, "samples": samples,
"median_ms": round(statistics.median(timings), 2),
"p95_ms": round(sorted(timings)[math.ceil(samples * .95) - 1], 2),
"peak_rss_kib": resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss,
}
if long_line:
result.update(long_line=long_line, total_ms=round(sum(timings), 2))
return result
def timed_out(signum, frame):
raise TimeoutError("document benchmark exceeded its 120 second deadline")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("server", type=Path)
workload = parser.add_mutually_exclusive_group()
workload.add_argument("--mib", type=int, choices=(1, 4, 8), default=1)
workload.add_argument("--long-line", choices=("start", "end"),
help="edit near the start or end of one 64 KiB ASCII line")
parser.add_argument("--samples", type=int, choices=range(1, 101), metavar="1..100", default=20)
args = parser.parse_args()
signal.signal(signal.SIGALRM, timed_out)
signal.alarm(120)
size = 64 << 10 if args.long_line else args.mib << 20
print(json.dumps(measure(args.server.resolve(strict=True), size, args.samples, args.long_line)))