Bench Listener

index.jsx 18 lines

Files · 2
123456789101112131415161718// Latency benchmark listener. For each 'bench-in' event it records
//   t1: handler invoked
//   t2: handler settled, i.e. after the handler's work (one awaited kv write)
// using performance.timeOrigin + performance.now(), plus the page's time
// origin and script start (to break down cold starts). t2 is stored by a
// second, bookkeeping write that is not part of the measured interval.
const now = () => performance.timeOrigin + performance.now();
const scriptAt = now();

window.lattice.events.listen('bench-in', async (data) => {
  const t1 = now();
  await window.lattice.kv.set('r:' + data.seq, { t0: data.t0, t1, page_origin: performance.timeOrigin, script_at: scriptAt });
  const t2 = now();
  await window.lattice.kv.set('s:' + data.seq, t2);
});

window.lattice.kv.set('clock:' + Math.round(performance.timeOrigin), { page_origin: performance.timeOrigin, script_at: scriptAt, skew_ms: now() - Date.now() });
document.body.textContent = 'bench listener ready';