diff --git a/apps/desktop/scripts/perf/gateway_attach_bench.py b/apps/desktop/scripts/perf/gateway_attach_bench.py new file mode 100644 index 0000000000..efc87f32cc --- /dev/null +++ b/apps/desktop/scripts/perf/gateway_attach_bench.py @@ -0,0 +1,188 @@ +"""Measure the gateway's attach-RPC dispatch, against the real dispatcher. + +Every attach handler (image.attach, image.attach_bytes, file.attach, +clipboard.paste, pdf.attach) resolves its session through ``_sess()``, which +blocks on the deferred agent build. None of them is in ``_LONG_HANDLERS``, so +that block happens INLINE on the socket reader thread. + +This drives the real ``tui_gateway.server.dispatch`` with a session whose +agent build has not completed, and times it. ``prompt.submit`` (which uses +``_sess_nowait``) is timed alongside as the control — it is the path that +stays instant today. + + python3 scripts/perf/gateway_attach_bench.py [--build-seconds 8] [--rounds 3] +""" + +from __future__ import annotations + +import argparse +import base64 +import os +import statistics +import sys +import tempfile +import threading +import time +from pathlib import Path + +REPO = Path(__file__).resolve().parents[4] +sys.path.insert(0, str(REPO)) + +os.environ.setdefault("HERMES_HOME", tempfile.mkdtemp(prefix="hermes-bench-home-")) + + +class CollectTransport: + """Stand-in for the WS transport: records frames, never touches a socket.""" + + def __init__(self) -> None: + self.frames: list[dict] = [] + self.lock = threading.Lock() + + def write(self, obj: dict) -> bool: + with self.lock: + self.frames.append(obj) + return True + + def close(self) -> None: + return None + + +def make_session(server, sid: str, *, build_seconds: float, home: Path) -> dict: + """A session whose deferred agent build is still running. + + Mirrors the shape ``_deferred_build`` leaves behind: an unset ``agent_ready`` + event plus a live build thread. That is exactly the state a session is in + for the first seconds after ``session.create`` — which is when a user + pastes their first image. + """ + ready = threading.Event() + session: dict = { + "agent": None, + "agent_ready": ready, + "agent_error": None, + "attached_images": [], + "cwd": str(home), + "history": [], + "history_lock": threading.RLock(), + "history_version": 0, + "image_counter": 0, + "profile_home": str(home), + "running": False, + "session_key": sid, + "transport": None, + } + + def build() -> None: + time.sleep(build_seconds) + ready.set() + + thread = threading.Thread(target=build, daemon=True) + session["_agent_build_thread"] = thread + thread.start() + + server._sessions[sid] = session + return session + + +def png_bytes(kb: int) -> bytes: + body = bytearray(b"\x89PNG\r\n\x1a\n") + body.extend(bytes((i * 37) & 0xFF for i in range(kb * 1024))) + return bytes(body) + + +def main() -> int: + ap = argparse.ArgumentParser() + ap.add_argument("--build-seconds", type=float, default=8.0) + ap.add_argument("--rounds", type=int, default=3) + ap.add_argument("--kb", type=int, default=900) + args = ap.parse_args() + + from tui_gateway import server + + # The build is already in flight for these sessions (that is the state the + # bench recreates), so the "start one if none is running" call is a no-op. + # Without this stub the real builder races the bench's controlled one and + # completes instantly, hiding the very wait being measured. + server._start_agent_build = lambda sid, session: None + + # Keep the run readable: session.info frames go to the transport, not stdout. + server._emit = lambda *a, **k: None + + home = Path(os.environ["HERMES_HOME"]) + home.mkdir(parents=True, exist_ok=True) + + content_b64 = base64.b64encode(png_bytes(args.kb)).decode("ascii") + + scratch = home / "scratch.txt" + scratch.write_text("hello from the bench\n") + + calls = [ + ( + "image.attach_bytes", + lambda sid: { + "session_id": sid, + "content_base64": content_b64, + "filename": "bench.png", + }, + ), + ( + "file.attach", + lambda sid: { + "session_id": sid, + "name": "scratch.txt", + "path": str(scratch), + }, + ), + ( + "prompt.submit", + lambda sid: {"session_id": sid, "text": "control: plain text"}, + ), + ] + + print( + f"agent build takes {args.build_seconds:.1f}s; " + f"image is {args.kb} KB; {args.rounds} rounds\n" + ) + print(f"{'rpc':<22} {'in _LONG_HANDLERS':<19} {'mean':>8} {'max':>8} blocks reader?") + + for method, build_params in calls: + samples: list[float] = [] + + for round_index in range(args.rounds): + sid = f"bench-{method}-{round_index}" + make_session(server, sid, build_seconds=args.build_seconds, home=home) + transport = CollectTransport() + req = { + "jsonrpc": "2.0", + "id": round_index, + "method": method, + "params": build_params(sid), + } + + start = time.perf_counter() + try: + server.dispatch(req, transport) + except Exception as exc: # noqa: BLE001 - report, don't mask + print(f" ! {method} raised {type(exc).__name__}: {exc}") + samples.append(time.perf_counter() - start) + + server._sessions.pop(sid, None) + + pooled = method in server._LONG_HANDLERS + mean = statistics.mean(samples) + worst = max(samples) + verdict = "no (pooled)" if pooled else ("YES" if mean > 1.0 else "no") + + print( + f"{method:<22} {str(pooled):<19} {mean:>7.2f}s {worst:>7.2f}s {verdict}" + ) + + print( + "\ndispatch() returns immediately for pooled handlers, so a pooled timing\n" + "is the enqueue cost — the work still happens, just off the reader thread." + ) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/apps/desktop/scripts/perf/image-attach-bench.mjs b/apps/desktop/scripts/perf/image-attach-bench.mjs new file mode 100644 index 0000000000..ac723d1980 --- /dev/null +++ b/apps/desktop/scripts/perf/image-attach-bench.mjs @@ -0,0 +1,307 @@ +// Measures the desktop image-attach pipeline stage by stage on a real image, +// against the real renderer helpers. No Electron, no LLM — just the transforms +// an attached image goes through between the paperclip and prompt.submit. +// +// node scripts/perf/image-attach-bench.mjs [--kb 900] [--rounds 7] + +import { readFileSync, writeFileSync, mkdtempSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { performance } from 'node:perf_hooks' + +const args = process.argv.slice(2) +const flag = (name, fallback) => { + const i = args.indexOf(`--${name}`) + + return i >= 0 ? Number(args[i + 1]) : fallback +} + +const ROUNDS = flag('rounds', 7) +const SIZES_KB = args.includes('--kb') ? [flag('kb', 900)] : [120, 900, 3200] + +const dir = mkdtempSync(join(tmpdir(), 'hermes-img-bench-')) + +/** A PNG-shaped byte blob of a given size. The pipeline treats it as opaque + * bytes everywhere we measure, so the pixels don't matter — the length does. */ +function makeImage(kb) { + const bytes = Buffer.alloc(kb * 1024) + Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]).copy(bytes) + + for (let i = 8; i < bytes.length; i += 1) { + bytes[i] = (i * 2654435761) & 0xff + } + + const path = join(dir, `img-${kb}kb.png`) + writeFileSync(path, bytes) + + return path +} + +const stat = samples => { + const s = [...samples].sort((a, b) => a - b) + + return { + mean: s.reduce((a, b) => a + b, 0) / s.length, + p50: s[Math.floor(s.length * 0.5)], + p95: s[Math.min(s.length - 1, Math.floor(s.length * 0.95))], + max: s[s.length - 1] + } +} + +const time = fn => { + const t0 = performance.now() + const out = fn() + + return { ms: performance.now() - t0, out } +} + +// --- the stages, transcribed from the shipped code paths ------------------- + +// electron/hardening.ts :: readFileDataUrlForIpc — main-process side of +// window.hermesDesktop.readFileDataUrl. +const readFileDataUrl = path => { + const data = readFileSync(path) + + return `data:image/png;base64,${data.toString('base64')}` +} + +// use-prompt-actions/utils.ts :: base64FromDataUrl +const base64FromDataUrl = dataUrl => { + const comma = dataUrl.indexOf(',') + + return comma >= 0 ? dataUrl.slice(comma + 1) : '' +} + +// The JSON-RPC frame the renderer sends for image.attach_bytes. +const encodeRpcFrame = (base64, filename) => + JSON.stringify({ + jsonrpc: '2.0', + id: 1, + method: 'image.attach_bytes', + params: { session_id: 'bench', content_base64: base64, filename } + }) + +// lib/embedded-images.ts :: extractEmbeddedImages — runs on the optimistic +// bubble text on EVERY DirectiveContent render, and the composer's base64 +// preview is what it scans. +const DATA_IMAGE_PREFIX = 'data:image/' +const BASE64_MARKER = ';base64,' +const MIN_EMBEDDED_IMAGE_BASE64_LENGTH = 64 + +const isImageMimeCode = c => + (c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122) || c === 43 || c === 45 || c === 46 || c === 95 + +const isBase64Code = c => + (c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122) || c === 43 || c === 47 || c === 61 + +function readDataImageUrl(text, start) { + if (!text.startsWith(DATA_IMAGE_PREFIX, start)) { + return null + } + + let cursor = start + DATA_IMAGE_PREFIX.length + + while (cursor < text.length && isImageMimeCode(text.charCodeAt(cursor))) { + cursor += 1 + } + + if (cursor === start + DATA_IMAGE_PREFIX.length || !text.startsWith(BASE64_MARKER, cursor)) { + return null + } + + cursor += BASE64_MARKER.length + const base64Start = cursor + + while (cursor < text.length && isBase64Code(text.charCodeAt(cursor))) { + cursor += 1 + } + + if (cursor - base64Start < MIN_EMBEDDED_IMAGE_BASE64_LENGTH) { + return null + } + + return { end: cursor, url: text.slice(start, cursor) } +} + +function extractEmbeddedImages(text) { + if (!text || !text.includes(DATA_IMAGE_PREFIX)) { + return { cleanedText: text, images: [] } + } + + const images = [] + const pieces = [] + let appendCursor = 0 + let searchCursor = 0 + + while (searchCursor < text.length) { + const dataStart = text.indexOf(DATA_IMAGE_PREFIX, searchCursor) + + if (dataStart === -1) { + break + } + + const dataUrl = readDataImageUrl(text, dataStart) + + if (!dataUrl) { + searchCursor = dataStart + DATA_IMAGE_PREFIX.length + + continue + } + + pieces.push(text.slice(appendCursor, dataStart)) + images.push(dataUrl.url) + appendCursor = dataUrl.end + searchCursor = dataUrl.end + } + + if (!images.length) { + return { cleanedText: text, images: [] } + } + + pieces.push(text.slice(appendCursor)) + + return { + cleanedText: pieces + .join('') + .replace(/[ \t]+\n/g, '\n') + .replace(/\n{3,}/g, '\n\n') + .trim(), + images + } +} + +// lib/render-weight.ts :: payloadCharacters — walks every string in a message's +// content, including the data URL riding in attachmentRefs. +const RENDER_WEIGHT_CHARS = 512 +const MAX_MEASURED_MESSAGE_CHARS = 300 * RENDER_WEIGHT_CHARS +const NON_RENDERED_CONTENT_FIELDS = new Set(['id', 'role', 'toolCallId', 'toolName', 'type']) + +function payloadCharacters(roots, budget) { + const seen = new WeakSet() + const pending = [...roots] + let characters = 0 + + while (pending.length > 0 && characters < budget) { + const value = pending.pop() + + if (typeof value === 'string') { + characters += Math.min(value.length, budget - characters) + + continue + } + + if (!value || typeof value !== 'object' || seen.has(value)) { + continue + } + + seen.add(value) + + if (Array.isArray(value)) { + for (const nested of value) { + pending.push(nested) + } + + continue + } + + for (const [key, nested] of Object.entries(value)) { + if (!NON_RENDERED_CONTENT_FIELDS.has(key)) { + pending.push(nested) + } + } + } + + return characters +} + +// store/composer.ts :: cloneDraft — every composer draft stash copies each +// attachment object; previewUrl (the data URL) rides along by reference, but +// the surrounding string ops on the draft still run. +const cloneDraft = draft => ({ + attachments: draft.attachments.map(a => ({ ...a })), + text: draft.text +}) + +// --- run ------------------------------------------------------------------- + +const results = [] + +for (const kb of SIZES_KB) { + const path = makeImage(kb) + const rows = {} + const record = (stage, ms) => { + ;(rows[stage] ??= []).push(ms) + } + + let dataUrl = '' + let base64 = '' + let frame = '' + let bubbleText = '' + + for (let round = 0; round < ROUNDS; round += 1) { + // 1. preview read (attachImagePath → attachmentPreviewDataUrl) + const preview = time(() => readFileDataUrl(path)) + record('preview_read_dataurl', preview.ms) + dataUrl = preview.out + + // 2. submit-time SECOND read of the same file (readImageForRemoteAttach) + const attachRead = time(() => readFileDataUrl(path)) + record('attach_read_dataurl', attachRead.ms) + + // 3. strip the data: prefix + const strip = time(() => base64FromDataUrl(attachRead.out)) + record('base64_from_dataurl', strip.ms) + base64 = strip.out + + // 4. JSON-RPC frame for image.attach_bytes + const encode = time(() => encodeRpcFrame(base64, 'img.png')) + record('rpc_frame_encode', encode.ms) + frame = encode.out + + // 5. the optimistic bubble carries the data URL as its attachmentRef + bubbleText = dataUrl + const extract = time(() => extractEmbeddedImages(bubbleText)) + record('extract_embedded_images', extract.ms) + + // 6. render-weight walk over the message holding that ref + const content = [{ type: 'text', text: 'what is this' }, { attachmentRefs: [dataUrl] }] + const weigh = time(() => payloadCharacters(content, MAX_MEASURED_MESSAGE_CHARS)) + record('render_weight_walk', weigh.ms) + + // 7. draft stash clone with the attachment held + const draft = { attachments: [{ id: 'a', kind: 'image', label: 'i', previewUrl: dataUrl, path }], text: 'hi' } + const clone = time(() => cloneDraft(draft)) + record('draft_clone', clone.ms) + } + + results.push({ + kb, + fileBytes: readFileSync(path).length, + dataUrlChars: dataUrl.length, + rpcFrameChars: frame.length, + rows + }) +} + +for (const r of results) { + console.log(`\n=== ${r.kb} KB image (${r.fileBytes} bytes on disk) ===`) + console.log( + `data URL: ${r.dataUrlChars.toLocaleString()} chars RPC frame: ${r.rpcFrameChars.toLocaleString()} chars ` + + `(${(r.rpcFrameChars / r.fileBytes).toFixed(2)}x the file)` + ) + console.log('') + console.log('stage mean p50 p95 max') + + let total = 0 + + for (const [stage, samples] of Object.entries(r.rows)) { + const s = stat(samples) + total += s.mean + console.log( + `${stage.padEnd(26)} ${s.mean.toFixed(2).padStart(7)} ${s.p50.toFixed(2).padStart(7)} ` + + `${s.p95.toFixed(2).padStart(7)} ${s.max.toFixed(2).padStart(7)} ms` + ) + } + + console.log(`${'TOTAL (mean)'.padEnd(26)} ${total.toFixed(2).padStart(7)} ms`) +}