| 1 | // native/benchmarks/live-push/gen-typing.mjs <seed.bin> <typists> <keys> <window> <outdir> |
| 2 | // |
| 3 | // The simulated typists' keystrokes for the live-push latency probe |
| 4 | // (internal/docs/live_push_probe_test.go). Typist t is Yjs client 5000+t; it |
| 5 | // types `keys` "Q"s, one after another, into its own paragraph of the seed |
| 6 | // document (paragraph t, counted from the middle). Written as <outdir>/t<t>.bin: |
| 7 | // a sequence of records [u32 lo][u32 hi][u32 n][n bytes], little endian, one |
| 8 | // per window [lo, hi] (0-based, inclusive, hi - lo < window): the update a |
| 9 | // sender that coalesced keys lo..hi would POST (Y.mergeUpdates of their own |
| 10 | // updates, which is what save-queue.mjs sends). Run from the repository root |
| 11 | // with fixtures/node_modules installed (cd fixtures && npm ci --ignore-scripts). |
| 12 | import { readFileSync, writeFileSync, mkdirSync } from 'node:fs' |
| 13 | import { fileURLToPath, pathToFileURL } from 'node:url' |
| 14 | import path from 'node:path' |
| 15 | |
| 16 | const here = path.dirname(fileURLToPath(import.meta.url)) |
| 17 | const Y = await import(pathToFileURL(path.join(here, '../../../fixtures/node_modules/yjs/dist/yjs.mjs')).href) |
| 18 | |
| 19 | const [seed, typistsArg = '1', keysArg = '200', windowArg = '24', outdir = '/tmp/live-push/typing'] = process.argv.slice(2) |
| 20 | const typists = Number(typistsArg), keys = Number(keysArg), window = Number(windowArg) |
| 21 | mkdirSync(outdir, { recursive: true }) |
| 22 | const seedBytes = readFileSync(seed) |
| 23 | for (let t = 0; t < typists; t++) { |
| 24 | const doc = new Y.Doc() |
| 25 | doc.clientID = 5000 + t |
| 26 | Y.applyUpdate(doc, seedBytes) |
| 27 | const blocks = doc.getXmlFragment('prosemirror').toArray() |
| 28 | const paragraphs = blocks.filter(b => b.nodeName === 'paragraph' && b.toArray && b.toArray()[0] instanceof Y.XmlText) |
| 29 | const text = paragraphs[(Math.floor(paragraphs.length / 2) + t) % paragraphs.length].toArray()[0] |
| 30 | const singles = [] |
| 31 | for (let k = 0; k < keys; k++) { |
| 32 | const before = Y.encodeStateVector(doc) |
| 33 | text.insert(10 + k, 'Q') |
| 34 | singles.push(Y.encodeStateAsUpdate(doc, before)) |
| 35 | } |
| 36 | const parts = [] |
| 37 | for (let lo = 0; lo < keys; lo++) { |
| 38 | for (let hi = lo; hi < Math.min(keys, lo + window); hi++) { |
| 39 | const bytes = hi === lo ? singles[lo] : Y.mergeUpdates(singles.slice(lo, hi + 1)) |
| 40 | const head = Buffer.alloc(12) |
| 41 | head.writeUInt32LE(lo, 0); head.writeUInt32LE(hi, 4); head.writeUInt32LE(bytes.length, 8) |
| 42 | parts.push(head, Buffer.from(bytes)) |
| 43 | } |
| 44 | } |
| 45 | writeFileSync(path.join(outdir, `t${t}.bin`), Buffer.concat(parts)) |
| 46 | } |
| 47 | console.log(JSON.stringify({ typists, keys, window, outdir })) |
| 48 | |