|
| 1 | +import type { SessionMessageInfo } from "@opencode-ai/client/promise" |
| 2 | +import { benchmark, expect } from "../benchmark" |
| 3 | +import { mockOpenCodeServer } from "../../utils/mock-server" |
| 4 | +import { fixture } from "../timeline/session-timeline-stress.fixture" |
| 5 | +import { installStressSessionTabs, installTimelineSettings, stressSessionHref } from "../timeline/timeline-test-helpers" |
| 6 | +import { completedAnswer } from "../../../../session-ui/performance/markdown-lifetime/answer" |
| 7 | +import { installMarkdownGate } from "./probe" |
| 8 | + |
| 9 | +for (const size of ["typical", "large"]) { |
| 10 | + benchmark(`timeline preload disposal: ${size}`, async ({ page, report }) => { |
| 11 | + const answer = completedAnswer(size === "typical" ? 2 : 36) |
| 12 | + const errors: string[] = [] |
| 13 | + page.on("pageerror", (error) => errors.push(error.message)) |
| 14 | + const messages: Record<string, SessionMessageInfo[]> = Object.fromEntries( |
| 15 | + [fixture.sourceID, fixture.targetID].map((id) => [ |
| 16 | + id, |
| 17 | + [ |
| 18 | + { |
| 19 | + id: `msg_1_${id}_user`, |
| 20 | + type: "user", |
| 21 | + time: { created: 1700000000000 }, |
| 22 | + text: "Review the recovery boundary.", |
| 23 | + }, |
| 24 | + { |
| 25 | + id: `msg_2_${id}_assistant`, |
| 26 | + type: "assistant", |
| 27 | + time: { created: 1700000001000, completed: 1700000008000 }, |
| 28 | + model: { id: "claude-opus-4-6", providerID: "opencode" }, |
| 29 | + agent: "build", |
| 30 | + cost: 0.01, |
| 31 | + tokens: { input: 100, output: 200, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 32 | + finish: "stop", |
| 33 | + content: [ |
| 34 | + { |
| 35 | + type: "text", |
| 36 | + text: |
| 37 | + id === fixture.targetID |
| 38 | + ? answer |
| 39 | + : "## Current destination\n\nThe selected session is ready.\n\n```typescript\nconst current = { ready: true }\n```", |
| 40 | + }, |
| 41 | + ], |
| 42 | + }, |
| 43 | + ] satisfies SessionMessageInfo[], |
| 44 | + ]), |
| 45 | + ) |
| 46 | + await mockOpenCodeServer(page, { |
| 47 | + sessions: fixture.sessions.filter((session) => session.id !== fixture.childID), |
| 48 | + provider: fixture.provider, |
| 49 | + directory: fixture.directory, |
| 50 | + project: fixture.project, |
| 51 | + pageMessages: (id) => ({ items: messages[id] ?? [] }), |
| 52 | + }) |
| 53 | + await installTimelineSettings(page) |
| 54 | + await installStressSessionTabs(page) |
| 55 | + const targetPart = `msg_2_${fixture.targetID}_assistant:text:0` |
| 56 | + const sourcePart = `msg_2_${fixture.sourceID}_assistant:text:0` |
| 57 | + await installMarkdownGate(page, { answer, targetPart, sourcePart, href: stressSessionHref(fixture.sourceID) }) |
| 58 | + const prefetched = page.waitForResponse((response) => |
| 59 | + new URL(response.url()).pathname.endsWith(`/session/${fixture.targetID}/message`), |
| 60 | + ) |
| 61 | + await page.goto(stressSessionHref(fixture.sourceID)) |
| 62 | + await prefetched |
| 63 | + const source = page.locator(`[data-timeline-part-id="${sourcePart}"] [data-component="markdown"]`) |
| 64 | + await expect(source).toHaveAttribute("data-markdown-ready", "") |
| 65 | + await page.locator(`[data-slot="titlebar-tabs"] a[href="${stressSessionHref(fixture.targetID)}"]`).click() |
| 66 | + await page.waitForFunction(() => Reflect.get(window, "markdownGate").held) |
| 67 | + await expect(page.locator(`[data-timeline-part-id="${targetPart}"]`)).toBeAttached() |
| 68 | + const cdp = await page.context().newCDPSession(page) |
| 69 | + await cdp.send("Performance.enable") |
| 70 | + const before = await cdp.send("Performance.getMetrics") |
| 71 | + await page.evaluate(() => Reflect.get(window, "markdownGate").arm()) |
| 72 | + await page.locator(`[data-slot="titlebar-tabs"] a[href="${stressSessionHref(fixture.sourceID)}"]`).click() |
| 73 | + await expect(source).toHaveAttribute("data-markdown-ready", "") |
| 74 | + await expect(source.getByRole("heading", { name: "Current destination" })).toBeVisible() |
| 75 | + await expect(page.locator(`[data-timeline-part-id="${targetPart}"]`)).toHaveCount(0) |
| 76 | + await page.waitForFunction(() => Reflect.get(window, "markdownGate").settled > 0) |
| 77 | + const after = await cdp.send("Performance.getMetrics") |
| 78 | + const stats = await page.evaluate(() => { |
| 79 | + const value = Reflect.get(window, "markdownGate") |
| 80 | + return { |
| 81 | + admitted: value.admitted, |
| 82 | + responses: value.responses, |
| 83 | + started: value.started, |
| 84 | + ready: value.ready, |
| 85 | + released: value.released, |
| 86 | + settled: value.settled, |
| 87 | + sanitizeCalls: value.sanitizeCalls, |
| 88 | + sanitizeChars: value.sanitizeChars, |
| 89 | + } |
| 90 | + }) |
| 91 | + expect(stats.admitted).toBe(1) |
| 92 | + expect(stats.responses).toBe(1) |
| 93 | + expect(stats.ready).toBeGreaterThan(stats.started) |
| 94 | + expect(stats.settled).toBeGreaterThan(stats.released) |
| 95 | + expect(errors).toEqual([]) |
| 96 | + if (process.env.MARKDOWN_ASSERT_DISPOSAL === "1") expect(stats.sanitizeCalls).toBe(0) |
| 97 | + const value = (data: typeof after, name: string) => data.metrics.find((item) => item.name === name)!.value |
| 98 | + const retained = process.env.MARKDOWN_RETAINED === "1" |
| 99 | + if (retained) await cdp.send("HeapProfiler.collectGarbage") |
| 100 | + report( |
| 101 | + { |
| 102 | + ...stats, |
| 103 | + destinationReadyMs: stats.ready - stats.started, |
| 104 | + releasedSettledMs: stats.settled - stats.released, |
| 105 | + taskMs: (value(after, "TaskDuration") - value(before, "TaskDuration")) * 1000, |
| 106 | + scriptMs: (value(after, "ScriptDuration") - value(before, "ScriptDuration")) * 1000, |
| 107 | + usedHeapBytes: (await cdp.send("Runtime.getHeapUsage")).usedSize, |
| 108 | + }, |
| 109 | + { |
| 110 | + size, |
| 111 | + retained, |
| 112 | + answerBytes: Buffer.byteLength(answer), |
| 113 | + messagesPerSession: 2, |
| 114 | + partsPerAnswer: 1, |
| 115 | + fences: size === "typical" ? 2 : 36, |
| 116 | + browser: page.context().browser()!.version(), |
| 117 | + transport: "playwright-route", |
| 118 | + build: process.env.MARKDOWN_APP_BUILD_DIR, |
| 119 | + }, |
| 120 | + ) |
| 121 | + if (process.env.MARKDOWN_SCREENSHOT) |
| 122 | + await page.screenshot({ path: `${process.env.MARKDOWN_SCREENSHOT}/timeline-${size}.png` }) |
| 123 | + await cdp.detach() |
| 124 | + }) |
| 125 | +} |
0 commit comments