()
| 291 | } |
| 292 | |
| 293 | function perfSnapshot(): NodeBackendPerfSnapshot { |
| 294 | const byPhase = new Map<string, number[]>(); |
| 295 | for (const s of perfSamples) { |
| 296 | let arr = byPhase.get(s.phase); |
| 297 | if (!arr) { |
| 298 | arr = []; |
| 299 | byPhase.set(s.phase, arr); |
| 300 | } |
| 301 | arr.push(s.durationMs); |
| 302 | } |
| 303 | const phases: Record< |
| 304 | string, |
| 305 | { |
| 306 | count: number; |
| 307 | avg: number; |
| 308 | p50: number; |
| 309 | p95: number; |
| 310 | p99: number; |
| 311 | max: number; |
| 312 | worst10: readonly number[]; |
| 313 | } |
| 314 | > = {}; |
| 315 | for (const [phase, samples] of byPhase) { |
| 316 | const sorted = [...samples].sort((a, b) => a - b); |
| 317 | const sum = sorted.reduce((acc, v) => acc + v, 0); |
| 318 | const p50Idx = Math.min(sorted.length - 1, Math.floor(sorted.length * 0.5)); |
| 319 | const p95Idx = Math.min(sorted.length - 1, Math.floor(sorted.length * 0.95)); |
| 320 | const p99Idx = Math.min(sorted.length - 1, Math.floor(sorted.length * 0.99)); |
| 321 | const worst10Start = Math.max(0, sorted.length - 10); |
| 322 | const worst10 = sorted.slice(worst10Start).reverse(); |
| 323 | phases[phase] = { |
| 324 | count: sorted.length, |
| 325 | avg: sorted.length > 0 ? sum / sorted.length : 0, |
| 326 | p50: sorted[p50Idx] ?? 0, |
| 327 | p95: sorted[p95Idx] ?? 0, |
| 328 | p99: sorted[p99Idx] ?? 0, |
| 329 | max: sorted[sorted.length - 1] ?? 0, |
| 330 | worst10, |
| 331 | }; |
| 332 | } |
| 333 | return { phases }; |
| 334 | } |
| 335 | |
| 336 | function rejectWaiters(err: Error): void { |
| 337 | while (eventWaiters.length > 0) eventWaiters.shift()?.reject(err); |
no test coverage detected