mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-22 07:02:16 +03:00
Obrigado — feature substancial e bem estruturada: separa qualidade operacional (comportamento de wire: 4xx/5xx, 429, respostas malformadas, stream interrompido) de qualidade semântica (só setada por avaliadores externos, nunca inferida do sucesso HTTP), com confidence/sample-awareness para não deixar poucos sucessos de sorte dominarem o ranking. Instrumentação de streaming (TTFT/ITL) threaded até RoutingEvent, endpoint de explicabilidade, e teste E2E determinístico cobrindo degradação→recuperação→blip. Validação (worktree própria a partir de origin/release/v3.8.50, merge limpo, 0 conflitos): - typecheck:core limpo, complexity/cognitive-complexity dentro do baseline - 59/59 testes passando (mlx-provider, routing-adaptive-e2e, routing-events(-concurrency), routing-otel, routing-quality, routing-scoring-quality, stream-timing, auto-combo-scoring-clamp)
87 lines
3.1 KiB
TypeScript
87 lines
3.1 KiB
TypeScript
/**
|
|
* tests/unit/stream-timing.test.ts
|
|
*
|
|
* Canonical stream instrumentation (open-sse/utils/streamTiming.ts):
|
|
* - TTFT = first-forwarded-SSE-chunk latency (NOT token-level) — documented
|
|
* - ITL = mean inter-chunk gap (chunk-latency proxy)
|
|
* - first-byte vs first-forward distinction
|
|
* - interruption marking
|
|
* - malformed/empty chunks do not corrupt timing
|
|
*/
|
|
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { createStreamTiming, type StreamTiming } from "../../open-sse/utils/streamTiming.ts";
|
|
|
|
test("ttft() is null when nothing was forwarded", () => {
|
|
const t = createStreamTiming();
|
|
t.markByte();
|
|
assert.equal(t.ttftMs(), null);
|
|
assert.equal(t.avgItlMs(), null);
|
|
});
|
|
|
|
test("ttft() measures first-forwarded-chunk latency (byte vs forward distinguished)", async () => {
|
|
const t = createStreamTiming();
|
|
t.markByte(); // first upstream byte arrives immediately
|
|
await new Promise((r) => setTimeout(r, 20));
|
|
t.markForward(); // first chunk forwarded 20ms later
|
|
const ttft = t.ttftMs();
|
|
assert.ok(ttft !== null && ttft >= 20 && ttft < 5000, `ttft=${ttft}`);
|
|
assert.ok(t.firstByteAt !== null);
|
|
assert.ok(t.firstByteAt! < t.firstForwardAt!, "first byte precedes first forward");
|
|
});
|
|
|
|
test("avgItlMs() measures mean inter-chunk gap across multiple chunks", async () => {
|
|
const t = createStreamTiming();
|
|
for (let i = 0; i < 4; i++) {
|
|
t.markForward();
|
|
await new Promise((r) => setTimeout(r, 10));
|
|
}
|
|
const itl = t.avgItlMs();
|
|
assert.ok(itl !== null && itl >= 8 && itl < 5000, `itl=${itl}`);
|
|
assert.equal(t.forwardedChunks, 4);
|
|
});
|
|
|
|
test("empty chunks do not corrupt timing (markByte without forward)", () => {
|
|
const t = createStreamTiming();
|
|
t.markByte();
|
|
t.markByte(); // duplicate bytes are idempotent for first-byte
|
|
assert.equal(t.ttftMs(), null, "no forward → no ttft");
|
|
t.markForward();
|
|
assert.ok(t.ttftMs() !== null);
|
|
});
|
|
|
|
test("malformed/keepalive-only traffic (no forward) yields no ttft", () => {
|
|
const t = createStreamTiming();
|
|
// Simulate a provider that only sends keepalives/blank lines, never data.
|
|
for (let i = 0; i < 5; i++) t.markByte();
|
|
assert.equal(t.ttftMs(), null);
|
|
assert.equal(t.forwardedChunks, 0);
|
|
});
|
|
|
|
test("interruption is recorded and does not reset other timing", async () => {
|
|
const t = createStreamTiming();
|
|
t.markForward();
|
|
await new Promise((r) => setTimeout(r, 5));
|
|
t.markForward();
|
|
t.markInterrupted();
|
|
assert.equal(t.interrupted, true);
|
|
assert.ok(t.ttftMs() !== null);
|
|
assert.ok(t.avgItlMs() !== null);
|
|
});
|
|
|
|
test("normal completion: totalMs() is monotonic and >= first-forward latency", async () => {
|
|
const t = createStreamTiming();
|
|
await new Promise((r) => setTimeout(r, 15));
|
|
t.markForward();
|
|
const total = t.totalMs();
|
|
const ttft = t.ttftMs();
|
|
assert.ok(total >= 15);
|
|
assert.ok(ttft !== null && ttft <= total, "ttft must be <= total duration");
|
|
});
|
|
|
|
test("max inter-chunk samples are bounded (memory bound)", async () => {
|
|
const t = createStreamTiming();
|
|
for (let i = 0; i < 200; i++) t.markForward();
|
|
assert.ok(t.interChunkGaps.length <= 32, `bounded to 32 samples, got ${t.interChunkGaps.length}`);
|
|
});
|