Spaces:
Sleeping
Sleeping
fix(arena): measure TTFT from browser
Browse filesExpose browser-observed TTFT as the public value, retain internal TTFT as an explicit diagnostic, and label client location as network exit. Five-call smoothing remains intentionally out of scope pending client approval.
- index.html +42 -15
index.html
CHANGED
|
@@ -1702,7 +1702,7 @@ const client = new OpenAI({
|
|
| 1702 |
}
|
| 1703 |
|
| 1704 |
function shareProvenanceLabel() {
|
| 1705 |
-
return `
|
| 1706 |
}
|
| 1707 |
|
| 1708 |
function shareFooterMetadata() {
|
|
@@ -2044,7 +2044,11 @@ const client = new OpenAI({
|
|
| 2044 |
const rows = [
|
| 2045 |
metric("Live provider", displayProviderName(provider)),
|
| 2046 |
metric("Live model", metrics.model_label || metrics.model || "--"),
|
| 2047 |
-
metric("
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2048 |
metric("Live latency", formatMs(metrics.latency_ms)),
|
| 2049 |
metric("Live tokens/s", formatNumber(metrics.tokens_per_second)),
|
| 2050 |
metric(
|
|
@@ -2135,8 +2139,8 @@ const client = new OpenAI({
|
|
| 2135 |
"cost unavailable",
|
| 2136 |
);
|
| 2137 |
const ttft = ratioPhrase(
|
| 2138 |
-
state.sovapi.
|
| 2139 |
-
state.challenger.
|
| 2140 |
"faster",
|
| 2141 |
"slower",
|
| 2142 |
"same speed",
|
|
@@ -2145,7 +2149,7 @@ const client = new OpenAI({
|
|
| 2145 |
return {
|
| 2146 |
headline: `TTFT · SovAPI ${ttft}`,
|
| 2147 |
ttft: `Total time ${speed} · ${cost}`,
|
| 2148 |
-
detail: "
|
| 2149 |
};
|
| 2150 |
}
|
| 2151 |
|
|
@@ -2248,6 +2252,10 @@ const client = new OpenAI({
|
|
| 2248 |
}
|
| 2249 |
|
| 2250 |
async function runSideAttempt(side, body, base) {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2251 |
const response = await fetchWithTimeout(`${base}/v1/challenge`, {
|
| 2252 |
method: "POST",
|
| 2253 |
headers: {
|
|
@@ -2260,7 +2268,7 @@ const client = new OpenAI({
|
|
| 2260 |
throw await challengeResponseError(response, body.side);
|
| 2261 |
}
|
| 2262 |
if (!response.body || typeof response.body.getReader !== "function") {
|
| 2263 |
-
consumeSseText(side, await response.text(), true);
|
| 2264 |
if (!state[side]) throw retryableError(`Challenge ${side} completed without metrics`);
|
| 2265 |
return;
|
| 2266 |
}
|
|
@@ -2270,17 +2278,22 @@ const client = new OpenAI({
|
|
| 2270 |
while (true) {
|
| 2271 |
const { done, value } = await reader.read();
|
| 2272 |
if (done) break;
|
| 2273 |
-
buffer = consumeSseText(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2274 |
}
|
| 2275 |
const trailing = decoder.decode();
|
| 2276 |
-
consumeSseText(side, buffer + trailing, true);
|
| 2277 |
if (!state[side]) throw retryableError(`Challenge ${side} completed without metrics`);
|
| 2278 |
}
|
| 2279 |
|
| 2280 |
-
function consumeSseText(side, text, flush = false) {
|
| 2281 |
const parts = text.replace(/\r\n/g, "\n").split("\n\n");
|
| 2282 |
const events = flush ? parts : parts.slice(0, -1);
|
| 2283 |
-
events.forEach((eventText) => handleEvent(side, eventText));
|
| 2284 |
return flush ? "" : parts[parts.length - 1] || "";
|
| 2285 |
}
|
| 2286 |
|
|
@@ -2308,7 +2321,7 @@ const client = new OpenAI({
|
|
| 2308 |
}
|
| 2309 |
}
|
| 2310 |
|
| 2311 |
-
function handleEvent(side, eventText) {
|
| 2312 |
if (eventText.includes("data: [DONE]")) return;
|
| 2313 |
const eventLine = eventText.split("\n").find((line) => line.startsWith("event:"));
|
| 2314 |
const eventName = eventLine?.replace("event:", "").trim() || "message";
|
|
@@ -2328,9 +2341,16 @@ const client = new OpenAI({
|
|
| 2328 |
}
|
| 2329 |
if (eventName === "challenge.metrics") {
|
| 2330 |
clearSideStarting(side);
|
| 2331 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2332 |
formatStructureOutputForDisplay(side);
|
| 2333 |
-
renderMetrics(
|
|
|
|
|
|
|
|
|
|
| 2334 |
if (payload.side === "challenger") {
|
| 2335 |
qs("#quotaText").textContent = `${formatNumber(payload.remaining_tokens)} Challenger tokens left in this duel.`;
|
| 2336 |
}
|
|
@@ -2338,8 +2358,15 @@ const client = new OpenAI({
|
|
| 2338 |
return;
|
| 2339 |
}
|
| 2340 |
const output = side === "sovapi" ? qs("#sovapiOutput") : qs("#challengerOutput");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2341 |
clearSideStarting(side);
|
| 2342 |
-
output.textContent +=
|
| 2343 |
}
|
| 2344 |
|
| 2345 |
async function settleRun(promise) {
|
|
@@ -2559,7 +2586,7 @@ const client = new OpenAI({
|
|
| 2559 |
const rows = [
|
| 2560 |
["Terrain", terrainName],
|
| 2561 |
["Model", modelName || metrics?.model_label || metrics?.model || "--"],
|
| 2562 |
-
["TTFT", formatMs(metrics?.
|
| 2563 |
["Throughput", `${formatNumber(metrics?.tokens_per_second)} tok/s`],
|
| 2564 |
["Tokens", `${formatNumber(metrics?.input_tokens)} in / ${formatNumber(metrics?.output_tokens)} out`],
|
| 2565 |
["Cost", formatCostUsd(metrics?.cost_usd)],
|
|
|
|
| 1702 |
}
|
| 1703 |
|
| 1704 |
function shareProvenanceLabel() {
|
| 1705 |
+
return `Network exit: ${ARENA_ORIGIN_CITY} → ${state.locationCity || "—"}`;
|
| 1706 |
}
|
| 1707 |
|
| 1708 |
function shareFooterMetadata() {
|
|
|
|
| 2044 |
const rows = [
|
| 2045 |
metric("Live provider", displayProviderName(provider)),
|
| 2046 |
metric("Live model", metrics.model_label || metrics.model || "--"),
|
| 2047 |
+
metric("Browser TTFT", formatMs(metrics.browser_time_to_first_text_ms)),
|
| 2048 |
+
metric(
|
| 2049 |
+
"Internal TTFT (client path excluded)",
|
| 2050 |
+
formatMs(metrics.time_to_first_token_ms),
|
| 2051 |
+
),
|
| 2052 |
metric("Live latency", formatMs(metrics.latency_ms)),
|
| 2053 |
metric("Live tokens/s", formatNumber(metrics.tokens_per_second)),
|
| 2054 |
metric(
|
|
|
|
| 2139 |
"cost unavailable",
|
| 2140 |
);
|
| 2141 |
const ttft = ratioPhrase(
|
| 2142 |
+
state.sovapi.browser_time_to_first_text_ms,
|
| 2143 |
+
state.challenger.browser_time_to_first_text_ms,
|
| 2144 |
"faster",
|
| 2145 |
"slower",
|
| 2146 |
"same speed",
|
|
|
|
| 2149 |
return {
|
| 2150 |
headline: `TTFT · SovAPI ${ttft}`,
|
| 2151 |
ttft: `Total time ${speed} · ${cost}`,
|
| 2152 |
+
detail: "Browser TTFT includes the client-to-API path. Internal TTFT is shown separately for diagnostics.",
|
| 2153 |
};
|
| 2154 |
}
|
| 2155 |
|
|
|
|
| 2252 |
}
|
| 2253 |
|
| 2254 |
async function runSideAttempt(side, body, base) {
|
| 2255 |
+
const browserTiming = {
|
| 2256 |
+
requestStartedAtMs: performance.now(),
|
| 2257 |
+
firstTextMs: null,
|
| 2258 |
+
};
|
| 2259 |
const response = await fetchWithTimeout(`${base}/v1/challenge`, {
|
| 2260 |
method: "POST",
|
| 2261 |
headers: {
|
|
|
|
| 2268 |
throw await challengeResponseError(response, body.side);
|
| 2269 |
}
|
| 2270 |
if (!response.body || typeof response.body.getReader !== "function") {
|
| 2271 |
+
consumeSseText(side, await response.text(), true, browserTiming);
|
| 2272 |
if (!state[side]) throw retryableError(`Challenge ${side} completed without metrics`);
|
| 2273 |
return;
|
| 2274 |
}
|
|
|
|
| 2278 |
while (true) {
|
| 2279 |
const { done, value } = await reader.read();
|
| 2280 |
if (done) break;
|
| 2281 |
+
buffer = consumeSseText(
|
| 2282 |
+
side,
|
| 2283 |
+
buffer + decoder.decode(value, { stream: true }),
|
| 2284 |
+
false,
|
| 2285 |
+
browserTiming,
|
| 2286 |
+
);
|
| 2287 |
}
|
| 2288 |
const trailing = decoder.decode();
|
| 2289 |
+
consumeSseText(side, buffer + trailing, true, browserTiming);
|
| 2290 |
if (!state[side]) throw retryableError(`Challenge ${side} completed without metrics`);
|
| 2291 |
}
|
| 2292 |
|
| 2293 |
+
function consumeSseText(side, text, flush = false, browserTiming = null) {
|
| 2294 |
const parts = text.replace(/\r\n/g, "\n").split("\n\n");
|
| 2295 |
const events = flush ? parts : parts.slice(0, -1);
|
| 2296 |
+
events.forEach((eventText) => handleEvent(side, eventText, browserTiming));
|
| 2297 |
return flush ? "" : parts[parts.length - 1] || "";
|
| 2298 |
}
|
| 2299 |
|
|
|
|
| 2321 |
}
|
| 2322 |
}
|
| 2323 |
|
| 2324 |
+
function handleEvent(side, eventText, browserTiming = null) {
|
| 2325 |
if (eventText.includes("data: [DONE]")) return;
|
| 2326 |
const eventLine = eventText.split("\n").find((line) => line.startsWith("event:"));
|
| 2327 |
const eventName = eventLine?.replace("event:", "").trim() || "message";
|
|
|
|
| 2341 |
}
|
| 2342 |
if (eventName === "challenge.metrics") {
|
| 2343 |
clearSideStarting(side);
|
| 2344 |
+
const measuredMetrics = {
|
| 2345 |
+
...payload,
|
| 2346 |
+
browser_time_to_first_text_ms: browserTiming?.firstTextMs ?? null,
|
| 2347 |
+
};
|
| 2348 |
+
state[side] = measuredMetrics;
|
| 2349 |
formatStructureOutputForDisplay(side);
|
| 2350 |
+
renderMetrics(
|
| 2351 |
+
side === "sovapi" ? "#sovapiMetrics" : "#challengerMetrics",
|
| 2352 |
+
measuredMetrics,
|
| 2353 |
+
);
|
| 2354 |
if (payload.side === "challenger") {
|
| 2355 |
qs("#quotaText").textContent = `${formatNumber(payload.remaining_tokens)} Challenger tokens left in this duel.`;
|
| 2356 |
}
|
|
|
|
| 2358 |
return;
|
| 2359 |
}
|
| 2360 |
const output = side === "sovapi" ? qs("#sovapiOutput") : qs("#challengerOutput");
|
| 2361 |
+
const delta = typeof payload.delta === "string" ? payload.delta : "";
|
| 2362 |
+
if (delta && browserTiming?.firstTextMs === null) {
|
| 2363 |
+
browserTiming.firstTextMs = Math.max(
|
| 2364 |
+
0,
|
| 2365 |
+
performance.now() - browserTiming.requestStartedAtMs,
|
| 2366 |
+
);
|
| 2367 |
+
}
|
| 2368 |
clearSideStarting(side);
|
| 2369 |
+
output.textContent += delta;
|
| 2370 |
}
|
| 2371 |
|
| 2372 |
async function settleRun(promise) {
|
|
|
|
| 2586 |
const rows = [
|
| 2587 |
["Terrain", terrainName],
|
| 2588 |
["Model", modelName || metrics?.model_label || metrics?.model || "--"],
|
| 2589 |
+
["TTFT", formatMs(metrics?.browser_time_to_first_text_ms)],
|
| 2590 |
["Throughput", `${formatNumber(metrics?.tokens_per_second)} tok/s`],
|
| 2591 |
["Tokens", `${formatNumber(metrics?.input_tokens)} in / ${formatNumber(metrics?.output_tokens)} out`],
|
| 2592 |
["Cost", formatCostUsd(metrics?.cost_usd)],
|