Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
334 changes: 120 additions & 214 deletions e2e/graph-frame-timeline.spec.ts
Original file line number Diff line number Diff line change
@@ -1,219 +1,93 @@
import { test, expect, type Page } from '@playwright/test'
import { navClick } from './helpers/navigation'
import { seedStore, type PersistedStudioState } from './helpers/store'
import { seedStore } from './helpers/store'
import {
MIN_SAMPLED_FRAMES,
PAN_P50_CEILING_MS,
PAN_P95_CEILING_MS,
SETTLE_MS,
ZOOM_P50_CEILING_MS,
ZOOM_P95_CEILING_MS,
buildSeededEntities,
dragCanvas,
recordEnvironment,
sampleHeap,
startFrameSampling,
stopFrameSampling,
summarize,
waitForPlacement,
zoomCanvas,
} from './helpers/graph-perf'

/**
* Graph frame-timeline guard (plans/198 "Known gap", plans/207).
* Graph frame-timeline guard (plans/198 "Known gap", plans/207, plans/211).
*
* #1053's acceptance target was 30 fps for pan and zoom on a 5,000-node corpus.
* Nothing asserted it: the layout benchmarks bound a placement pass and the
* culling tests bound how many nodes mount, but neither reads a frame timeline,
* so "the graph stays interactive at 5k" was a claim with no check behind it.
* Seeds a large library through the shared store fixture, waits for progressive
* placement, then records the real interval between animation frames while a
* drag runs and, separately, while a wheel zoom runs. The numbers are printed
* with the browser/OS/hardware context they were measured under; the ceilings
* are regression floors, not product targets (see plans/perf-baselines.md).
*
* This spec reads the frames the page actually produced. A `requestAnimation
* Frame` sampler records the interval between frames while a real drag and a
* real wheel-zoom run; when the main thread cannot keep up, those callbacks are
* delayed and the delay appears as a longer interval. Pan and zoom are sampled
* separately because they stress different code — pointer-capture pan versus
* focal zoom over a re-laid-out viewBox.
*
* **The 30 fps target is measured, not asserted.** Under headless software
* rendering — the same class of runner CI uses, where SVG compositing gets no
* GPU — the 2026-11-13 baseline is pan p50 50 ms / zoom p50 67 ms (17–20 fps),
* so asserting 1000/30 ms here would ship a check that is red on every machine
* in this project. The assertions are per-phase p50/p95 regression floors with
* roughly 2× headroom over that baseline; they exist to catch a bounded-render
* regression, and they do: disabling culling raises pan p50 to 216.7 ms and
* fails the check. `plans/perf-baselines.md` records both the measurement and
* what a GPU-backed runner would be needed for.
*/

/**
* Entity count from #1053. The seeded library also carries links so the render
* pass has edges to cull and draw, not just nodes.
*/
const NODE_COUNT = 5000

/** Links per node, giving the graph an edge population at library scale. */
const LINKS_PER_NODE = 2

/** The #1053 frame target, reported alongside the measurement for comparison. */
const TARGET_FPS = 30
const TARGET_FRAME_MS = 1000 / TARGET_FPS

/**
* Below this, the sample says nothing: a page that never ran an animation frame
* would otherwise have an empty — and trivially passing — percentile list.
*/
const MIN_SAMPLED_FRAMES = 30

/**
* Regression ceilings, not the product target.
*
* Measured on 2026-11-13 under headless software rendering (the same class of
* runner CI uses, where SVG compositing gets no GPU): the 30 fps target of #1053
* is **not** met in this environment — see `plans/perf-baselines.md`. These
* ceilings sit far enough above the measurement to absorb runner variance and
* far enough below the pre-culling cost (thousands of mounted nodes, seconds per
* frame) to fail if the bounded-render path regresses. A GPU-backed runner is
* what would let this spec assert the 30 fps target itself.
* The default run asserts the 5k case. Set GRAPH_BENCH_SIZES (e.g.
* "1,5,10" for 1k/5k/10k) to sweep every size with the same gestures.
*/
const PAN_P50_CEILING_MS = 120
const PAN_P95_CEILING_MS = 1_500
const ZOOM_P50_CEILING_MS = 120
const ZOOM_P95_CEILING_MS = 1_500

