mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-15 11:22:15 +03:00
* feat(perf): add performance.mark/measure to SSE pipeline + request-size metric
- streamingPipeline.ts: mark/measure around assembly of SSE transform
chain — 'omni-pipeline-start'/'omni-pipeline-end'/'omni-pipeline'
- stream.ts: compute JSON body byte count on stream creation, emit as
performance.mark('omni-request-body-size', { detail: bytes })
Marks are visible via performance.getEntriesByType('mark') and
performance.getEntriesByType('measure') for DevTools/monitoring.
* fix(perf): prevent memory leak and TextEncoder allocation on hot path
- Add performance.clearMarks/clearMeasures before creating new marks to
prevent timeline accumulation in long-lived processes.
- Replace new TextEncoder().encode(str).length with Buffer.byteLength to
avoid allocating a full Uint8Array just to measure byte length.
* test(perf): add performance instrumentation tests
* chore(ci): rebaseline stream.ts 2796->2805 for perf instrumentation
Add _rebaseline_ entry documenting the +9 line growth from:
- b48ba21c4: performance.mark/measure instrumentation around SSE dispatch
- c35e8a9b4: TextEncoder hoisting fix
These are irreducible instrumentations at the stream dispatch chokepoint.
* chore: trigger CI re-run
* fix(ci): restore file-size-baseline.json corrupted by prior rebaseline commit
The rebaseline commit (9efdd636d) accidentally replaced the entire
config/quality/file-size-baseline.json with the literal string
"test content" instead of adding the intended stream.ts entry,
breaking JSON.parse() in check:file-size for every subsequent CI run.
Restore the full baseline from origin/release/v3.8.49 and apply the
intended bump: open-sse/utils/stream.ts 2796->2806 (measured LOC,
matching the script's countLines() split("\n").length, not wc -l)
for the performance.mark/measure instrumentation + TextEncoder
hoisting fix added by this PR.
Co-authored-by: diegosouzapw <diegosouza.pw@gmail.com>
* fix(perf): clear the omni-request-body-size mark immediately after creation
Addresses review feedback on this PR: the fixed-name "omni-request-body-size"
performance mark was created on every createSSEStream() call and never
cleared, so it accumulated without bound in Node's global performance
timeline over a long-running server's lifetime (unlike the pipeline-assembly
marks in streamingPipeline.ts, which are bounded — cleared at the start of
the next call). A wired PerformanceObserver still receives the entry;
clearMarks() only removes it from getEntriesByName()/getEntriesByType().
Rebaseline file-size-baseline.json to the actual measured LOC (2796->2813)
for the comment + clear call, and add
tests/unit/stream-request-body-size-mark-7045.test.ts covering: the mark
fires with the correct JSON-byte-length detail, it does not accumulate
across repeated calls, and it is skipped when there is no request body.
Co-authored-by: diegosouzapw <diegosouza.pw@gmail.com>
* fix(ci): correct file-size-baseline.json off-by-one for stream.ts
check-file-size.mjs counts lines via fs.readFileSync().split("\n").length,
which is wc -l + 1 for a file ending in a trailing newline (the last split
element is an empty string after the final newline). The previous commit
baselined the wc -l value (2813) instead of the script's own metric (2814),
so CI still failed by exactly 1 line.
Co-authored-by: diegosouzapw <diegosouza.pw@gmail.com>
---------
Co-authored-by: oyi77 <oyi77@users.noreply.github.com>
Co-authored-by: diegosouzapw <diegosouza.pw@gmail.com>
123 lines
4.0 KiB
TypeScript
123 lines
4.0 KiB
TypeScript
// PR #7045 (@oyi77, performance.mark/measure SSE instrumentation) regression coverage +
|
|
// fix for a review-flagged leak: the "omni-request-body-size" mark is created on every
|
|
// createSSEStream() call with a fixed name and, before this fix, was never cleared —
|
|
// each call added another entry to Node's global performance timeline, unbounded over a
|
|
// long-running server's lifetime. It must now be observable (via PerformanceObserver,
|
|
// which still fires synchronously) yet cleared from the buffer immediately after.
|
|
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { performance } from "node:perf_hooks";
|
|
|
|
const TEST_DATA_DIR = fs.mkdtempSync(
|
|
path.join(os.tmpdir(), "omniroute-stream-body-size-mark-")
|
|
);
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
const core = await import("../../src/lib/db/core.ts");
|
|
|
|
const { createSSEStream } = await import("../../open-sse/utils/stream.ts");
|
|
const { FORMATS } = await import("../../open-sse/translator/formats.ts");
|
|
|
|
const textEncoder = new TextEncoder();
|
|
|
|
async function drainSSEStream(options) {
|
|
const source = new ReadableStream({
|
|
start(controller) {
|
|
controller.enqueue(
|
|
textEncoder.encode(
|
|
`data: ${JSON.stringify({
|
|
id: "chatcmpl_bodysize",
|
|
object: "chat.completion.chunk",
|
|
created: 1,
|
|
model: "gpt-4.1-mini",
|
|
choices: [{ index: 0, delta: { role: "assistant", content: "hi" } }],
|
|
})}\n\n`
|
|
)
|
|
);
|
|
controller.enqueue(
|
|
textEncoder.encode(
|
|
`data: ${JSON.stringify({
|
|
id: "chatcmpl_bodysize",
|
|
object: "chat.completion.chunk",
|
|
created: 1,
|
|
model: "gpt-4.1-mini",
|
|
choices: [{ index: 0, delta: {}, finish_reason: "stop" }],
|
|
})}\n\n`
|
|
)
|
|
);
|
|
controller.close();
|
|
},
|
|
});
|
|
return new Response(source.pipeThrough(createSSEStream(options))).text();
|
|
}
|
|
|
|
test.after(() => {
|
|
core.resetDbInstance();
|
|
if (fs.existsSync(TEST_DATA_DIR)) {
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("createSSEStream emits a request-body-size mark with the JSON byte length as detail", async (t) => {
|
|
const observedMarks = [];
|
|
const originalMark = performance.mark.bind(performance);
|
|
t.mock.method(performance, "mark", (name, options) => {
|
|
if (name === "omni-request-body-size") observedMarks.push(options?.detail);
|
|
return originalMark(name, options);
|
|
});
|
|
|
|
const body = { messages: [{ role: "user", content: "hello world" }] };
|
|
const expectedBytes = Buffer.byteLength(JSON.stringify(body), "utf8");
|
|
|
|
await drainSSEStream({
|
|
mode: "passthrough",
|
|
sourceFormat: FORMATS.OPENAI,
|
|
provider: "openai",
|
|
model: "gpt-4.1-mini",
|
|
body,
|
|
});
|
|
|
|
assert.equal(observedMarks.length, 1, "mark() should have been called exactly once");
|
|
assert.equal(observedMarks[0], expectedBytes, "mark detail should be the JSON byte length");
|
|
});
|
|
|
|
test("createSSEStream clears the request-body-size mark immediately (no unbounded growth)", async () => {
|
|
performance.clearMarks("omni-request-body-size");
|
|
|
|
const body = { messages: [{ role: "user", content: "hello" }] };
|
|
for (let i = 0; i < 5; i++) {
|
|
await drainSSEStream({
|
|
mode: "passthrough",
|
|
sourceFormat: FORMATS.OPENAI,
|
|
provider: "openai",
|
|
model: "gpt-4.1-mini",
|
|
body,
|
|
});
|
|
}
|
|
|
|
assert.equal(
|
|
performance.getEntriesByName("omni-request-body-size").length,
|
|
0,
|
|
"the mark must not accumulate across repeated calls"
|
|
);
|
|
});
|
|
|
|
test("createSSEStream skips the mark when body is absent (bodySize stays 0)", async () => {
|
|
performance.clearMarks("omni-request-body-size");
|
|
|
|
await drainSSEStream({
|
|
mode: "passthrough",
|
|
sourceFormat: FORMATS.OPENAI,
|
|
provider: "openai",
|
|
model: "gpt-4.1-mini",
|
|
});
|
|
|
|
assert.equal(
|
|
performance.getEntriesByName("omni-request-body-size").length,
|
|
0,
|
|
"no mark expected when there is no request body"
|
|
);
|
|
});
|