diff --git a/packages/core-wasm/tests/measure.test.ts b/packages/core-wasm/tests/measure.test.ts new file mode 100644 index 00000000..f49cfc65 --- /dev/null +++ b/packages/core-wasm/tests/measure.test.ts @@ -0,0 +1,123 @@ +/** + * R2 measurements (owner brief ยง6): what the shipped module costs on the application's + * paths, printed for docs/development/RUST.md. Loose budgets only catch a regression by an + * order of magnitude; the printed numbers are the record. + */ +import { readFileSync } from 'node:fs' +import { fileURLToPath } from 'node:url' + +import { CoreRuntime, type GlueModule } from '../src/index' +import { nodeCoreSource } from '../src/node' +import { + AnatomicalSheetDocumentSchema, + buildSheetIndex, + readArchiveDocument, + ReportMigrationMapSchema, +} from './n-1/domain' + +const ROOT = '../../../' +const SEED = `${ROOT}supabase/seed/anatomical-sheets/equine/draft/` +const read = (path: string) => + readFileSync(fileURLToPath(new URL(path, import.meta.url)), 'utf8') + +const bundle = JSON.stringify({ + status: 'draft', + ref: { + sheet_id: 'sheet', + version_id: null, + document_sha256: '0'.repeat(64), + schema_version: 2, + species_id: 'equine', + }, + document: JSON.parse(read(`${SEED}document.json`)), + migrationMap: JSON.parse(read(`${SEED}report-migration-map.json`)), + assets: {}, + assetHashes: {}, +}) +const envelope = read( + `${ROOT}apps/app/tests/fixtures/goldens/r2/envelope.demo.fr.nologos.json`, +) + +/** Median wall time of `runs` calls, in milliseconds. */ +function median(runs: number, fn: () => void): number { + const times: number[] = [] + for (let i = 0; i < runs; i += 1) { + const started = performance.now() + fn() + times.push(performance.now() - started) + } + times.sort((a, b) => a - b) + return times[Math.floor(times.length / 2)]! +} + +describe('R2 measurements (shipped module, Node)', () => { + it('loads, indexes the sheet and runs the main operations within budget', async () => { + let memory: WebAssembly.Memory | undefined + const source = nodeCoreSource('pkg') + const runtime = new CoreRuntime({ + compile: source.compile, + importGlue: async (n) => { + const glue = await source.importGlue(n) + return { + ...glue, + default: async (input: { module_or_path: WebAssembly.Module }) => { + const exports = (await glue.default(input)) as { memory?: WebAssembly.Memory } + memory = exports.memory + return exports + }, + } as GlueModule + }, + }) + let started = performance.now() + expect(await runtime.ready()).toBe(true) + const loadMs = performance.now() - started + + started = performance.now() + const sheet = runtime.sheet(bundle) + const index = sheet.call<{ document: unknown }>('index_data', '') + const indexMs = performance.now() - started + expect(index.ok).toBe(true) + + const report = JSON.parse(envelope).report + const observation = report.observations[0] + const click = median(50, () => { + sheet.call('zone.click', JSON.stringify({ observation, annotationId: 'c7-t1' })) + }) + const restore = median(20, () => { + sheet.call('draft.restore', JSON.stringify({ draft: JSON.stringify(report), archive: null })) + }) + const importMs = median(10, () => { + sheet.call('archive.import', envelope) + }) + const snapshot = median(10, () => { + sheet.call('archive.snapshot', JSON.stringify({ report })) + }) + const memoryMiB = (memory?.buffer.byteLength ?? 0) / 1024 / 1024 + // The TypeScript it replaces, on the same inputs (the frozen N-1 copy of packages/domain). + const parsed = JSON.parse(bundle) + const tsIndex = median(5, () => { + buildSheetIndex({ + ref: { documentSha256: '0'.repeat(64) }, + document: AnatomicalSheetDocumentSchema.parse(parsed.document), + assets: {}, + }) + }) + const map = ReportMigrationMapSchema.parse(parsed.migrationMap) + const tsImport = median(10, () => { + readArchiveDocument(JSON.parse(envelope), map) + }) + console.info( + `[core-wasm] TypeScript on the same inputs: document parse + index ${tsIndex.toFixed(1)} ms, ` + + `readArchiveDocument (demo envelope, without rebase) ${tsImport.toFixed(2)} ms`, + ) + console.info( + `[core-wasm] load ${loadMs.toFixed(1)} ms, sheet handle + index data ${indexMs.toFixed(1)} ms, ` + + `zone.click ${click.toFixed(3)} ms, draft.restore ${restore.toFixed(2)} ms, ` + + `archive.import (demo envelope, ${(envelope.length / 1024).toFixed(0)} KiB) ${importMs.toFixed(2)} ms, ` + + `archive.snapshot ${snapshot.toFixed(2)} ms, linear memory ${memoryMiB.toFixed(1)} MiB`, + ) + expect(click).toBeLessThan(5) + expect(importMs).toBeLessThan(200) + expect(indexMs).toBeLessThan(500) + }) +})