openpencil/tests/e2e/perf/basic.spec.ts
Danila Poyarkov aa86873dd7
test: typecheck the test suites and fix what that found (#896)
* build: typecheck the test suites

Tests were in no TypeScript program: no tsconfig included tests/** or
packages/*/tests/**, and bun strips types without checking them, so a
fixture could drop a required field and keep passing until something
read it.

@types/bun moves to the root because it was installed per package only,
and #cli-tests/* joins the paths the root config already carries.

* test: fix the type errors the test suites were hiding

Typechecking the tests turned up 1123 errors. Most were ordinary
strictness, but some were real: `NodeChange` bound to Figma's plugin
typings rather than the Kiwi codec in thirteen .fig tests,
materializeInstance was called with six arguments against five so the
blobs and source children were dropped, CanvasKit pixels were written
to a plain object that never reached WASM, and assertions were made
through accessors that do not exist, so they asserted nothing.

Fixtures that had quietly lost a required field now carry it, nullable
results are narrowed through the existing expectDefined helper rather
than assumed, and stand-ins for CanvasKit and the editor go through one
named helper instead of an unexplained cast at each site.

No test was deleted, skipped, or weakened, and no `any`, non-null
assertion, or ts-expect-error was introduced.

* docs: record what typechecking the tests established

Pins the app program's global types with an assertion rather than a
note, since an unpinned types list lets any root @types package decide
which platform src/** is judged against.

The two environment faults that look like code regressions — Vite's
dependency pre-bundle outliving a package rebuild, and heavy .fig
suites failing under load — go to the development docs, where an
explanation belongs.

* fix: align @types/bun and keep node types resolvable when extended

The root manifest declared a newer @types/bun than every package, which
check:monorepo rejects, and pinning the app program's types left them
unresolvable from a config that extends this one out of tree.

* fix: fail the test typecheck when the compiler itself fails

The gate matched diagnostics by substring, so a compiler or config
failure that named no test file printed a pass while having checked
nothing. Diagnostics are now split by whether they name a file: an
unscoped one is the run failing and stops the gate, a test file's is a
finding, and a source file's stays out by design.

Also drops the parameter planComponentConstruction never read, and
makes the inner-shadow verification script exit non-zero when it
renders no image instead of logging and succeeding.

* chore: merge master into tests-typecheck
2026-10-05 12:42:38 +00:00

264 lines
9.5 KiB
TypeScript

import { test, expect } from '@playwright/test'
import { CanvasHelper } from '#tests/helpers/canvas'
const NODE_COUNT = 500
const ITERATIONS = 200
test.describe('Render performance', () => {
let helper: CanvasHelper
test.beforeAll(async ({ browser, baseURL }) => {
const page = await browser.newPage({ baseURL })
helper = new CanvasHelper(page)
await page.goto('/?test&no-chrome&no-rulers')
await helper.waitForInit()
await page.evaluate((count: number) => {
const store = window.openPencil?.getStore?.()
if (!store) throw new Error('OpenPencil store not initialized')
const arr = new Uint8Array(count * 3)
crypto.getRandomValues(arr)
const cols = Math.ceil(Math.sqrt(count))
for (let i = 0; i < count; i++) {
const mod = i % 10
const isVector = mod === 0
const isEllipse = mod === 5
let type: 'VECTOR' | 'ELLIPSE' | 'RECTANGLE' = 'RECTANGLE'
if (isVector) type = 'VECTOR'
else if (isEllipse) type = 'ELLIPSE'
const props: Record<string, unknown> = {
x: (i % cols) * 60,
y: Math.floor(i / cols) * 60,
width: 50,
height: 50,
cornerRadius: type === 'RECTANGLE' ? 8 : 0,
fills: [
{
type: 'SOLID',
color: {
r: (arr[i * 3] ?? 0) / 255,
g: (arr[i * 3 + 1] ?? 0) / 255,
b: (arr[i * 3 + 2] ?? 0) / 255,
a: 1
},
visible: true,
opacity: 1
}
],
strokes: [
{
type: 'SOLID',
color: { r: 0, g: 0, b: 0, a: 1 },
visible: true,
opacity: 1,
weight: 1
}
]
}
if (isVector) {
props.vectorNetwork = {
vertices: [
{ x: 0, y: 25 },
{ x: 25, y: 0 },
{ x: 50, y: 25 },
{ x: 25, y: 50 }
],
segments: [
{ start: 0, end: 1, tangentStart: { x: 0, y: -15 }, tangentEnd: { x: -15, y: 0 } },
{ start: 1, end: 2, tangentStart: { x: 15, y: 0 }, tangentEnd: { x: 0, y: -15 } },
{ start: 2, end: 3, tangentStart: { x: 0, y: 15 }, tangentEnd: { x: 15, y: 0 } },
{ start: 3, end: 0, tangentStart: { x: -15, y: 0 }, tangentEnd: { x: 0, y: 15 } }
],
regions: [{ loops: [[0, 1, 2, 3]], windingRule: 'NONZERO' as const }]
}
}
store.graph.createNode(type, store.state.currentPageId, props)
}
store.requestRender()
}, NODE_COUNT)
await helper.waitForRender()
})
test.afterAll(async () => {
await helper.page.close()
})
test('benchmark: synchronous render throughput', async () => {
const results = await helper.page.evaluate((iterations: number) => {
const store = window.openPencil?.getStore?.()
if (!store) throw new Error('OpenPencil store not initialized')
const renderer = store.renderer
if (!renderer) throw new Error('OpenPencil renderer not initialized')
const setupRenderer = () => {
renderer.dpr = window.devicePixelRatio || 1
renderer.panX = store.state.panX
renderer.panY = store.state.panY
renderer.zoom = store.state.zoom
renderer.viewportWidth = 1280
renderer.viewportHeight = 800
renderer.showRulers = false
renderer.pageColor = store.state.pageColor
renderer.pageId = store.state.currentPageId
}
// Warm up
setupRenderer()
renderer.render(store.graph, store.state.selectedIds, {}, store.state.sceneVersion)
renderer.render(store.graph, store.state.selectedIds, {}, store.state.sceneVersion)
// Benchmark 1: Pan (repaint — picture cache hit)
setupRenderer()
const panStart = performance.now()
for (let i = 0; i < iterations; i++) {
renderer.panX += 2
renderer.panY += 1
renderer.render(store.graph, store.state.selectedIds, {}, store.state.sceneVersion)
}
const panMs = performance.now() - panStart
// Benchmark 2: Scene change (cache miss every frame)
const pageNode = store.graph.getNode(store.state.currentPageId)
if (!pageNode) throw new Error(`Page ${store.state.currentPageId} not found`)
const nodes = [...pageNode.childIds]
setupRenderer()
const sceneStart = performance.now()
for (let i = 0; i < iterations; i++) {
const nodeId = nodes[i % nodes.length]
if (!nodeId) throw new Error('Benchmark node id not found')
const node = store.graph.getNode(nodeId)
if (node) store.graph.updateNode(node.id, { x: node.x + 0.1 })
store.state.sceneVersion++
renderer.render(store.graph, store.state.selectedIds, {}, store.state.sceneVersion)
}
const sceneMs = performance.now() - sceneStart
// Benchmark 3: Hover (volatile overlay, no caching)
setupRenderer()
const hoverStart = performance.now()
for (let i = 0; i < iterations; i++) {
renderer.render(
store.graph,
store.state.selectedIds,
{ hoveredNodeId: nodes[i % nodes.length] },
store.state.sceneVersion
)
}
const hoverMs = performance.now() - hoverStart
return {
pan: {
ms: Math.round(panMs * 100) / 100,
avg: Math.round((panMs / iterations) * 100) / 100
},
scene: {
ms: Math.round(sceneMs * 100) / 100,
avg: Math.round((sceneMs / iterations) * 100) / 100
},
hover: {
ms: Math.round(hoverMs * 100) / 100,
avg: Math.round((hoverMs / iterations) * 100) / 100
}
}
}, ITERATIONS)
console.log(`\n═══ RENDER BENCHMARK (${NODE_COUNT} nodes, ${ITERATIONS} iterations) ═══`)
console.log(` Pan (cache hit): ${results.pan.avg}ms/frame (${results.pan.ms}ms total)`)
console.log(` Scene change: ${results.scene.avg}ms/frame (${results.scene.ms}ms total)`)
console.log(` Hover (no cache): ${results.hover.avg}ms/frame (${results.hover.ms}ms total)`)
console.log(` Speedup (pan vs scene): ${(results.scene.avg / results.pan.avg).toFixed(1)}x`)
console.log(`═══════════════════════════════════════════════════════\n`)
expect(results.pan.avg).toBeLessThan(50)
})
test('benchmark: shadow rendering throughput', async () => {
const SHADOW_NODES = 50
const SHADOW_ITERS = 100
const results = await helper.page.evaluate(
({ count, iterations }) => {
const store = window.openPencil?.getStore?.()
if (!store) throw new Error('OpenPencil store not initialized')
const renderer = store.renderer
if (!renderer) throw new Error('OpenPencil renderer not initialized')
const graph = store.graph
const pageId = store.state.currentPageId
const shadowIds: string[] = []
const cols = Math.ceil(Math.sqrt(count))
for (let i = 0; i < count; i++) {
const id = graph.createNode('RECTANGLE', pageId, {
x: (i % cols) * 80,
y: Math.floor(i / cols) * 80 + 2000,
width: 60,
height: 60,
cornerRadius: 8,
fills: [
{ type: 'SOLID', color: { r: 1, g: 1, b: 1, a: 1 }, visible: true, opacity: 1 }
],
effects: [
{
type: 'DROP_SHADOW',
color: { r: 0, g: 0, b: 0, a: 0.2 },
offset: { x: 0, y: 4 },
radius: 12,
spread: 0,
visible: true
}
]
})
shadowIds.push(id.id)
}
const setupRenderer = () => {
renderer.dpr = window.devicePixelRatio || 1
renderer.panX = 0
renderer.panY = -2000
renderer.zoom = 1
renderer.viewportWidth = 1280
renderer.viewportHeight = 800
renderer.showRulers = false
renderer.pageColor = store.state.pageColor
renderer.pageId = pageId
}
setupRenderer()
renderer.render(graph, store.state.selectedIds, {}, store.state.sceneVersion)
setupRenderer()
const sceneStart = performance.now()
for (let i = 0; i < iterations; i++) {
const nodeId = shadowIds[i % shadowIds.length]
if (!nodeId) throw new Error('Shadow benchmark node id not found')
const node = graph.getNode(nodeId)
if (node) graph.updateNode(node.id, { x: node.x + 0.1 })
store.state.sceneVersion++
renderer.render(graph, store.state.selectedIds, {}, store.state.sceneVersion)
}
const sceneMs = performance.now() - sceneStart
for (const id of shadowIds) graph.deleteNode(id)
return {
scene: {
ms: Math.round(sceneMs * 100) / 100,
avg: Math.round((sceneMs / iterations) * 100) / 100
}
}
},
{ count: SHADOW_NODES, iterations: SHADOW_ITERS }
)
console.log(
`\n═══ SHADOW BENCHMARK (${SHADOW_NODES} shadow nodes, ${SHADOW_ITERS} iterations) ═══`
)
console.log(` Scene change: ${results.scene.avg}ms/frame (${results.scene.ms}ms total)`)
console.log(`═══════════════════════════════════════════════════════\n`)
expect(results.scene.avg).toBeLessThan(50)
})
})