|
| 1 | +#!/usr/bin/env bun |
| 2 | +/** |
| 3 | + * Backend latency of native task pane actions, against REAL hosts (seq 1382). |
| 4 | + * |
| 5 | + * Answers one question: how long does the server take to answer a pane action, by |
| 6 | + * pane count. It is the machine half of click-to-settled; the renderer half is |
| 7 | + * covered deterministically by `PaneActionPropagation.test.tsx`, because a |
| 8 | + * wall-clock assertion on a shared dev machine is noise, not a gate. |
| 9 | + * |
| 10 | + * Run: `bun run measure:native-pane-latency` (writes JSON to stdout, logs to stderr). |
| 11 | + * Everything lands in a tmpdir, so `~/.dev3.0/` is never touched. |
| 12 | + */ |
| 13 | + |
| 14 | +import { mkdirSync, mkdtempSync, rmSync } from "node:fs"; |
| 15 | +import { tmpdir } from "node:os"; |
| 16 | +import { join } from "node:path"; |
| 17 | +import { NATIVE_MULTIPANE_DIR_ENV } from "../src/bun/native-terminal-multipane/paths"; |
| 18 | + |
| 19 | +/** Fixed id so repeated runs address the same deterministic session id. */ |
| 20 | +const TASK_ID = "00000000-0000-4000-8000-00000013820f"; |
| 21 | +const PANE_COUNTS = [1, 2, 4, 6]; |
| 22 | +const REPS = 5; |
| 23 | + |
| 24 | +interface Stats { n: number; min: number; p50: number; p95: number; max: number } |
| 25 | + |
| 26 | +function stats(samples: number[]): Stats { |
| 27 | + const sorted = [...samples].sort((a, b) => a - b); |
| 28 | + const at = (q: number) => sorted[Math.min(sorted.length - 1, Math.floor(sorted.length * q))]!; |
| 29 | + const round = (value: number) => Math.round(value * 10) / 10; |
| 30 | + return { |
| 31 | + n: sorted.length, |
| 32 | + min: round(sorted[0]!), |
| 33 | + p50: round(at(0.5)), |
| 34 | + p95: round(at(0.95)), |
| 35 | + max: round(sorted[sorted.length - 1]!), |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +async function main(): Promise<void> { |
| 40 | + const root = mkdtempSync(join(tmpdir(), "dev3-native-pane-latency-")); |
| 41 | + const work = join(root, "work"); |
| 42 | + mkdirSync(work, { recursive: true }); |
| 43 | + process.env.DEV3_NATIVE_SESSIONS_DIR = join(root, "native-sessions"); |
| 44 | + process.env[NATIVE_MULTIPANE_DIR_ENV] = join(root, "native-multipane"); |
| 45 | + process.env.DEV3_NATIVE_HOST_IMAGES_DIR = join(root, "host-images"); |
| 46 | + process.env.DEV3_LOG_DIR = join(root, "logs"); |
| 47 | + |
| 48 | + // Imported after the redirects: the path modules read these on first use. |
| 49 | + const panes = await import("../src/bun/native-task-panes"); |
| 50 | + const { defaultNativeShellLaunchSpec } = await import("../src/bun/native-terminal-registry/shell-launch"); |
| 51 | + const { restoreSplitTree } = await import("../src/shared/split-tree"); |
| 52 | + const { applySplitLayout, nextSplitLayoutPreset, SPLIT_LAYOUT_PRESETS } = |
| 53 | + await import("../src/shared/split-tree-layouts"); |
| 54 | + |
| 55 | + const defaults = defaultNativeShellLaunchSpec({ platform: process.platform, cwd: work, env: process.env }); |
| 56 | + const launch = { executable: defaults.executable, argv: [...defaults.argv] }; |
| 57 | + |
| 58 | + const byPaneCount: Record<string, unknown> = {}; |
| 59 | + const startedAt = performance.now(); |
| 60 | + await panes.startNativeTaskPanes({ taskId: TASK_ID, cwd: work, env: {}, launch, cols: 120, rows: 40 }); |
| 61 | + const startMs = Math.round((performance.now() - startedAt) * 10) / 10; |
| 62 | + |
| 63 | + try { |
| 64 | + for (const target of PANE_COUNTS) { |
| 65 | + for (;;) { |
| 66 | + const state = await panes.nativeTaskPanesState(TASK_ID); |
| 67 | + if (!state || state.panes.length >= target) break; |
| 68 | + const from = state.panes[state.panes.length - 1]!.paneId; |
| 69 | + await panes.splitNativeTaskPane(TASK_ID, from, "horizontal", { cwd: work, env: {}, launch }); |
| 70 | + } |
| 71 | + |
| 72 | + const readState: number[] = []; |
| 73 | + const layoutPreset: number[] = []; |
| 74 | + const layoutCycle: number[] = []; |
| 75 | + for (let rep = 0; rep < REPS; rep++) { |
| 76 | + let mark = performance.now(); |
| 77 | + const state = await panes.nativeTaskPanesState(TASK_ID); |
| 78 | + readState.push(performance.now() - mark); |
| 79 | + const tree = restoreSplitTree(state!.layout)!; |
| 80 | + if (state!.panes.length < 2) continue; |
| 81 | + |
| 82 | + mark = performance.now(); |
| 83 | + await panes.setNativeTaskPaneLayout(TASK_ID, applySplitLayout(tree, "even-horizontal")); |
| 84 | + layoutPreset.push(performance.now() - mark); |
| 85 | + |
| 86 | + const next = nextSplitLayoutPreset(SPLIT_LAYOUT_PRESETS[rep % SPLIT_LAYOUT_PRESETS.length]!); |
| 87 | + mark = performance.now(); |
| 88 | + await panes.setNativeTaskPaneLayout(TASK_ID, applySplitLayout(tree, next)); |
| 89 | + layoutCycle.push(performance.now() - mark); |
| 90 | + } |
| 91 | + |
| 92 | + // A split adds a pane, so measure it and close the extra one back off. |
| 93 | + const splitSamples: number[] = []; |
| 94 | + for (let rep = 0; rep < REPS; rep++) { |
| 95 | + const before = (await panes.nativeTaskPanesState(TASK_ID))!; |
| 96 | + const mark = performance.now(); |
| 97 | + const created = await panes.splitNativeTaskPane(TASK_ID, before.panes[0]!.paneId, "vertical", { |
| 98 | + cwd: work, |
| 99 | + env: {}, |
| 100 | + launch, |
| 101 | + }); |
| 102 | + splitSamples.push(performance.now() - mark); |
| 103 | + await panes.closeNativeTaskPane(TASK_ID, created.paneId); |
| 104 | + } |
| 105 | + |
| 106 | + byPaneCount[String(target)] = { |
| 107 | + readState: stats(readState), |
| 108 | + layoutPreset: layoutPreset.length ? stats(layoutPreset) : null, |
| 109 | + layoutCycle: layoutCycle.length ? stats(layoutCycle) : null, |
| 110 | + split: stats(splitSamples), |
| 111 | + }; |
| 112 | + console.error(` measured panes=${target}`); |
| 113 | + } |
| 114 | + |
| 115 | + console.log(JSON.stringify({ platform: process.platform, bun: Bun.version, startMs, byPaneCount }, null, 2)); |
| 116 | + } finally { |
| 117 | + await panes.stopNativeTaskPanes(TASK_ID).catch(() => {}); |
| 118 | + rmSync(root, { recursive: true, force: true }); |
| 119 | + } |
| 120 | + // The registry holds host sockets open; nothing else keeps this process useful. |
| 121 | + process.exit(0); |
| 122 | +} |
| 123 | + |
| 124 | +main().catch((err) => { |
| 125 | + console.error(err); |
| 126 | + process.exit(1); |
| 127 | +}); |
0 commit comments