|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { performance } from 'node:perf_hooks'; |
| 3 | + |
| 4 | +const memory = () => { |
| 5 | + const { rss, heapUsed, external, arrayBuffers } = process.memoryUsage(); |
| 6 | + return { rss, heapUsed, external, arrayBuffers }; |
| 7 | +}; |
| 8 | +type Memory = ReturnType<typeof memory>; |
| 9 | +const fields = ['rss', 'heapUsed', 'external', 'arrayBuffers'] as const; |
| 10 | +const delta = (end: Memory, start: Memory) => |
| 11 | + Object.fromEntries(fields.map((field) => [field, end[field] - start[field]])); |
| 12 | + |
| 13 | +/** Local fixture instrumentation only; production calls are not patched. */ |
| 14 | +export function profileSseMemory(run: string) { |
| 15 | + const gc = global.gc; |
| 16 | + assert.ok(gc, 'Run the benchmark with node --expose-gc for --profile-memory'); |
| 17 | + gc(); |
| 18 | + const baseline = memory(); |
| 19 | + const peak = { ...baseline }; |
| 20 | + const sample = () => { |
| 21 | + const current = memory(); |
| 22 | + for (const field of fields) peak[field] = Math.max(peak[field], current[field]); |
| 23 | + }; |
| 24 | + const timer = setInterval(sample, 10); |
| 25 | + const stringify = JSON.stringify; |
| 26 | + const from = Buffer.from; |
| 27 | + let stringifyMs = 0; |
| 28 | + let serializedEvents = 0; |
| 29 | + let fullFrameBufferMs = 0; |
| 30 | + let fullFrameBufferBytes = 0; |
| 31 | + let writeCalls = 0; |
| 32 | + let writeMs = 0; |
| 33 | + JSON.stringify = ((value: unknown, ...rest: unknown[]) => { |
| 34 | + if (!value || typeof value !== 'object' || !('runId' in value) || value.runId !== run) |
| 35 | + return Reflect.apply(stringify, JSON, [value, ...rest]); |
| 36 | + const at = performance.now(); |
| 37 | + const result = Reflect.apply(stringify, JSON, [value, ...rest]); |
| 38 | + stringifyMs += performance.now() - at; |
| 39 | + serializedEvents++; |
| 40 | + return result; |
| 41 | + }) as typeof JSON.stringify; |
| 42 | + Buffer.from = ((value: unknown, ...rest: unknown[]) => { |
| 43 | + if (typeof value !== 'string' || value.length < 128 * 1024 || !value.startsWith('id: ')) |
| 44 | + return Reflect.apply(from, Buffer, [value, ...rest]); |
| 45 | + const at = performance.now(); |
| 46 | + const result = Reflect.apply(from, Buffer, [value, ...rest]); |
| 47 | + fullFrameBufferMs += performance.now() - at; |
| 48 | + fullFrameBufferBytes += result.byteLength; |
| 49 | + return result; |
| 50 | + }) as typeof Buffer.from; |
| 51 | + const dispose = () => { |
| 52 | + clearInterval(timer); |
| 53 | + JSON.stringify = stringify; |
| 54 | + Buffer.from = from; |
| 55 | + }; |
| 56 | + return { |
| 57 | + observeWrite(ms: number) { |
| 58 | + writeCalls++; |
| 59 | + writeMs += ms; |
| 60 | + }, |
| 61 | + finish() { |
| 62 | + sample(); |
| 63 | + dispose(); |
| 64 | + gc(); |
| 65 | + return { |
| 66 | + sampled_peak_delta_bytes: delta(peak, baseline), |
| 67 | + paused_after_gc_delta_bytes: delta(memory(), baseline), |
| 68 | + encoding: { |
| 69 | + serialized_events: serializedEvents, |
| 70 | + stringify_ms: stringifyMs, |
| 71 | + full_frame_buffer_ms: fullFrameBufferMs, |
| 72 | + full_frame_buffer_bytes: fullFrameBufferBytes, |
| 73 | + write_calls: writeCalls, |
| 74 | + write_ms: writeMs, |
| 75 | + }, |
| 76 | + }; |
| 77 | + }, |
| 78 | + dispose, |
| 79 | + }; |
| 80 | +} |
0 commit comments