242 lines
13 KiB
TypeScript
242 lines
13 KiB
TypeScript
/**
|
|
* V1-2.1 timing at the caps on the real stack (contract §15: "save, submit and
|
|
* publish under 2 s each", measured in V1-2.1, fixed at the V1-2.3
|
|
* checkpoint), after the independent audit (M4).
|
|
*
|
|
* The core's at-caps fixture belongs to V1-2.2, which has not started. This
|
|
* suite builds an equivalent document for what SQL does with one: the
|
|
* structural guards, the 2 MiB size (UTF-8 bytes of the JSON text), the status walk at approval
|
|
* and publication, the group coverage, the drawing checks. It reaches every
|
|
* cap those depend on: 64 views (one main, three with their own drawing, the
|
|
* rest crops), 1 024 annotations, 8 192 finding nodes counting categories
|
|
* (1 024 zones, each a category and a chain of seven nodes: depth 8), four
|
|
* drawings near 2 MiB, labels sized so the document's text (`::text`, the
|
|
* contract's measure, §12.2) is as close to 2 MiB as the variants allow; its
|
|
* binary jsonb, larger, is reported and is not the measure, every status
|
|
* `reviewed`, a verdict of 1 089 groups (general, 64 views, 1 024 zones). The
|
|
* core never parses it (SQL holds no business rule, §12.2), so it says nothing
|
|
* about the verifier's 120 s budget, which V1-2.3 measures.
|
|
*
|
|
* Every call is timed over HTTP as the page would make it (PostgREST, real
|
|
* GoTrue aal2 token). Run through `pnpm db:sandbox test tests/sandbox/sheets-perf.sandbox.ts`.
|
|
*/
|
|
import { performance } from 'node:perf_hooks'
|
|
import { rpc, sql } from './stack'
|
|
import type { SteppedAccount } from './mfa'
|
|
import { drawing, holder, ok, sha256, upload } from './sheet-fixtures'
|
|
|
|
const BUDGET_MS = 2000
|
|
const DOC_CAP = 2_097_152
|
|
const VIEWS = 64
|
|
const ANNOTATIONS = 1024
|
|
const ZONES = 1024
|
|
const CHAIN = 7
|
|
|
|
/** The at-caps document; `variant` changes one text so each save differs. */
|
|
function capsDocument(assets: string[], labelLength: number, variant: number): Record<string, unknown> {
|
|
const text = (base: string) => (base + ' ').padEnd(labelLength, 'x')
|
|
const name = (base: string) => ({ fr: text(`${base} fr`), en: text(`${base} en`) })
|
|
const views = Array.from({ length: VIEWS }, (_, i) =>
|
|
i === 0
|
|
? { id: 'main', kind: 'main', name: name('main'), svg_asset_id: assets[0], view_box: null }
|
|
: i < 4
|
|
? { id: `v${i}`, kind: 'detail', parent_view_id: 'main', name: name(`v${i}`), svg_asset_id: assets[i], view_box: null }
|
|
: { id: `v${i}`, kind: 'detail', parent_view_id: 'main', name: name(`v${i}`), svg_asset_id: null,
|
|
view_box: { min_x: i, min_y: i, width: 100, height: 80 } })
|
|
const node = (zone: number, level: number): Record<string, unknown> => ({
|
|
id: `n${level}`, name: name(`z${zone} n${level}`), review_status: 'reviewed', requires_vet_referral: level === CHAIN,
|
|
reference_ids: level === 1 ? ['r0'] : [],
|
|
...(level < CHAIN ? { children: [node(zone, level + 1)] } : {}),
|
|
})
|
|
return {
|
|
schema_version: 2,
|
|
species_id: 'equine',
|
|
name: name(`caps ${variant}`),
|
|
description: name('caps document'),
|
|
scope_disclaimer: { fr: 'x'.repeat(500), en: 'y'.repeat(480) },
|
|
attribution: { authors: ['Sandbox'], license: null, notes: null },
|
|
vertebral_config: { regions: ['C', 'T', 'L', 'S', 'Cd'].map((code, i) => ({ code, name: name(code), count: 5 + i })) },
|
|
references: Array.from({ length: 64 }, (_, i) => ({ id: `r${i}`, type: 'manual', citation: text(`reference ${i}`), url: null, doi: null })),
|
|
categories: Array.from({ length: 8 }, (_, i) => ({ id: `c${i}`, name: name(`c${i}`), color: '#123456' })),
|
|
views,
|
|
annotations: Array.from({ length: ANNOTATIONS }, (_, i) => ({
|
|
id: `a${i}`, kind: i % 4 === 0 ? 'zone' : 'point', view_id: views[i % VIEWS].id, category_id: `c${i % 8}`,
|
|
laterality: 'unpaired',
|
|
geometry: i % 4 === 0
|
|
? { type: 'polygon', points: [[i, 1], [i + 5, 2], [i + 9, 7], [i + 3, 9]] }
|
|
: { type: 'circle', cx: i, cy: i, r: 5 },
|
|
review_status: 'reviewed', name: name(`a${i}`), finding_zone_id: `z${i % ZONES}`, vertebral_region_code: null,
|
|
})),
|
|
finding_zones: Array.from({ length: ZONES }, (_, z) => ({
|
|
id: `z${z}`, name: name(`z${z}`),
|
|
categories: [{ id: 'cat', kind: 'dysfunction', name: name(`z${z} cat`), review_status: 'reviewed', children: [node(z, 1)] }],
|
|
})),
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The two sizes PostgreSQL gives the document: its text (`octet_length`, the
|
|
* contract's measure, read by the save guard and the table CHECK) and its
|
|
* binary jsonb (`pg_column_size`, reported only: not the measure).
|
|
*/
|
|
const pgSizes = async (doc: Record<string, unknown>) => {
|
|
const [text, jsonb] = (await sql(`select octet_length(d::text) || ' ' || pg_column_size(d)
|
|
from (select $caps$${JSON.stringify(doc)}$caps$::jsonb d) x`)).split(' ').map(Number)
|
|
return { text, jsonb }
|
|
}
|
|
|
|
/** The largest label length whose document's text stays under the cap (with 4 KiB for the variants). */
|
|
async function calibrate(assets: string[]): Promise<number> {
|
|
let low = 1
|
|
let high = 200
|
|
while (low < high) {
|
|
const mid = Math.ceil((low + high) / 2)
|
|
const size = await pgSizes(capsDocument(assets, mid, 0))
|
|
if (size.text <= DOC_CAP - 4096) low = mid
|
|
else high = mid - 1
|
|
}
|
|
return low
|
|
}
|
|
|
|
async function timed(call: () => Promise<{ status: number; text: string }>): Promise<number> {
|
|
const start = performance.now()
|
|
const result = await call()
|
|
const ms = performance.now() - start
|
|
if (result.status !== 200) throw new Error(`HTTP ${result.status}: ${result.text.slice(0, 300)}`)
|
|
return ms
|
|
}
|
|
|
|
const stats = (samples: number[]) => {
|
|
const sorted = [...samples].sort((a, b) => a - b)
|
|
return {
|
|
n: sorted.length,
|
|
min: Math.round(sorted[0]),
|
|
median: Math.round(sorted[Math.floor(sorted.length / 2)]),
|
|
max: Math.round(sorted[sorted.length - 1]),
|
|
}
|
|
}
|
|
|
|
describe('timing at the caps (§15)', () => {
|
|
let E: SteppedAccount
|
|
let V: SteppedAccount
|
|
const samples: Record<string, number[]> = { save: [], submit: [], approve: [], publish: [] }
|
|
|
|
beforeAll(async () => {
|
|
E = await holder('perf-editor', 'admin', ['sheet_editor', 'sheet_publisher'])
|
|
V = await holder('perf-reviewer', null, ['sheet_reviewer'])
|
|
}, 120_000)
|
|
|
|
it('saves, submits, approves and publishes an at-caps document within the budget', async () => {
|
|
// The sheet and its four drawings near 2 MiB, uploaded through the INSERT policy.
|
|
const sheet = ok(await rpc('create_official_sheet',
|
|
{ slug: `perf-${Date.now().toString(36)}`, document: { ...capsDocument([null, null, null, null] as unknown as string[], 8, 0) } },
|
|
E.aal2)) as string
|
|
const draft = await sql(`select id from public.anatomical_sheet_versions where sheet_id = '${sheet}' and status = 'draft'`)
|
|
const assets: string[] = []
|
|
const shas: string[] = []
|
|
for (let i = 0; i < 4; i++) {
|
|
const bytes = drawing(2_000_000)
|
|
const asset = ok(await rpc('register_sheet_asset',
|
|
{ sheet_id: sheet, sha256: sha256(bytes), byte_size: bytes.length, file_name: `d${i}.svg` }, E.aal2)) as string
|
|
expect(await upload(E.aal2, bytes)).toBe(200)
|
|
ok(await rpc('confirm_sheet_asset_upload', { asset_id: asset }, E.aal2))
|
|
assets.push(asset)
|
|
shas.push(sha256(bytes))
|
|
}
|
|
const labelLength = await calibrate(assets)
|
|
const docSize = await pgSizes(capsDocument(assets, labelLength, 1))
|
|
let revision = Number(await sql(`select revision from public.anatomical_sheet_versions where id = '${draft}'`))
|
|
let version = draft
|
|
let variant = 0
|
|
|
|
const save = async () => {
|
|
variant += 1
|
|
samples.save.push(await timed(() => rpc('save_sheet_draft',
|
|
{ version_id: version, expected_revision: revision, document: capsDocument(assets, labelLength, variant), change_note: 'caps' }, E.aal2)))
|
|
revision += 1
|
|
}
|
|
const submit = async () => {
|
|
const start = performance.now()
|
|
const result = await rpc('submit_sheet_review', { version_id: version, expected_revision: revision, note: 'caps' }, E.aal2)
|
|
samples.submit.push(performance.now() - start)
|
|
return ok(result) as string
|
|
}
|
|
// The verdict of the worker's stand-in: 1 089 groups, the four drawings.
|
|
const groups = ['general', ...Array.from({ length: VIEWS }, (_, i) => (i === 0 ? 'view:main' : `view:v${i}`)),
|
|
...Array.from({ length: ZONES }, (_, z) => `zone:z${z}`)]
|
|
const open = (review: string) => sql(`update public.anatomical_sheet_reviews set status = 'open', verified_at = now(),
|
|
verified_with = 'sandbox', verdict = '${JSON.stringify({
|
|
fingerprint: 'a'.repeat(64),
|
|
drawings: assets.map((asset_id, i) => ({ asset_id, sha256: shas[i] })),
|
|
groups: groups.map((key) => ({ key, digest: sha256(Buffer.from(key)), coverable: true })),
|
|
})}'::jsonb where id = '${review}'`)
|
|
const approveAndPublish = async (review: string) => {
|
|
samples.approve.push(await timed(() => rpc('decide_sheet_review', { review_id: review, approve: true, note: 'caps' }, V.aal2)))
|
|
samples.publish.push(await timed(() => rpc('publish_sheet_version', { review_id: review }, E.aal2)))
|
|
}
|
|
|
|
// Cycle 1, no base: five saves, three submissions (two withdrawn), every
|
|
// group acknowledged by the reviewer, approval, publication.
|
|
for (let i = 0; i < 5; i++) await save()
|
|
for (let i = 0; i < 2; i++) ok(await rpc('withdraw_sheet_review', { review_id: await submit(), note: 'again' }, E.aal2))
|
|
const first = await submit()
|
|
await open(first)
|
|
await sql(`insert into public.anatomical_sheet_review_acknowledgments (review_id, group_key, group_digest, reviewer)
|
|
select '${first}', g ->> 'key', g ->> 'digest', '${V.id}'
|
|
from public.anatomical_sheet_reviews r, jsonb_array_elements(r.verdict -> 'groups') g where r.id = '${first}'`)
|
|
await approveAndPublish(first)
|
|
|
|
// Cycles 2 and 3: a new draft on the published base, one save, one
|
|
// submission, the same digests (covered by the base), approval, publication.
|
|
for (let cycle = 0; cycle < 2; cycle++) {
|
|
version = ok(await rpc('create_sheet_draft', { sheet_id: sheet, from_version_id: null, change_note: 'next' }, E.aal2)) as string
|
|
revision = 0
|
|
await save()
|
|
const review = await submit()
|
|
await open(review)
|
|
await approveAndPublish(review)
|
|
}
|
|
ok(await rpc('archive_anatomical_sheet', { sheet_id: sheet }, E.aal2))
|
|
|
|
const report = {
|
|
document: { textBytes: docSize.text, jsonbBytes: docSize.jsonb, cap: DOC_CAP, labelLength, views: VIEWS, annotations: ANNOTATIONS,
|
|
findingNodes: ZONES * (CHAIN + 1), depth: CHAIN + 1, groups: groups.length, drawings: 4, drawingBytes: 2_000_000 },
|
|
budgetMs: BUDGET_MS,
|
|
save: stats(samples.save), submit: stats(samples.submit), approve: stats(samples.approve), publish: stats(samples.publish),
|
|
}
|
|
console.info(`AT-CAPS TIMING ${JSON.stringify(report)}`)
|
|
expect(docSize.text).toBeGreaterThan(DOC_CAP - 64 * 1024)
|
|
expect(docSize.text).toBeLessThanOrEqual(DOC_CAP)
|
|
for (const op of ['save', 'submit', 'publish'])
|
|
expect(Math.max(...samples[op]), `${op} at the caps`).toBeLessThan(BUDGET_MS)
|
|
}, 600_000)
|
|
|
|
// The size guard's number walk at the caps (security gate on 7c4bf12): a
|
|
// document of many small numbers within the cap saves within the budget, one
|
|
// just over the cap answers the closed code within it, never a statement timeout.
|
|
it('measures a document of many small numbers within the budget', async () => {
|
|
// A small valid base (empty collections), so the numbers carry the size.
|
|
const base = { ...capsDocument([null, null, null, null] as unknown as string[], 8, 0),
|
|
references: [], categories: [], views: [], annotations: [], finding_zones: [] }
|
|
const sheet = ok(await rpc('create_official_sheet', { slug: `perf-n-${Date.now().toString(36)}`, document: base }, E.aal2)) as string
|
|
const draft = await sql(`select id from public.anatomical_sheet_versions where sheet_id = '${sheet}' and status = 'draft'`)
|
|
const revision = Number(await sql(`select revision from public.anatomical_sheet_versions where id = '${draft}'`))
|
|
const withNumbers = (count: number) => ({ ...base, pad: new Array<number>(count).fill(0) })
|
|
const within = await pgSizes(withNumbers(600_000))
|
|
expect(within.text).toBeLessThanOrEqual(DOC_CAP)
|
|
const saveMs = await timed(() => rpc('save_sheet_draft',
|
|
{ version_id: draft, expected_revision: revision, document: withNumbers(600_000), change_note: 'numbers' }, E.aal2))
|
|
const start = performance.now()
|
|
const over = await rpc('save_sheet_draft',
|
|
{ version_id: draft, expected_revision: revision + 1, document: withNumbers(1_000_000), change_note: 'numbers' }, E.aal2)
|
|
const overMs = performance.now() - start
|
|
console.info(`NUMBER WALK ${JSON.stringify({ within: { numbers: 600_000, textBytes: within.text, ms: Math.round(saveMs) },
|
|
over: { numbers: 1_000_000, status: over.status, ms: Math.round(overMs) } })}`)
|
|
expect(over.text).toContain('document.too_large')
|
|
expect(saveMs).toBeLessThan(BUDGET_MS)
|
|
expect(overMs).toBeLessThan(BUDGET_MS)
|
|
ok(await rpc('archive_anatomical_sheet', { sheet_id: sheet }, E.aal2))
|
|
}, 120_000)
|
|
})
|
|
|