oxedyne/fe2o3/fe2o3_austenite/tools/heap_probe.mjs
21.3 KiB, 70 runs, executable
created by r1870400018:65172, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | #!/usr/bin/env node |
| 2 | // heap_probe.mjs -- peak memory of a compile, Austenite against typst.ts, in node. |
| 3 | // |
| 4 | // Each (engine, page count, run) is a fresh child process, so one compile's heap cannot flatter or |
| 5 | // burden the next: a wasm32 linear memory only ever grows, which makes its `byteLength` after the |
| 6 | // compile the high-water mark of that compile, and the child's `VmHWM` is the kernel's own peak |
| 7 | // resident set for the whole process (node itself included; the baseline before the engine loads |
| 8 | // is reported beside it so the difference can be read). |
| 9 | // |
| 10 | // The corpus is synthetic and deterministic: headings, paragraphs, lists, a small table and an |
| 11 | // equation per section, sized by a words-per-page estimate. The page count each engine actually |
| 12 | // produced is read back from its own PDF (or, on the live view's path, from the delta) and |
| 13 | // printed, so a corpus that came out shorter or longer than asked is visible rather than assumed. |
| 14 | // |
| 15 | // Two modes, the two ways Daimond compiles: |
| 16 | // pdf one compile to PDF, the door behind the PDF button and the model's tool; |
| 17 | // delta the live view. Austenite: `compileProjectDelta` opened with `known: []`, then --edits |
| 18 | // one-letter edits each carrying the previous `order` as `known`, the consumer's own |
| 19 | // cache (id -> SVG) kept as Daimond keeps it. typst.ts has no delta door: its live view is |
| 20 | // a full `vector` compile on every edit, so the same edits go through that. The heap is |
| 21 | // read after the open and after each edit. |
| 22 | // |
| 23 | // node tools/heap_probe.mjs --pages 50,300,1000 --runs 3 --austenite-pkg PKG --json heap.json --gate |
| 24 | // node tools/heap_probe.mjs --mode delta --pages 50,300,1000 --engines austenite,typst --runs 3 |
| 25 | // node tools/heap_probe.mjs --gate-file heap.json # judge a saved run; runs nothing |
| 26 | // tools/heap_gate/check.sh # the gate's own check, on synthetic files |
| 27 | // |
| 28 | // Run it under a memory cap, as every heavy job on this host is: |
| 29 | // systemd-run --user --scope --quiet -p MemoryMax=3G --slice=claude-rc.slice node tools/heap_probe.mjs |
| 30 | // |
| 31 | // Options: |
| 32 | // --pages a,b,c page counts to generate (default 50,300,1000) |
| 33 | // --engines x,y,z `typst` (typst.ts web compiler), `austenite` (--austenite-pkg) and `ref` |
| 34 | // (--ref-pkg, Daimond's vendored curated Austenite), default typst,austenite |
| 35 | // --mode pdf|delta see above (default pdf) |
| 36 | // --runs N fresh processes per cell, the engine order alternating from one run to the |
| 37 | // next so neither always runs first on a warm host (default 1) |
| 38 | // --edits N one-letter edits after the open, in delta mode (default 5) |
| 39 | // --typst-vendor DIR directory holding typst_ts_web_compiler{.mjs,_bg.wasm} and fonts/ |
| 40 | // --austenite-pkg DIR a wasm-pack `--target web` output directory for fe2o3_austenite |
| 41 | // --ref-pkg DIR the same, for the reference row |
| 42 | // --plain prose and headings only, for an engine that refuses richer constructs |
| 43 | // --timeout SEC per child (default 600) |
| 44 | // --json FILE also write every measurement, the per-cell medians and the gate as JSON |
| 45 | // --gate exit non-zero unless the memory gate holds (D-20260923-34), see `judge` |
| 46 | // --gate-file A[,B..] judge the rows of saved --json files (one cell's runs may be split across |
| 47 | // them) and exit; nothing is run |
| 48 | // --dump PAGES print the generated source for PAGES and exit |
| 49 | |
| 50 | import { readFileSync, readdirSync, writeFileSync } from 'node:fs'; |
| 51 | import { spawnSync } from 'node:child_process'; |
| 52 | import { join, resolve } from 'node:path'; |
| 53 | import { fileURLToPath, pathToFileURL } from 'node:url'; |
| 54 | |
| 55 | const HOME = process.env.HOME || ''; |
| 56 | const DAIMOND = join(HOME, 'usr/code/web/apps/oxedyne/daimond/www/vendor'); |
| 57 | const DEFAULT_TYPST = join(DAIMOND, 'typst'); |
| 58 | const DEFAULT_AUST = join(DAIMOND, 'austenite'); |
| 59 | |
| 60 | // The memory gate: Austenite's peak heap at 300 pages over typst.ts's must not exceed R_GATE (the aim |
| 61 | // is R_AIM), and its growth from 300 to 1,000 pages must not exceed G_GATE MiB for each page it produced. |
| 62 | export const R_GATE = 0.62; |
| 63 | export const R_AIM = 0.5; |
| 64 | export const G_GATE = 0.46; |
| 65 | |
| 66 | function opt(name, dflt) { |
| 67 | const i = process.argv.indexOf(name); |
| 68 | return i >= 0 && i + 1 < process.argv.length ? process.argv[i + 1] : dflt; |
| 69 | } |
| 70 | const flag = (name) => process.argv.includes(name); |
| 71 | |
| 72 | // Corpus |
| 73 | |
| 74 | const WORDS = ('the of a and to in is that for it as was with be by on not this are or his from at ' + |
| 75 | 'which but have an they one you were her all she there would their we him been has when who will ' + |
| 76 | 'more no if out so said what up its about into than them can only other new some could time these ' + |
| 77 | 'two may then do first any my now such like our over man me even most made after also did many ' + |
| 78 | 'before must through back years where much your way well down should because each just those people ' + |
| 79 | 'how too little state good very make world still own see men work long get here between both life ' + |
| 80 | 'being under never day same another know while last might us great old year off come since against ' + |
| 81 | 'go came right used take three structure measure element paragraph evaluation realisation margin').split(' '); |
| 82 | |
| 83 | // A small deterministic generator (xorshift32), so every run and every engine sees the same text. |
| 84 | function rng(seed) { |
| 85 | let s = seed >>> 0 || 1; |
| 86 | return () => { |
| 87 | s ^= s << 13; s >>>= 0; |
| 88 | s ^= s >> 17; |
| 89 | s ^= s << 5; s >>>= 0; |
| 90 | return s / 4294967296; |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | function sentence(r, n) { |
| 95 | const w = []; |
| 96 | for (let i = 0; i < n; i++) w.push(WORDS[Math.floor(r() * WORDS.length)]); |
| 97 | w[0] = w[0][0].toUpperCase() + w[0].slice(1); |
| 98 | return w.join(' ') + '.'; |
| 99 | } |
| 100 | |
| 101 | function paragraph(r, words, rich) { |
| 102 | const out = []; |
| 103 | let left = words; |
| 104 | while (left > 0) { |
| 105 | const n = Math.min(left, 8 + Math.floor(r() * 12)); |
| 106 | let s = sentence(r, n); |
| 107 | if (rich && r() < 0.15) s = s.replace(/^(\S+) (\S+)/, '$1 _$2_'); |
| 108 | if (rich && r() < 0.10) s = s.replace(/ (\S+)\.$/, ' *$1*.'); |
| 109 | out.push(s); |
| 110 | left -= n; |
| 111 | } |
| 112 | return out.join(' '); |
| 113 | } |
| 114 | |
| 115 | // Calibrated against typst.ts: about 590 of these words to an A4 page of 11pt Libertinus with |
| 116 | // Typst's default margins, headings and blocks included; the read-back page count is what is |
| 117 | // reported, not this estimate. |
| 118 | const WORDS_PER_PAGE = 590; |
| 119 | |
| 120 | export function corpus(pages, rich) { |
| 121 | const r = rng(0x5eed + pages); |
| 122 | const parts = ['#set heading(numbering: "1.1")', '']; |
| 123 | const target = pages * WORDS_PER_PAGE; |
| 124 | let words = 0; |
| 125 | let chapter = 0; |
| 126 | let section = 0; |
| 127 | while (words < target) { |
| 128 | if (section % 6 === 0) { |
| 129 | chapter++; |
| 130 | parts.push('= Chapter ' + chapter + ': ' + sentence(r, 3).slice(0, -1), ''); |
| 131 | } |
| 132 | section++; |
| 133 | parts.push('== ' + sentence(r, 4).slice(0, -1), ''); |
| 134 | for (let p = 0; p < 4; p++) { |
| 135 | const n = 70 + Math.floor(r() * 60); |
| 136 | parts.push(paragraph(r, n, rich), ''); |
| 137 | words += n; |
| 138 | } |
| 139 | if (rich) { |
| 140 | parts.push('- ' + sentence(r, 6), '- ' + sentence(r, 7), '- ' + sentence(r, 5), ''); |
| 141 | parts.push('#table(columns: 3, [' + WORDS[section % 50] + '], [' + section + '], [' + |
| 142 | (section * 7) + '], [a], [b], [c])', ''); |
| 143 | parts.push('$ x_' + section + ' = sum_(i=1)^n a_i b_i $', ''); |
| 144 | words += 40; |
| 145 | } |
| 146 | } |
| 147 | return parts.join('\n') + '\n'; |
| 148 | } |
| 149 | |
| 150 | // `n` successive sources, each the last with one letter typed onto a plain word of a body paragraph, |
| 151 | // the paragraphs spread evenly through the document and visited in turn, as a person typing about |
| 152 | // would. Only a word that is bare lowercase letters is touched, so no edit can break the markup. |
| 153 | export function edited(src, n) { |
| 154 | const lines = src.split('\n'); |
| 155 | const body = []; |
| 156 | for (let i = 0; i < lines.length; i++) { |
| 157 | if (lines[i].length > 80 && !/^[=\-#$]/.test(lines[i])) body.push(i); |
| 158 | } |
| 159 | if (!body.length) throw new Error('the corpus has no body paragraph to edit'); |
| 160 | const out = []; |
| 161 | for (let k = 0; k < n; k++) { |
| 162 | const at = body[Math.min(Math.floor(((k + 0.5) / n) * body.length), body.length - 1)]; |
| 163 | const words = lines[at].split(' '); |
| 164 | const w = words.findIndex((x) => /^[a-z]{4,}$/.test(x)); |
| 165 | if (w < 0) throw new Error('paragraph ' + at + ' has no plain word to edit'); |
| 166 | words[w] += 'x'; |
| 167 | lines[at] = words.join(' '); |
| 168 | out.push(lines.join('\n')); |
| 169 | } |
| 170 | return out; |
| 171 | } |
| 172 | |
| 173 | function pdfPages(bytes) { |
| 174 | const s = Buffer.from(bytes).toString('latin1'); |
| 175 | const m = s.match(/\/Type\s*\/Page(?![s\w])/g); |
| 176 | return m ? m.length : 0; |
| 177 | } |
| 178 | |
| 179 | function status() { |
| 180 | const s = readFileSync('/proc/self/status', 'utf8'); |
| 181 | const kb = (k) => { |
| 182 | const m = new RegExp(k + ':\\s+(\\d+) kB').exec(s); |
| 183 | return m ? +m[1] : 0; |
| 184 | }; |
| 185 | return { rss: kb('VmRSS'), hwm: kb('VmHWM') }; |
| 186 | } |
| 187 | |
| 188 | // Engines, each run inside a child |
| 189 | |
| 190 | async function loadTypst(dir) { |
| 191 | const mod = await import(pathToFileURL(join(dir, 'typst_ts_web_compiler.mjs')).href); |
| 192 | const exports = await mod.default({ module_or_path: readFileSync(join(dir, 'typst_ts_web_compiler_bg.wasm')) }); |
| 193 | const builder = new mod.TypstCompilerBuilder(); |
| 194 | builder.set_dummy_access_model(); |
| 195 | const fontDir = join(dir, 'fonts'); |
| 196 | for (const f of readdirSync(fontDir).filter((n) => /\.(otf|ttf)$/i.test(n)).sort()) { |
| 197 | await builder.add_raw_font(new Uint8Array(readFileSync(join(fontDir, f)))); |
| 198 | } |
| 199 | const compiler = await builder.build(); |
| 200 | const run = (src, kind) => { |
| 201 | compiler.reset_shadow(); |
| 202 | compiler.add_source('/main.typ', src); |
| 203 | const ret = compiler.compile('/main.typ', undefined, kind, 3); |
| 204 | const out = ret instanceof Uint8Array ? ret : (ret && (ret.result || ret.artifact)); |
| 205 | if (!(out instanceof Uint8Array)) { |
| 206 | return { error: JSON.stringify(ret && ret.diagnostics ? ret.diagnostics : ret).slice(0, 400) }; |
| 207 | } |
| 208 | return { bytes: out }; |
| 209 | }; |
| 210 | return { |
| 211 | memory: exports.memory, |
| 212 | compile(src) { |
| 213 | const o = run(src, 'pdf'); |
| 214 | return o.error ? o : { pdf: o.bytes }; |
| 215 | }, |
| 216 | // No delta door: the live view is a full `vector` compile on each edit. |
| 217 | open(src) { |
| 218 | const o = run(src, 'vector'); |
| 219 | return o.error ? o : { pages: null, sent: o.bytes.length }; |
| 220 | }, |
| 221 | edit(src) { |
| 222 | return this.open(src); |
| 223 | }, |
| 224 | }; |
| 225 | } |
| 226 | |
| 227 | async function loadAustenite(dir) { |
| 228 | const js = readdirSync(dir).find((n) => /^oxedyne_fe2o3_austenite\.js$/.test(n)); |
| 229 | const wasm = readdirSync(dir).find((n) => /_bg\.wasm$/.test(n)); |
| 230 | if (!js || !wasm) throw new Error('no wasm-pack output in ' + dir); |
| 231 | const mod = await import(pathToFileURL(join(dir, js)).href); |
| 232 | const exports = await mod.default({ module_or_path: readFileSync(join(dir, wasm)) }); |
| 233 | const aus = new mod.DaimondTypst(); |
| 234 | const project = (src, extra) => ({ main: '/main.typ', sources: [['/main.typ', src]], assets: [], fonts: [], ...extra }); |
| 235 | // The consumer's page cache, id -> SVG, as Daimond keeps it: new pages in, absent ids out. |
| 236 | const cache = new Map(); |
| 237 | let known = []; |
| 238 | const delta = (src) => { |
| 239 | const ret = aus.compileProjectDelta(project(src, { known })); |
| 240 | if (!ret || ret.error || !Array.isArray(ret.order)) { |
| 241 | return { error: String(ret && ret.error ? ret.error : 'no delta').slice(0, 400) }; |
| 242 | } |
| 243 | let sent = 0; |
| 244 | for (const c of ret.changed) { |
| 245 | cache.set(c.id, c.svg); |
| 246 | sent += c.svg.length; |
| 247 | } |
| 248 | const live = new Set(ret.order); |
| 249 | for (const id of [...cache.keys()]) if (!live.has(id)) cache.delete(id); |
| 250 | known = ret.order; |
| 251 | return { pages: ret.order.length, sent }; |
| 252 | }; |
| 253 | return { |
| 254 | memory: exports.memory, |
| 255 | compile(src) { |
| 256 | const ret = aus.compileProject(project(src)); |
| 257 | if (ret && ret.pdf) return { pdf: ret.pdf }; |
| 258 | return { error: String(ret && ret.error ? ret.error : 'no pdf').slice(0, 400) }; |
| 259 | }, |
| 260 | open: delta, |
| 261 | edit: delta, |
| 262 | }; |
| 263 | } |
| 264 | |
| 265 | function pkgOf(engine) { |
| 266 | if (engine === 'ref') return opt('--ref-pkg', DEFAULT_AUST); |
| 267 | const dir = opt('--austenite-pkg', null); |
| 268 | if (!dir) throw new Error('the austenite engine needs --austenite-pkg DIR, the wasm-pack output to measure'); |
| 269 | return dir; |
| 270 | } |
| 271 | |
| 272 | async function child(engine, pages) { |
| 273 | const base = status(); |
| 274 | const src = corpus(pages, !flag('--plain')); |
| 275 | const mode = opt('--mode', 'pdf'); |
| 276 | const t0 = performance.now(); |
| 277 | const eng = engine === 'typst' |
| 278 | ? await loadTypst(resolve(opt('--typst-vendor', DEFAULT_TYPST))) |
| 279 | : await loadAustenite(resolve(pkgOf(engine))); |
| 280 | const t1 = performance.now(); |
| 281 | const heapBefore = eng.memory.buffer.byteLength; |
| 282 | const row = { |
| 283 | engine, pages, mode, sourceBytes: Buffer.byteLength(src), |
| 284 | initMs: Math.round(t1 - t0), heapBefore, rssBaselineKb: base.rss, error: null, |
| 285 | }; |
| 286 | // A throw out of the wasm (an allocation past the 4 GiB a wasm32 memory can hold, a panic) is a result |
| 287 | // of the cell, not of the probe: the row records it with the heap the instance had reached. |
| 288 | if (mode === 'pdf') { |
| 289 | try { |
| 290 | const out = eng.compile(src); |
| 291 | row.compileMs = Math.round(performance.now() - t1); |
| 292 | row.heapPeak = eng.memory.buffer.byteLength; |
| 293 | row.pagesOut = out.pdf ? pdfPages(out.pdf) : 0; |
| 294 | row.error = out.error || null; |
| 295 | } catch (e) { |
| 296 | row.error = 'threw ' + String(e).slice(0, 300); |
| 297 | row.heapPeak = eng.memory.buffer.byteLength; |
| 298 | row.pagesOut = 0; |
| 299 | } |
| 300 | } else { |
| 301 | const n = +opt('--edits', '5'); |
| 302 | const steps = [src, ...edited(src, n)]; |
| 303 | row.heaps = []; |
| 304 | row.sent = []; |
| 305 | row.ms = []; |
| 306 | for (const s of steps) { |
| 307 | const t = performance.now(); |
| 308 | let out; |
| 309 | try { |
| 310 | out = row.heaps.length === 0 ? eng.open(s) : eng.edit(s); |
| 311 | } catch (e) { |
| 312 | out = { error: 'threw ' + String(e).slice(0, 300) }; |
| 313 | } |
| 314 | row.ms.push(Math.round(performance.now() - t)); |
| 315 | if (out.error) { |
| 316 | row.error = (row.heaps.length === 0 ? 'open: ' : 'edit ' + row.heaps.length + ': ') + out.error; |
| 317 | row.heapAtError = eng.memory.buffer.byteLength; |
| 318 | break; |
| 319 | } |
| 320 | if (row.pagesOut === undefined && out.pages) row.pagesOut = out.pages; |
| 321 | row.heaps.push(eng.memory.buffer.byteLength); |
| 322 | row.sent.push(out.sent); |
| 323 | // What the parent keeps if this process is killed before it finishes. |
| 324 | process.stdout.write('@@STEP ' + JSON.stringify({ heaps: row.heaps, sent: row.sent, ms: row.ms, pagesOut: row.pagesOut ?? null, rssPeakKb: status().hwm }) + '\n'); |
| 325 | } |
| 326 | row.heapPeak = eng.memory.buffer.byteLength; |
| 327 | row.heapOpen = row.heaps[0] ?? null; |
| 328 | row.pagesOut ??= null; |
| 329 | } |
| 330 | row.rssPeakKb = status().hwm; |
| 331 | process.stdout.write('@@ROW ' + JSON.stringify(row) + '\n'); |
| 332 | } |
| 333 | |
| 334 | // Parent |
| 335 | |
| 336 | const mb = (b) => (b / 1048576).toFixed(1); |
| 337 | |
| 338 | function runChild(engine, pages) { |
| 339 | const args = [fileURLToPath(import.meta.url), '--child', engine, String(pages)]; |
| 340 | for (const k of ['--typst-vendor', '--austenite-pkg', '--ref-pkg', '--mode', '--edits']) { |
| 341 | const v = opt(k, null); |
| 342 | if (v) args.push(k, v); |
| 343 | } |
| 344 | if (flag('--plain')) args.push('--plain'); |
| 345 | const res = spawnSync(process.execPath, args, { |
| 346 | encoding: 'utf8', timeout: +opt('--timeout', '600') * 1000, maxBuffer: 1 << 26, |
| 347 | }); |
| 348 | const lines = (res.stdout || '').split('\n'); |
| 349 | const line = lines.find((l) => l.startsWith('@@ROW ')); |
| 350 | if (line) return JSON.parse(line.slice(6)); |
| 351 | const why = res.error ? String(res.error.message) |
| 352 | : res.signal ? 'killed by ' + res.signal : 'exit ' + res.status; |
| 353 | const row = { engine, pages, mode: opt('--mode', 'pdf'), error: why + ': ' + (res.stderr || '').trim().split('\n').slice(-3).join(' | ') }; |
| 354 | // A killed delta run still says how far it got: the heap after each step it finished. |
| 355 | const step = lines.filter((l) => l.startsWith('@@STEP ')).pop(); |
| 356 | if (step) { |
| 357 | const st = JSON.parse(step.slice(7)); |
| 358 | Object.assign(row, st, { heapPeak: st.heaps[st.heaps.length - 1], heapOpen: st.heaps[0], finished: st.heaps.length }); |
| 359 | } |
| 360 | return row; |
| 361 | } |
| 362 | |
| 363 | function median(xs) { |
| 364 | const v = xs.filter((x) => typeof x === 'number').sort((a, b) => a - b); |
| 365 | if (!v.length) return null; |
| 366 | const m = v.length >> 1; |
| 367 | return v.length % 2 ? v[m] : (v[m - 1] + v[m]) / 2; |
| 368 | } |
| 369 | |
| 370 | // The median of each measure over the runs of one (engine, pages) cell. A cell with an erroring run is |
| 371 | // left out: a median over the survivors would flatter the engine that fell over. |
| 372 | export function cells(rows) { |
| 373 | const by = new Map(); |
| 374 | for (const r of rows) { |
| 375 | const k = r.engine + '/' + r.pages; |
| 376 | if (!by.has(k)) by.set(k, []); |
| 377 | by.get(k).push(r); |
| 378 | } |
| 379 | const out = []; |
| 380 | for (const [, rs] of by) { |
| 381 | const bad = rs.filter((r) => r.error); |
| 382 | const c = { |
| 383 | engine: rs[0].engine, pages: rs[0].pages, runs: rs.length, errors: bad.length, |
| 384 | heapPeak: null, rssPeakKb: null, pagesOut: null, |
| 385 | }; |
| 386 | if (!bad.length) { |
| 387 | c.heapPeak = median(rs.map((r) => r.heapPeak)); |
| 388 | c.rssPeakKb = median(rs.map((r) => r.rssPeakKb)); |
| 389 | c.pagesOut = median(rs.map((r) => r.pagesOut)); |
| 390 | c.heapOpen = median(rs.map((r) => r.heapOpen)); |
| 391 | } else { |
| 392 | c.error = bad[0].error; |
| 393 | } |
| 394 | out.push(c); |
| 395 | } |
| 396 | return out; |
| 397 | } |
| 398 | |
| 399 | // The memory gate (D-20260923-34), from the per-cell medians, in MiB: |
| 400 | // R = H_A(300) / H_T(300), at most R_GATE, with R_AIM the aim; |
| 401 | // G = (H_A(1000) - H_A(300)) / (P_A(1000) - P_A(300)), at most G_GATE MiB for each page Austenite |
| 402 | // produced between the two sizes (P is the page count read back from its own PDF). |
| 403 | // typst.ts's own G is computed the same way and printed beside it. A missing or failed cell is a |
| 404 | // verdict of `incomplete`, which is not a pass. |
| 405 | export function judge(cs, a = 'austenite', t = 'typst') { |
| 406 | const at = (e, p) => cs.find((c) => c.engine === e && c.pages === p && c.heapPeak !== null); |
| 407 | const a300 = at(a, 300); |
| 408 | const a1000 = at(a, 1000); |
| 409 | const t300 = at(t, 300); |
| 410 | const t1000 = at(t, 1000); |
| 411 | const need = [[a, 300, a300], [a, 1000, a1000], [t, 300, t300]].filter((x) => !x[2]).map((x) => x[0] + '/' + x[1]); |
| 412 | if (need.length) return { verdict: 'incomplete', missing: need }; |
| 413 | const R = a300.heapPeak / t300.heapPeak; |
| 414 | const dp = a1000.pagesOut - a300.pagesOut; |
| 415 | const G = dp > 0 ? (a1000.heapPeak - a300.heapPeak) / 1048576 / dp : Infinity; |
| 416 | const dpt = t1000 && t300 ? t1000.pagesOut - t300.pagesOut : 0; |
| 417 | const Gt = t1000 && dpt > 0 ? (t1000.heapPeak - t300.heapPeak) / 1048576 / dpt : null; |
| 418 | const rOk = R <= R_GATE; |
| 419 | const gOk = G <= G_GATE; |
| 420 | return { |
| 421 | verdict: rOk && gOk ? 'pass' : 'fail', |
| 422 | R, R_gate: R_GATE, R_aim: R_AIM, R_ok: rOk, R_aim_met: R <= R_AIM, |
| 423 | G, G_gate: G_GATE, G_ok: gOk, G_typst: Gt, |
| 424 | pages_a300: a300.pagesOut, pages_a1000: a1000.pagesOut, |
| 425 | }; |
| 426 | } |
| 427 | |
| 428 | function printJudgement(j) { |
| 429 | if (j.verdict === 'incomplete') { |
| 430 | console.log('gate INCOMPLETE: no measurement for ' + j.missing.join(', ')); |
| 431 | return; |
| 432 | } |
| 433 | const f = (x, d) => (x === null || x === undefined ? '-' : Number.isFinite(x) ? x.toFixed(d) : String(x)); |
| 434 | console.log('R = H_A(300)/H_T(300) = ' + f(j.R, 3) + ' gate <= ' + j.R_gate + ' -> ' + (j.R_ok ? 'PASS' : 'FAIL') |
| 435 | + ' (margin ' + f(j.R_gate - j.R, 3) + ')'); |
| 436 | console.log('aim R <= ' + j.R_aim + ' -> ' + (j.R_aim_met ? 'met' : 'not met (' + f(j.R - j.R_aim, 3) + ' over)')); |
| 437 | console.log('G = growth 300 -> 1000 = ' + f(j.G, 3) + ' MiB a page over ' + (j.pages_a1000 - j.pages_a300) |
| 438 | + ' pages gate <= ' + j.G_gate + ' -> ' + (j.G_ok ? 'PASS' : 'FAIL') + ' (margin ' + f(j.G_gate - j.G, 3) |
| 439 | + ') typst.ts G = ' + f(j.G_typst, 3)); |
| 440 | console.log('memory gate: ' + j.verdict.toUpperCase()); |
| 441 | } |
| 442 | |
| 443 | function table(rows) { |
| 444 | console.log('engine mode pages out src KB init ms work ms (open or compile, then edits) heap MB open MB rss base MB rss peak MB note'); |
| 445 | for (const r of rows) { |
| 446 | console.log( |
| 447 | r.engine.padEnd(10) + String(r.mode ?? 'pdf').padEnd(6) + String(r.pages).padStart(6) + String(r.pagesOut ?? '-').padStart(5) |
| 448 | + String(r.sourceBytes ? Math.round(r.sourceBytes / 1024) : '-').padStart(8) |
| 449 | + String(r.initMs ?? '-').padStart(9) |
| 450 | + ' ' + String(r.compileMs ?? (r.ms ? r.ms.join('/') : '-')).padEnd(34) |
| 451 | + (r.heapPeak ? mb(r.heapPeak) : '-').padStart(9) |
| 452 | + (r.heapOpen ? mb(r.heapOpen) : '-').padStart(9) |
| 453 | + (r.rssBaselineKb ? mb(r.rssBaselineKb * 1024) : '-').padStart(13) |
| 454 | + (r.rssPeakKb ? mb(r.rssPeakKb * 1024) : '-').padStart(13) |
| 455 | + ' ' + (r.error ? 'ERROR ' + r.error : '')); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | async function main() { |
| 460 | if (flag('--child')) { |
| 461 | const i = process.argv.indexOf('--child'); |
| 462 | return child(process.argv[i + 1], +process.argv[i + 2]); |
| 463 | } |
| 464 | if (opt('--gate-file', null)) { |
| 465 | const rows = opt('--gate-file', '').split(',').flatMap((f) => JSON.parse(readFileSync(f, 'utf8')).rows); |
| 466 | const j = judge(cells(rows)); |
| 467 | printJudgement(j); |
| 468 | process.exit(j.verdict === 'pass' ? 0 : j.verdict === 'fail' ? 1 : 2); |
| 469 | } |
| 470 | if (opt('--dump', null)) { |
| 471 | process.stdout.write(corpus(+opt('--dump', '1'), !flag('--plain'))); |
| 472 | return; |
| 473 | } |
| 474 | const pages = opt('--pages', '50,300,1000').split(',').map(Number); |
| 475 | const engines = opt('--engines', 'typst,austenite').split(','); |
| 476 | const runs = +opt('--runs', '1'); |
| 477 | const rows = []; |
| 478 | for (let run = 0; run < runs; run++) { |
| 479 | const order = run % 2 ? [...engines].reverse() : engines; |
| 480 | for (const p of pages) { |
| 481 | for (const e of order) { |
| 482 | const r = runChild(e, p); |
| 483 | r.run = run; |
| 484 | rows.push(r); |
| 485 | process.stderr.write(`run ${run} ${e} ${p}: heap ${r.heapPeak ? mb(r.heapPeak) : '-'} MB${r.error ? ' ERROR' : ''}\n`); |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | table(rows); |
| 490 | const cs = cells(rows); |
| 491 | console.log('\nmedians over ' + runs + ' run(s), MiB:'); |
| 492 | for (const c of cs) { |
| 493 | console.log(c.engine.padEnd(10) + String(c.pages).padStart(6) + ' pages out ' + String(c.pagesOut ?? '-').padStart(5) |
| 494 | + ' heap ' + (c.heapPeak !== null ? mb(c.heapPeak) : '-').padStart(8) |
| 495 | + ' VmHWM ' + (c.rssPeakKb !== null ? mb(c.rssPeakKb * 1024) : '-').padStart(8) |
| 496 | + (c.errors ? ' ' + c.errors + ' erroring run(s): ' + c.error : '')); |
| 497 | } |
| 498 | const j = judge(cs); |
| 499 | printJudgement(j); |
| 500 | if (engines.includes('ref')) { |
| 501 | const k = judge(cs, 'ref'); |
| 502 | if (k.verdict !== 'incomplete') console.log('reference row (Daimond\'s vendored Austenite): R = ' + k.R.toFixed(3) + ', G = ' + k.G.toFixed(3) + ' MiB a page'); |
| 503 | } |
| 504 | const json = opt('--json', null); |
| 505 | if (json) writeFileSync(json, JSON.stringify({ rows, cells: cs, gate: j }, null, 1)); |
| 506 | if (flag('--gate') && j.verdict !== 'pass') process.exit(j.verdict === 'fail' ? 1 : 2); |
| 507 | } |
| 508 | |
| 509 | if (import.meta.url === pathToFileURL(process.argv[1]).href) await main(); |