// Docs live push: timing through the real CARLOS edge (spec // 2026-11-26-live-push-design.md, ยง1 and question 4). Paste the whole of this // into the browser console while a SCRATCH document is open at // https://.docs.oficina.works/d/. Your session never leaves your // browser: it only makes same-origin requests from that page. // // What it does, in about ten seconds: // 1. opens a second live stream for this document and times its first event; // 2. three empty fetches (GET /updates?since=): the fetch round trip; // 3. three empty updates (POST /updates with the two bytes 00 00, a valid Yjs // update that changes nothing): the time to the server's answer, and the // time until this stream announces it. If the edge buffered the stream, // the second would be far behind the first. // It changes nothing you can see. Each empty update adds one row to this // document's history, so use a scratch document and delete it afterwards. // On a server with live push it also reports the push stream's own frame. (async () => { const m = location.pathname.match(/^\/d\/([^/]+)\/?$/) if (!m) { console.log('Open the scratch document first (a /d/... page).'); return } const base = `/d/${m[1]}` const now = () => performance.now() const sleep = (ms) => new Promise((r) => setTimeout(r, ms)) const median = (xs) => { const s = [...xs].sort((a, b) => a - b); return s.length ? s[Math.floor(s.length / 2)] : NaN } const r1 = (x) => Math.round(x * 10) / 10 const events = [] // { seq, t, kind }, every announcement in arrival order const waiters = [] let streamID = null, current = null, pushServer = false const t0 = now() const es = new EventSource(`${base}/stream?push=1`, { withCredentials: true }) const seen = (seq, kind) => { const ev = { seq, t: now(), kind } events.push(ev) for (const w of waiters.splice(0)) if (seq >= w.seq) w.resolve(ev); else waiters.push(w) } // The announcement of seq: already here (it can beat the POST's answer), or the next one. const announcement = (seq, after) => { const ev = events.find((e) => e.seq >= seq && e.t >= after) return ev ? Promise.resolve(ev) : new Promise((resolve) => waiters.push({ seq, resolve })) } const first = new Promise((resolve, reject) => { es.addEventListener('hello', (e) => { pushServer = true; const [id, seq] = e.data.split(' '); streamID = id; current = Number(seq); resolve(now() - t0) }) es.addEventListener('update', (e) => { const seq = Number(e.data); if (current === null) { current = seq; resolve(now() - t0) } else seen(seq, 'update') }) es.addEventListener('push', (e) => seen(Number(e.data.split(' ')[0]), 'push')) es.addEventListener('own', (e) => seen(Number(e.data.split(' ')[0]), 'own')) es.onerror = () => reject(new Error('the stream failed to open')) setTimeout(() => reject(new Error('no stream event within 10 s')), 10000) }) const result = { page: location.host, protocol: null } try { result.streamFirstEventMs = r1(await first) result.pushServer = pushServer const gets = [] for (let i = 0; i < 3; i++) { const s = now() const r = await fetch(`${base}/updates?since=${current}`, { credentials: 'same-origin', cache: 'no-store' }) await r.arrayBuffer() gets.push(now() - s) await sleep(300) } result.fetchRoundTripMs = gets.map(r1) const entry = performance.getEntriesByType('resource').filter((e) => e.name.includes(`${base}/updates?since=`)).pop() result.protocol = entry && entry.nextHopProtocol ? entry.nextHopProtocol : 'unknown' const answers = [], announced = [], kinds = [] for (let i = 0; i < 3; i++) { const headers = { 'Content-Type': 'application/octet-stream' } if (streamID) headers['Docs-Stream'] = `${streamID}.${i + 1}` const s = now() const r = await fetch(`${base}/updates`, { method: 'POST', credentials: 'same-origin', headers, body: new Uint8Array([0, 0]) }) const answered = now() if (!r.ok) throw new Error(`POST answered ${r.status}`) const seq = Number(r.headers.get('Docs-Seq')) const ev = await Promise.race([announcement(seq, s), sleep(5000).then(() => null)]) answers.push(answered - s) announced.push(ev ? ev.t - s : NaN) kinds.push(ev ? ev.kind : 'none within 5 s') await sleep(500) } result.postAnswerMs = answers.map(r1) result.postToStreamEventMs = announced.map(r1) result.streamEventKinds = kinds result.summary = `fetch RTT ~${r1(median(gets))} ms; upload answered ~${r1(median(answers))} ms; ` + `stream announced it ~${r1(median(announced))} ms after sending ` + `(${r1(median(announced) - median(answers))} ms after the answer; near 0 or negative means the edge does not buffer the stream)` } catch (err) { result.error = String(err) } finally { es.close() } console.log('DOCS EDGE TIMING ' + JSON.stringify(result, null, 2)) return result })()