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