// A Redis twin's own account of its service, from its own clock: what a command costs from its bytes' arrival to its // reply, and which part of the twin holds that time. One line every two seconds while commands are served, none // while there are none. It is read to name where a slow request's time is, which a CPU profile's seconds are not // (a profile credits the gap between two samples to the later one). // // A stream serves one command at a time (stream.ts `ordered`), so one command is open at a time and a phase belongs // to it. The phases: `image` (the keyspace's image, with the tree's stamp), `execute` (the command itself), `flush` // (its writes: the append and its locks), `reply` (the write of the answer to the connection), and `context`, which // is the rest of the pack's run around those three (its fetch, the World's clock, the request's context). `other` // is what is left of the command's own time: the stream's own work and its waits between those. type Phase = 'image' | 'execute' | 'flush' | 'reply' | 'run'; const PHASES: readonly Phase[] = ['image', 'execute', 'flush', 'reply', 'run']; const SAY_EVERY_MS = 2000; const now = (): number => performance.now(); type Command = { session: number; command: string }; type Slowest = Command & { threw: boolean; serviceMs: number; waitedMs: number; ownMs: number }; type Window = { service: number[]; waited: number; waitedMax: number; own: number; phases: Record; runs: number; queueMax: number; writes: number; laps: Record; slowest?: Slowest }; const fresh = (): Window => ({ service: [], waited: 0, waitedMax: 0, own: 0, phases: { image: 0, execute: 0, flush: 0, reply: 0, run: 0 }, runs: 0, queueMax: 0, writes: 0, laps: {} }); // THE PARTS OF ONE RUN, in the order it passes them: a run is the pack's own request to itself, carried through the // whole of a pack's fetch (pack-fetch.ts) to the command and back. Each `lap` names the stretch that ended there, // from the lap before it; the stretches of a window's runs are summed by name as `runPartsMs`. A stretch holds // whatever else this realm did between its two ends: under traffic that is other connections' bytes arriving. let lapAt = 0; let window = fresh(); let open: { received: number; started: number; command: Command } | undefined; let timer: ReturnType | undefined; let served = 0; function say(): void { timer = undefined; const w = window; window = fresh(); if (w.service.length === 0) return; const sorted = [...w.service].sort((a, b) => a - b); const at = (share: number): number => sorted[Math.min(sorted.length - 1, Math.floor(sorted.length * share))]!; const ms = (value: number): number => Math.round(value * 10) / 10; const inRun = w.phases.image + w.phases.execute + w.phases.flush; console.error('[redis-twin]', JSON.stringify({ commands: w.service.length, servedSoFar: served, runs: w.runs, wrote: w.writes, // from a command's bytes read to its reply written, its wait behind the commands before it included serviceMs: { total: ms(w.service.reduce((sum, each) => sum + each, 0)), p50: ms(at(0.5)), p95: ms(at(0.95)), max: ms(sorted[sorted.length - 1]!) }, waitedMs: { total: ms(w.waited), max: ms(w.waitedMax) }, phasesMs: { context: ms(w.phases.run - inRun), image: ms(w.phases.image), execute: ms(w.phases.execute), flush: ms(w.phases.flush), reply: ms(w.phases.reply), other: ms(w.own - w.phases.run - w.phases.reply) }, runPartsMs: Object.fromEntries(Object.entries(w.laps).map(([name, value]) => [name, ms(value)])), queueMax: w.queueMax, slowest: w.slowest ? { ...w.slowest, serviceMs: ms(w.slowest.serviceMs), waitedMs: ms(w.slowest.waitedMs), ownMs: ms(w.slowest.ownMs) } : null, })); } export const redisServiceTimes = { now, /** A command starts: `received` is when its bytes were read, `waiting` the commands read and not yet started. */ begin(received: number, waiting: number, command: Command): void { open = { received, started: now(), command }; if (waiting > window.queueMax) window.queueMax = waiting; if (!timer) { timer = setTimeout(say, SAY_EVERY_MS); (timer as { unref?: () => void }).unref?.(); } }, /** Time of the open command in one phase. */ add(phase: Phase, ms: number): void { if (open) { window.phases[phase] += ms; if (phase === 'run') window.runs += 1; } }, wrote(): void { if (open) window.writes += 1; }, /** A run begins: its first stretch is counted from here. */ lapFrom(): void { lapAt = now(); }, /** The stretch of the open command's run that ends here, by name. Outside a served command it is nothing. */ lap(name: string): void { if (!open || lapAt === 0) return; const at = now(); window.laps[name] = (window.laps[name] ?? 0) + (at - lapAt); lapAt = at; }, /** A run has ended: nothing after it is a stretch of it. */ lapEnd(name: string): void { this.lap(name); lapAt = 0; }, /** The open command has answered (or blocked, or failed with its error answered). */ end(threw = false): void { if (!open) return; const ended = now(), waited = open.started - open.received; const serviceMs = ended - open.received; if (!window.slowest || serviceMs > window.slowest.serviceMs) { window.slowest = { ...open.command, threw, serviceMs, waitedMs: waited, ownMs: ended - open.started }; } window.service.push(ended - open.received); window.waited += waited; if (waited > window.waitedMax) window.waitedMax = waited; window.own += ended - open.started; served += 1; open = undefined; }, /** `work`, its time counted in `phase` of the open command. */ timed(phase: Phase, work: () => T): T { const began = now(); try { return work(); } finally { this.add(phase, now() - began); } }, phases: PHASES, };