/** Frames the app needs to finish progressive placement before the drag starts. */
const PLACEMENT_MAX_WAIT_MS = 90_000

/** Quiet period after placement, so its frames are never part of the sample. */
const SETTLE_MS = 500

/** Quantiles reported: the typical frame, the slow tail, and the single worst. */
const QUANTILE_MEDIAN = 0.5
const QUANTILE_P95 = 0.95
const QUANTILE_MAX = 1

/** Drag steps per sampling phase — enough pointer moves to cover a real gesture. */
const DRAG_STEPS = 48
/** The seeded size the CI floors are recorded at. */
const DEFAULT_NODE_COUNT = 5000

/** Pixels per drag step; small enough to read as a drag, not a jump. */
const DRAG_STEP_PX = 3

/** Wheel-zoom events per phase; enough that the sample clears `MIN_SAMPLED_FRAMES`. */
const ZOOM_STEPS = 24

/** Wheel delta per event. */
const ZOOM_DELTA_Y = 25

/** Zoom-out events before sampling, so the drag runs against the whole graph. */
const ZOOM_OUT_TO_FIT_STEPS = 0

/** In-page sampler handle; the name is shared between the injected scripts. */
const SAMPLER_KEY = '__dksFrameSampler'

/** Shape of the sampler the page keeps between `page.evaluate` calls. */
interface FrameSampler {
deltas: number[]
handle: number | null
}

/** Start recording the interval between animation frames. */
const startFrameSampling = async (page: Page): Promise<void> => {
await page.evaluate((key) => {
const scope = globalThis as unknown as Record<string, FrameSampler>
const sampler: FrameSampler = { deltas: [], handle: null }
scope[key] = sampler
let last = performance.now()
const tick = (now: number): void => {
sampler.deltas.push(now - last)
last = now
sampler.handle = requestAnimationFrame(tick)
}
sampler.handle = requestAnimationFrame(tick)
}, SAMPLER_KEY)
}

/** Stop recording and return every frame interval the page observed. */
const stopFrameSampling = async (page: Page): Promise<number[]> => {
return page.evaluate((key) => {
const scope = globalThis as unknown as Record<string, FrameSampler>
const sampler = scope[key]
if (sampler === null || sampler === undefined) return []
if (sampler.handle !== null) cancelAnimationFrame(sampler.handle)
delete scope[key]
return sampler.deltas
}, SAMPLER_KEY)
}

/** Distribution summary used both for reporting and for the assertions. */
interface FrameSummary {
frames: number
p50: number
p95: number
worst: number
text: string
}

/** Describe a frame-interval sample. */
const summarize = (deltas: number[]): FrameSummary => {
const p50 = percentile(deltas, QUANTILE_MEDIAN)
const p95 = percentile(deltas, QUANTILE_P95)
const worst = percentile(deltas, QUANTILE_MAX)
return {
frames: deltas.length,
p50,
p95,
worst,
text: `frames=${deltas.length} p50=${p50.toFixed(1)}ms p95=${p95.toFixed(1)}ms worst=${worst.toFixed(1)}ms`,
}
}

