oxedyne/fe2o3/fe2o3_austenite/tools/bench/cpuprof_runs.mjs
6.0 KiB, 1 run
created by r1870400018:66230, 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 | // One CPU-profiled run of the Austenite wasm, for cpuprof_rank.mjs. The profiler is node's own (the |
| 3 | // inspector's Profiler domain, the engine behind `--cpu-prof`) switched on in process around only the |
| 4 | // calls being measured, so loading the wasm and the untimed open are not in the profile. |
| 5 | // |
| 6 | // cold a fresh instance; one `compileProject` (PDF), the profile covering that call; |
| 7 | // warm one untimed `compileProject`, then the profile covers a second of the same project; |
| 8 | // edits an untimed `compileProjectDelta` open with `known: []`, then --edits one-letter edits, each |
| 9 | // a `compileProjectDelta` carrying the previous result's `order` as `known` (the live view's |
| 10 | // own path) with every changed page's SVG string walked, the profile covering all of them; |
| 11 | // unchanged the same open, then --reps `compileProjectDelta` calls of the unchanged source carrying |
| 12 | // `known`, the profile covering them. |
| 13 | // |
| 14 | // The edits are those of edit_latency.mjs: a letter typed onto the word at an EDITTOK marker, the markers |
| 15 | // at 0, 1/N, 2/N ... of the list (N = --positions) visited round-robin. |
| 16 | // |
| 17 | // Usage: cpuprof_runs.mjs --vendor=DIR --doc=FILE --mode=cold|warm|edits|unchanged --out=FILE.cpuprofile |
| 18 | // [--edits=10] [--positions=5] [--reps=1] [--interval=100] |
| 19 | // Prints one JSON line: the wall time of each profiled call (inflated a little by the sampling), the page |
| 20 | // count and the sizes. |
| 21 | import fs from 'node:fs'; |
| 22 | import inspector from 'node:inspector'; |
| 23 | import { loadAustenite, projectFromFile, timeOnce } from './lib/wasm_common.mjs'; |
| 24 | |
| 25 | function parseArgs(argv) { |
| 26 | const out = { edits: 10, positions: 5, reps: 1, interval: 100 }; |
| 27 | for (const a of argv.slice(2)) { |
| 28 | const m = a.match(/^--([^=]+)=(.*)$/); |
| 29 | if (!m) continue; |
| 30 | out[m[1]] = /^\d+$/.test(m[2]) ? Number(m[2]) : m[2]; |
| 31 | } |
| 32 | return out; |
| 33 | } |
| 34 | |
| 35 | function drawable(ret) { |
| 36 | if (!ret || ret.error) return -1; |
| 37 | let n = 0; |
| 38 | for (const c of ret.changed || []) n += typeof c.svg === 'string' ? c.svg.length : 0; |
| 39 | return n; |
| 40 | } |
| 41 | |
| 42 | function post(session, method, params) { |
| 43 | return new Promise((resolve, reject) => { |
| 44 | session.post(method, params, (e, r) => (e ? reject(e) : resolve(r))); |
| 45 | }); |
| 46 | } |
| 47 | |
| 48 | async function main() { |
| 49 | const args = parseArgs(process.argv); |
| 50 | for (const req of ['vendor', 'doc', 'mode', 'out']) { |
| 51 | if (!args[req]) { |
| 52 | process.stderr.write(`cpuprof_runs: --${req} is required\n`); |
| 53 | process.exit(2); |
| 54 | } |
| 55 | } |
| 56 | const engine = await loadAustenite(args.vendor); |
| 57 | const session = new inspector.Session(); |
| 58 | session.connect(); |
| 59 | await post(session, 'Profiler.enable'); |
| 60 | await post(session, 'Profiler.setSamplingInterval', { interval: args.interval }); |
| 61 | const result = { mode: args.mode, doc: args.doc, calls_s: [], ok: true }; |
| 62 | |
| 63 | const profiled = async (fn) => { |
| 64 | await post(session, 'Profiler.start'); |
| 65 | try { |
| 66 | fn(); |
| 67 | } finally { |
| 68 | const { profile } = await post(session, 'Profiler.stop'); |
| 69 | fs.writeFileSync(args.out, JSON.stringify(profile)); |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | const base = projectFromFile(args.doc); |
| 74 | if (args.mode === 'cold' || args.mode === 'warm') { |
| 75 | if (args.mode === 'warm') { |
| 76 | const { wall_s, ret } = timeOnce(() => engine.compilePdf(base)); |
| 77 | result.open_s = wall_s; |
| 78 | if (!ret || ret.error) throw new Error(`open failed: ${ret && ret.error}`); |
| 79 | } |
| 80 | await profiled(() => { |
| 81 | const { wall_s, ret } = timeOnce(() => engine.compilePdf(base)); |
| 82 | if (!ret || ret.error) { |
| 83 | result.ok = false; |
| 84 | result.error = String(ret && ret.error).slice(0, 300); |
| 85 | return; |
| 86 | } |
| 87 | result.calls_s.push(wall_s); |
| 88 | result.pages = ret.pages; |
| 89 | result.bytes = ret.pdf ? ret.pdf.length : 0; |
| 90 | }); |
| 91 | } else if (args.mode === 'edits' || args.mode === 'unchanged') { |
| 92 | let text = base.sources[0][1]; |
| 93 | const open = { ...base, known: [] }; |
| 94 | const { wall_s, ret } = timeOnce(() => engine.compileDelta(open)); |
| 95 | result.open_s = wall_s; |
| 96 | if (!ret || ret.error || !Array.isArray(ret.order)) throw new Error(`open failed: ${ret && ret.error}`); |
| 97 | let known = ret.order; |
| 98 | result.pages = ret.pages; |
| 99 | if (args.mode === 'unchanged') { |
| 100 | await profiled(() => { |
| 101 | for (let i = 0; i < args.reps; i++) { |
| 102 | const { wall_s: w, ret: r } = timeOnce(() => { |
| 103 | const x = engine.compileDelta({ ...base, known }); |
| 104 | drawable(x); |
| 105 | return x; |
| 106 | }); |
| 107 | if (!r || r.error || r.changed.length !== 0) { |
| 108 | result.ok = false; |
| 109 | result.error = `an unchanged recompile returned ${r && r.error ? r.error : r.changed.length + ' changed pages'}`; |
| 110 | return; |
| 111 | } |
| 112 | known = r.order; |
| 113 | result.calls_s.push(w); |
| 114 | } |
| 115 | }); |
| 116 | } else { |
| 117 | const tokens = [...new Set([...text.matchAll(/EDITTOK(\d+) /g)].map((m) => m[0].slice(0, -1)))]; |
| 118 | if (!tokens.length) throw new Error('no EDITTOK markers; doc not from gen_synthetic.py'); |
| 119 | const positions = []; |
| 120 | for (let i = 0; i < args.positions; i++) { |
| 121 | positions.push(tokens[Math.min(Math.floor((i / args.positions) * tokens.length), tokens.length - 1)]); |
| 122 | } |
| 123 | result.changed = []; |
| 124 | await profiled(() => { |
| 125 | for (let i = 0; i < args.edits; i++) { |
| 126 | const tok = positions[i % positions.length]; |
| 127 | const marker = `${tok}${String.fromCharCode(97 + ((i + 1) % 26))}`; |
| 128 | const at = text.indexOf(`${tok} `); |
| 129 | if (at < 0) throw new Error(`marker ${tok} is gone`); |
| 130 | text = text.slice(0, at) + marker + text.slice(at + tok.length); |
| 131 | positions[i % positions.length] = marker; |
| 132 | const project = { main: '/main.typ', sources: [['/main.typ', text]], known, strict: true }; |
| 133 | const { wall_s: w, ret: r } = timeOnce(() => { |
| 134 | const x = engine.compileDelta(project); |
| 135 | drawable(x); |
| 136 | return x; |
| 137 | }); |
| 138 | if (!r || r.error) { |
| 139 | result.ok = false; |
| 140 | result.error = `edit ${i}: ${r && r.error}`; |
| 141 | return; |
| 142 | } |
| 143 | known = r.order; |
| 144 | result.calls_s.push(w); |
| 145 | result.changed.push(r.changed.length); |
| 146 | } |
| 147 | }); |
| 148 | } |
| 149 | } else { |
| 150 | throw new Error(`unknown mode ${args.mode}`); |
| 151 | } |
| 152 | result.heap_mb = engine.heapMB ? engine.heapMB() : null; |
| 153 | process.stdout.write(JSON.stringify(result) + '\n'); |
| 154 | } |
| 155 | |
| 156 | main().catch((e) => { |
| 157 | process.stdout.write(JSON.stringify({ ok: false, error: String((e && e.stack) || e) }) + '\n'); |
| 158 | process.exit(1); |
| 159 | }); |