| 1 | import { performance } from "node:perf_hooks"; |
| 2 | |
| 3 | // Bounded host-side aggregates; no instrumentation or retained history in the app. |
| 4 | export function createTimings(now = () => performance.now()) { |
| 5 | const totals = new Map(); |
| 6 | return { |
| 7 | async measure(name, operation) { |
| 8 | const start = now(); |
| 9 | try { return await operation(); } |
| 10 | finally { |
| 11 | const elapsed = now() - start; |
| 12 | const previous = totals.get(name) ?? { count: 0, totalMs: 0, maxMs: 0 }; |
| 13 | totals.set(name, { count: previous.count + 1, totalMs: previous.totalMs + elapsed, maxMs: Math.max(previous.maxMs, elapsed) }); |
| 14 | } |
| 15 | }, |
| 16 | snapshot() { |
| 17 | return Object.fromEntries([...totals].map(([key, value]) => [key, { |
| 18 | count: value.count, totalMs: Math.round(value.totalMs), maxMs: Math.round(value.maxMs), |
| 19 | }])); |
| 20 | }, |
| 21 | }; |
| 22 | } |
| 23 |