Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,135 @@
|
||||
"use strict";
|
||||
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs/promises");
|
||||
const { performance } = require("node:perf_hooks");
|
||||
const { setTimeout: delay } = require("node:timers/promises");
|
||||
const { createRun } = require("../public/js/agent-live");
|
||||
|
||||
const ACCOUNT_COUNT = 300;
|
||||
const SAMPLE_MS = 250;
|
||||
const WORKLOAD_TIMEOUT_MS = 215_000;
|
||||
const fixtureImage = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aU1cAAAAASUVORK5CYII=";
|
||||
|
||||
function percentiles(samples) {
|
||||
if (!samples.length) return null;
|
||||
const sorted = [...samples].sort((left, right) => left - right);
|
||||
const percentile = (fraction) => Math.round(sorted[Math.ceil(sorted.length * fraction) - 1]);
|
||||
return { count: sorted.length, p95: percentile(0.95), p99: percentile(0.99) };
|
||||
}
|
||||
|
||||
async function readWorkerRss(pid) {
|
||||
const text = await fs.readFile(`/proc/${pid}/status`, "utf8");
|
||||
return Number(text.match(/^VmRSS:\s+(\d+)/m)?.[1] || 0);
|
||||
}
|
||||
|
||||
async function measureLoad({ browser, login, query, passwordHash, workerPid, providerStats }) {
|
||||
const accounts = Array.from({ length: ACCOUNT_COUNT }, (_, index) => `agent-load-${index}@example.test`);
|
||||
await query(`INSERT INTO users (email, password, agent_access) VALUES ${accounts.map(() => "(?, ?, 1)").join(",")}`,
|
||||
accounts.flatMap((email) => [email, passwordHash]));
|
||||
await query("INSERT INTO user_access (user_id, server_name) SELECT id, 'fixture' FROM users WHERE email LIKE 'agent-load-%@example.test'");
|
||||
const clients = [];
|
||||
for (let index = 0; index < accounts.length; index += 2) {
|
||||
const signInAddress = `192.0.2.${Math.floor(index / 8) + 1}`;
|
||||
clients.push(...await Promise.all(accounts.slice(index, index + 2).map((email) => login(email, signInAddress))));
|
||||
}
|
||||
|
||||
async function sampleTraffic(signal) {
|
||||
const probes = { page: { route: "/server/fixture", ms: [], statuses: {} },
|
||||
console: { route: "/api/servers/fixture/console-history?limit=10", ms: [], statuses: {} } };
|
||||
while (!signal.aborted) {
|
||||
await Promise.all(Object.values(probes).map(async (probe) => {
|
||||
const start = performance.now();
|
||||
try {
|
||||
const response = await browser(probe.route);
|
||||
await response.arrayBuffer();
|
||||
probe.statuses[response.status] = (probe.statuses[response.status] || 0) + 1;
|
||||
if (response.ok) probe.ms.push(performance.now() - start);
|
||||
} catch { probe.statuses.network = (probe.statuses.network || 0) + 1; }
|
||||
}));
|
||||
await delay(SAMPLE_MS, undefined, { signal }).catch(() => {});
|
||||
}
|
||||
return Object.fromEntries(Object.entries(probes).map(([name, probe]) => [name, { ms: percentiles(probe.ms), statuses: probe.statuses }]));
|
||||
}
|
||||
|
||||
const baselineStop = AbortSignal.timeout(4000);
|
||||
const baseline = await sampleTraffic(baselineStop);
|
||||
assert.ok(baseline.page.ms, "baseline server page must work");
|
||||
assert.ok(baseline.console.ms, "baseline console route must work");
|
||||
|
||||
const trafficStop = new AbortController();
|
||||
const workloadTraffic = sampleTraffic(trafficStop.signal);
|
||||
const baselineProvider = providerStats();
|
||||
const submitMs = [];
|
||||
const replyMs = [];
|
||||
const statuses = {};
|
||||
const pollErrors = {};
|
||||
const started = performance.now();
|
||||
const runs = clients.map((client, index) => createRun({
|
||||
server: "fixture", provider: "openai-compatible", model: "synthetic", prompt: `Fixture diagnostic ${index}`,
|
||||
image: index % 20 === 0 ? fixtureImage : undefined,
|
||||
}, { fetcher: client }));
|
||||
let peakRunning = 0;
|
||||
let peakRssKiB = 0;
|
||||
let reply = {};
|
||||
let queueDelay = null;
|
||||
const polls = runs.map(async (run) => {
|
||||
try { await run.accepted; } catch { return; }
|
||||
try {
|
||||
await run.result();
|
||||
replyMs.push(performance.now() - started);
|
||||
} catch (error) {
|
||||
if (error.name === "AbortError") return;
|
||||
const reason = `${error.status || "job"}:${error.code || error.message}`;
|
||||
pollErrors[reason] = (pollErrors[reason] || 0) + 1;
|
||||
}
|
||||
});
|
||||
try {
|
||||
const accepted = await Promise.all(runs.map(async (run) => {
|
||||
try {
|
||||
const job = await run.accepted;
|
||||
submitMs.push(performance.now() - started);
|
||||
return job;
|
||||
} catch (error) {
|
||||
const reason = `${error.status || "network"}:${error.code || "response"}`;
|
||||
statuses[reason] = (statuses[reason] || 0) + 1;
|
||||
return null;
|
||||
}
|
||||
}));
|
||||
const ids = accepted.filter(Boolean).map((job) => job.jobId);
|
||||
statuses[202] = ids.length;
|
||||
assert.equal(new Set(ids).size, ids.length);
|
||||
const cutoff = performance.now() + WORKLOAD_TIMEOUT_MS;
|
||||
const placeholder = ids.map(() => "?").join(",");
|
||||
while (ids.length && performance.now() < cutoff) {
|
||||
const rows = await query(`SELECT status, error, TIMESTAMPDIFF(MICROSECOND, created_at, started_at) / 1000 AS wait_ms
|
||||
FROM ai_agent_jobs WHERE id IN (${placeholder})`, ids);
|
||||
reply = {};
|
||||
for (const row of rows) {
|
||||
const key = row.error ? `${row.status}:${row.error}` : row.status;
|
||||
reply[key] = (reply[key] || 0) + 1;
|
||||
}
|
||||
queueDelay = percentiles(rows.filter((row) => row.wait_ms != null).map((row) => Number(row.wait_ms)));
|
||||
peakRunning = Math.max(peakRunning, reply.running || 0);
|
||||
peakRssKiB = Math.max(peakRssKiB, await readWorkerRss(workerPid));
|
||||
if (!(reply.queued || reply.running) && replyMs.length + Object.values(pollErrors).reduce((total, count) => total + count, 0) === ids.length) break;
|
||||
await delay(SAMPLE_MS);
|
||||
}
|
||||
} finally {
|
||||
trafficStop.abort();
|
||||
for (const run of runs) run.detach();
|
||||
}
|
||||
await Promise.allSettled(polls);
|
||||
const traffic = await workloadTraffic;
|
||||
const provider = providerStats();
|
||||
const result = {
|
||||
offered: runs.length, statuses, submitMs: percentiles(submitMs), queueDelayMs: queueDelay,
|
||||
replies: reply, replyMs: percentiles(replyMs), pollErrors, baseline, traffic, peakRunning, peakRssKiB,
|
||||
providerRequests: provider.requests - baselineProvider.requests, peakProviderRequests: provider.peak,
|
||||
observedMs: Math.round(performance.now() - started),
|
||||
};
|
||||
console.log(`LOAD ${JSON.stringify(result)}`);
|
||||
return result;
|
||||
}
|
||||
|
||||
module.exports = { measureLoad, fixtureImage };
|
||||
Reference in new issue
Block a user