#!/usr/bin/env node /** * 单人基线响应时间(基准测试) * * 目的:给出「一个人用容器,正常要等多久」的基准值, * 用于和并发档位对比,量化「人多带来的额外等待」。 * * 做法:串行跑 R 轮,每轮 1 个新会话、1 个请求,记录端到端耗时。 * 串行是刻意的 —— 基准必须没有任何并发干扰。 * * 用法:node scripts/loadtest-baseline.mjs --rounds 8 --mode both */ import { readFileSync, writeFileSync } from 'node:fs'; import { homedir } from 'node:os'; import { join } from 'node:path'; const FUNCTIONS = 'https://server.fmode.cn/api/functions'; const FN_PROVISION = 'RP5Ht55NeX'; const AGENT = 'agent-node-lj-test'; const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); const arg = (n, d) => { const i = process.argv.indexOf(`--${n}`); return i >= 0 && process.argv[i + 1] ? process.argv[i + 1] : d; }; const ROUNDS = Number(arg('rounds', '8')); const MODE = arg('mode', 'text'); // text | file const TIMEOUT_S = Number(arg('timeout', '300')); const RUN = `base${Date.now().toString().slice(-6)}`; const TOKEN = JSON.parse(readFileSync(join(homedir(), '.fmode/config/user.json'), 'utf8')).sessionToken; const pct = (a, p) => (a.length ? [...a].sort((x, y) => x - y)[Math.min(a.length - 1, Math.floor((p / 100) * a.length))] : 0); const r1 = (n) => Math.round(n * 10) / 10; async function main() { const j = await (await fetch(FUNCTIONS, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Parse-Application-Id': 'ncloudmaster' }, body: JSON.stringify({ id: FN_PROVISION, token: TOKEN, params: { agentId: AGENT } }), })).json(); const p = j.result ?? j; const mx = await import('matrix-js-sdk'); const c = mx.createClient({ baseUrl: p.homeserver, accessToken: p.accessToken, userId: p.userMxId }); const hs = p.homeserver.replace(/\/$/, ''); const auth = { Authorization: `Bearer ${p.accessToken}` }; console.log(`基线测试:${ROUNDS} 轮串行,模式 ${MODE},容器 ${p.agentMxId}\n`); const results = []; for (let k = 1; k <= ROUNDS; k++) { const room = (await c.createRoom({ invite: [p.agentMxId], name: `${RUN}-${k}`, preset: mx.Preset.TrustedPrivateChat })).room_id; // 等进房 for (let i = 0; i < 40; i++) { await sleep(2000); const jm = await (await fetch(`${hs}/_matrix/client/v3/rooms/${encodeURIComponent(room)}/joined_members`, { headers: auth })).json().catch(() => ({})); if ((jm.joined ?? {})[p.agentMxId]) break; } const tok = `B${k}${Math.random().toString(36).slice(2, 7).toUpperCase()}`; const prompt = MODE === 'text' ? `只回复一行:${tok} 已收到` : `在 /opt/data/${RUN}/${k}/ 下生成 1 个文件 r.txt(三行文字,内容含 ${tok}),并作为附件发给我。完成后一句话总结。`; const sent = await c.sendEvent(room, 'm.room.message', { msgtype: 'm.text', body: prompt, 'm.mentions': { user_ids: [p.agentMxId] } }); const t0 = Date.now(); let firstProcess = 0, firstBody = 0, lastEv = 0; /* * 🔴 附件必须按 event_id 去重计数:轮询是「每次拉最近 N 条」, * 同一个 m.file 会被反复读到。之前用累加,1 个附件被数成了 13 个。 */ const fileIds = new Set(); const seenEv = new Set(); const deadline = t0 + TIMEOUT_S * 1000; const isProg = (b) => /^[*·•\s]*(🐍|💻|🔎|📖|✍️|🧠|📝|⚙|🔧|Writing|Reading|Running|terminal|write_file|execute_code)/i.test(b.trim()); while (Date.now() < deadline) { await sleep(2500); const jj = await (await fetch(`${hs}/_matrix/client/v3/rooms/${encodeURIComponent(room)}/messages?dir=b&limit=40`, { headers: auth })).json().catch(() => ({})); for (const ev of (jj.chunk ?? []).reverse()) { if (ev.event_id === sent.event_id) continue; if (ev.type !== 'm.room.message' || ev.sender !== p.agentMxId) continue; const cc = ev.content ?? {}, body = String(cc.body ?? ''); if (seenEv.has(ev.event_id)) continue; seenEv.add(ev.event_id); lastEv = ev.origin_server_ts; if (cc['fmode.kind'] === 'process' || isProg(body)) { if (!firstProcess) firstProcess = ev.origin_server_ts; continue; } if (!firstBody && body.trim()) firstBody = ev.origin_server_ts; if (cc.url && (cc.msgtype === 'm.file' || cc.msgtype === 'm.image')) fileIds.add(ev.event_id); } if (firstBody && Date.now() - lastEv > 30000) break; } const files = fileIds.size; const e2e = firstBody ? (lastEv - t0) / 1000 : null; const queue = firstProcess ? (firstProcess - t0) / 1000 : null; results.push({ k, room, tok, e2e, queue, files }); console.log(` 第 ${k} 轮:端到端 ${e2e ?? '超时'}s | 首条过程 ${queue ?? '-'}s | 文件 ${files}`); } const e = results.map((r) => r.e2e).filter((v) => v != null); const q = results.map((r) => r.queue).filter((v) => v != null); const stats = { run: RUN, mode: MODE, rounds: ROUNDS, success: e.length, p50: r1(pct(e, 50)), p90: r1(pct(e, 90)), min: r1(Math.min(...e)), max: r1(Math.max(...e)), queueP50: r1(pct(q, 50)), results, }; console.log(`\n基线(${MODE}):成功 ${stats.success}/${ROUNDS} | p50 ${stats.p50}s | p90 ${stats.p90}s | 区间 ${stats.min}–${stats.max}s | 首条响应 p50 ${stats.queueP50}s`); writeFileSync(`docs/03-interface/baseline-ljtest-${MODE}-${RUN}.json`, JSON.stringify(stats, null, 2)); console.log(`落盘:docs/03-interface/baseline-ljtest-${MODE}-${RUN}.json`); c.stopClient(); } main().catch((e) => { console.error('失败:', e.message ?? e); process.exit(1); });