/** Sorted percentile of a sample, linearly interpolated between neighbours. */
const percentile = (sample: number[], fraction: number): number => {
if (sample.length === 0) return Number.NaN
const sorted = [...sample].sort((a, b) => a - b)
const position = (sorted.length - 1) * fraction
const lowerIndex = Math.floor(position)
const upperIndex = Math.ceil(position)
const lower = sorted[lowerIndex]
const upper = sorted[upperIndex]
if (lower === undefined || upper === undefined) return Number.NaN
return lower + (upper - lower) * (position - lowerIndex)
/** Node counts to sweep when GRAPH_BENCH_SIZES is set (values in thousands). */
const readSweepSizes = (): number[] => {
const raw = process.env.GRAPH_BENCH_SIZES
if (!raw) return []
return raw.split(',').flatMap((part) => {
const thousands = Number(part.trim())
return Number.isFinite(thousands) && thousands > 0 ? [thousands * 1000] : []
})
}

/** The seeded library: `NODE_COUNT` notes with `LINKS_PER_NODE` outgoing links. */
const buildSeededEntities = (): PersistedStudioState['entities'] =>
Array.from({ length: NODE_COUNT }, (_, index) => ({
id: `frame-${index}`,
name: `Frame node ${index}`,
type: 'note',
description: `Seeded node ${index} for the frame-timeline guard`,
content: '',
tags: ['frame-timeline'],
aliases: [],
createdAt: '2025-01-01T00:00:00Z',
updatedAt: '2025-06-15T00:00:00Z',
links: Array.from({ length: LINKS_PER_NODE }, (_, linkIndex) => ({
targetId: `frame-${(index + linkIndex + 1) % NODE_COUNT}`,
relation: 'relates_to',
})),
}))

/** Wait until progressive placement stops reporting progress. */
const waitForPlacement = async (page: Page): Promise<void> => {
await page.waitForFunction(
() => {
const canvas = document.querySelector('svg[aria-label^="Knowledge graph"]')
if (canvas === null) return false
const status = document.querySelector('[role="status"]')
if (status === null) return true
return !(status.textContent ?? '').includes('Placing nodes')
},
undefined,
{ timeout: PLACEMENT_MAX_WAIT_MS },
const measureFrameTimeline = (nodeCount: number) => async ({ page }: { page: Page }): Promise<void> => {
test.setTimeout(180_000)
const svg = page.locator('svg[aria-label^="Knowledge graph"]')

await seedStore(page, { entities: buildSeededEntities(nodeCount) })
await page.goto('/')
await navClick(page, /graph/i)

await waitForPlacement(page)
await page.waitForTimeout(SETTLE_MS)

const env = await recordEnvironment(page)
const heapLoaded = await sampleHeap(page)

const box = await svg.boundingBox()
expect(box).not.toBeNull()
if (box === null) return

await startFrameSampling(page)
await dragCanvas(page, box)
const panDeltas = await stopFrameSampling(page)
const heapAfterPan = await sampleHeap(page)
const pan = summarize(panDeltas)

await startFrameSampling(page)
await zoomCanvas(page, box)
const zoomDeltas = await stopFrameSampling(page)

const zoom = summarize(zoomDeltas)
// Printed, not merely annotated: the numbers are the deliverable, and a
// failing run must say what the machine actually produced.
console.log(
`[frame-timeline] nodes=${nodeCount} env=${JSON.stringify(env)} ` +
`heapLoaded=${JSON.stringify(heapLoaded)} heapAfterPan=${JSON.stringify(heapAfterPan)} ` +
`pan=${pan.text} zoom=${zoom.text}`,
)
}

/** Drag the canvas horizontally for `DRAG_STEPS` pointer moves. */
const dragCanvas = async (page: Page, box: { x: number; y: number; width: number; height: number }): Promise<void> => {
const centerX = box.x + box.width / 2
const centerY = box.y + box.height / 2
await page.mouse.move(centerX, centerY)
await page.mouse.down()
for (let step = 1; step <= DRAG_STEPS; step += 1) {
const direction = step % 2 === 0 ? 1 : -1
await page.mouse.move(centerX + direction * step * DRAG_STEP_PX, centerY)
}
await page.mouse.up()
}

/** Wheel-zoom in and out at the canvas centre. */
const zoomCanvas = async (page: Page, box: { x: number; y: number; width: number; height: number }): Promise<void> => {
await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2)
for (let step = 0; step < ZOOM_STEPS; step += 1) {
await page.mouse.wheel(0, step % 2 === 0 ? -ZOOM_DELTA_Y : ZOOM_DELTA_Y)
}
expect(pan.frames).toBeGreaterThanOrEqual(MIN_SAMPLED_FRAMES)
expect(zoom.frames).toBeGreaterThanOrEqual(MIN_SAMPLED_FRAMES)
expect(pan.p50).toBeLessThanOrEqual(PAN_P50_CEILING_MS)
expect(zoom.p50).toBeLessThanOrEqual(ZOOM_P50_CEILING_MS)
expect(pan.p95).toBeLessThanOrEqual(PAN_P95_CEILING_MS)
expect(zoom.p95).toBeLessThanOrEqual(ZOOM_P95_CEILING_MS)
}

test.describe('Graph frame timeline at 5k nodes', () => {
Expand All @@ -222,24 +96,20 @@ test.describe('Graph frame timeline at 5k nodes', () => {
test.setTimeout(180_000)
const svg = page.locator('svg[aria-label^="Knowledge graph"]')

await seedStore(page, { entities: buildSeededEntities() })
await seedStore(page, { entities: buildSeededEntities(DEFAULT_NODE_COUNT) })
await page.goto('/')
await navClick(page, /graph/i)

await expect(svg).toBeVisible()
await waitForPlacement(page)
await page.waitForTimeout(SETTLE_MS)

// Zoom out so the drag runs against the whole library rather than the
// focus-mode neighborhood the view opens in at this size.
const env = await recordEnvironment(page)
const heapLoaded = await sampleHeap(page)

const box = await svg.boundingBox()
expect(box).not.toBeNull()
if (box === null) return
await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2)
for (let step = 0; step < ZOOM_OUT_TO_FIT_STEPS; step += 1) {
await page.mouse.wheel(0, ZOOM_DELTA_Y)
}
await page.waitForTimeout(SETTLE_MS)

// Above the label threshold each node draws exactly one circle: the
// decorative core dot is LOD-gated (plans/211). Re-adding it doubles the
Expand All @@ -263,17 +133,18 @@ test.describe('Graph frame timeline at 5k nodes', () => {
await startFrameSampling(page)
await dragCanvas(page, box)
const panDeltas = await stopFrameSampling(page)
const heapAfterPan = await sampleHeap(page)

await startFrameSampling(page)
await zoomCanvas(page, box)
const zoomDeltas = await stopFrameSampling(page)

const pan = summarize(panDeltas)
const zoom = summarize(zoomDeltas)
// Printed, not merely annotated: the numbers are the deliverable, and a
// failing run must say what the machine actually produced.
console.log(
`[frame-timeline] pan=${pan.text} zoom=${zoom.text} target=${TARGET_FRAME_MS.toFixed(1)}ms`,
`[frame-timeline] nodes=${DEFAULT_NODE_COUNT} env=${JSON.stringify(env)} ` +
`heapLoaded=${JSON.stringify(heapLoaded)} heapAfterPan=${JSON.stringify(heapAfterPan)} ` +
`pan=${pan.text} zoom=${zoom.text}`,
)

expect(pan.frames).toBeGreaterThanOrEqual(MIN_SAMPLED_FRAMES)
Expand All @@ -283,4 +154,39 @@ test.describe('Graph frame timeline at 5k nodes', () => {
expect(pan.p95).toBeLessThanOrEqual(PAN_P95_CEILING_MS)
expect(zoom.p95).toBeLessThanOrEqual(ZOOM_P95_CEILING_MS)
})

test('applies a type filter on the 5k corpus and re-renders', async ({ page }) => {
test.setTimeout(180_000)
const svg = page.locator('svg[aria-label^="Knowledge graph"]')

await seedStore(page, { entities: buildSeededEntities(DEFAULT_NODE_COUNT) })
await page.goto('/')
await navClick(page, /graph/i)

await expect(svg).toBeVisible()
await waitForPlacement(page)

await page.getByRole('button', { name: /^queries$/i }).click()
await page.getByRole('checkbox', { name: 'Concept' }).check()

const startedAt = Date.now()
await waitForPlacement(page)
const elapsedMs = Date.now() - startedAt

const label = await svg.getAttribute('aria-label')
const entityCount = Number(/with (\d+) entities/.exec(label ?? '')?.[1] ?? Number.NaN)
expect(Number.isFinite(entityCount)).toBe(true)
expect(entityCount).toBeGreaterThan(0)
expect(entityCount).toBeLessThan(DEFAULT_NODE_COUNT)
console.log(
`[frame-timeline-filter] nodes=${DEFAULT_NODE_COUNT} → ${entityCount} entities in ${elapsedMs}ms`,
)
})
})

test.describe('Graph frame timeline size sweep (GRAPH_BENCH_SIZES)', () => {
const sizes = readSweepSizes()
for (const nodeCount of sizes) {
test(`pan and zoom at ${nodeCount} nodes`, measureFrameTimeline(nodeCount))
}
})
Loading
Loading