magpie-model-speedtest 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,19 @@
1
- # Temporary Holding Version
1
+ # Magpie 模型测速
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 在 Magpie 的「模型测速」页面点「开始测速」,逐个测量当前网关启用的文本模型。每轮都要求抄写同一段英文,最多请求 256 个输出 token。全新 Impeccable 界面设计,同时呈现**端到端速度**与**正文流速**,支持搜索过滤、动态排序与一键导出。测速会消耗模型额度。
4
+
5
+ ```sh
6
+ magpie plugin add magpie-model-speedtest
7
+ ```
8
+
9
+ 本地开发可用 `magpie plugin add ./magpie-model-speedtest`。页面服务默认监听 `127.0.0.1:3487`,可在插件选项中修改 `port`。网关端口从 Magpie 的 `gateway.json` 读取,插件用调用方标识 `magpie-model-speedtest` 访问本机网关。无需另外配置供应商密钥。
10
+
11
+ - **端到端速度 (E2E Throughput)**:综合网络建连、排队、Prompt 预填充、内部思考(Reasoning)与正文输出的全局总耗时计算的 token/s,直观反映发出请求到完整拿到结果的真实体验。
12
+ - **正文流速 (Streaming Speed)**:排除首字等待与思考延迟,仅统计首个可见字符到达至最后一个字符到达之间的打字机喷发流速。
13
+ - **首字延迟 (TTFT)**:从发出请求到收到第一段可见正文的等待时间,支持智能色阶标注。
14
+ - 默认每个实体模型测一次,按端到端速度排序(点击表头可自由切换任意列排序)。路由组可单独勾选,命中模型以网关报告为准。
15
+ - 下拉选择一个模型或组可测三轮,三个有效速度样本齐全后显示中位数。
16
+ - 支持按模型名称或供应商实时搜索过滤,支持一键复制标准 Markdown 表格或导出完整 JSON。
17
+ - 供应商同时报告输出和推理用量时,以两者之差作为正文 token 数。缺少推理明细时,按英文正文平均 4 个 UTF-8 字节估算一个 token。
18
+ - 正文集中在 10 毫秒以内到达或整段返回时,端到端速度正常计算,正文流速留空。只有推理、流中断、HTTP 错误和单请求超过 60 秒都会逐行标明,队列继续。支持时使用模型公开的最低推理档位。
19
+ - 停止会取消当前请求和后续队列。已完成的结果在本次服务进程内保留到下次测速。
package/app/app.js ADDED
@@ -0,0 +1,432 @@
1
+ const byId = id => document.getElementById(id)
2
+ const token = document.querySelector('meta[name="session"]').content
3
+ const target = byId("target"), groups = byId("groups"), filterInput = byId("filter")
4
+ const startBtn = byId("start"), stopBtn = byId("stop")
5
+
6
+ let models = []
7
+ let busy = false
8
+ let pending = false
9
+ let latestState = { rows: [], count: 0, completed: 0, running: false }
10
+ let sortColumn = "e2eSpeed"
11
+ let sortAscending = false
12
+
13
+ async function api(path, params = {}) {
14
+ const response = await fetch(path, {
15
+ method: "POST",
16
+ headers: { "Content-Type": "application/json" },
17
+ body: JSON.stringify({ ...params, token }),
18
+ signal: AbortSignal.timeout(15000)
19
+ })
20
+ const data = await response.json()
21
+ if (response.status === 403 && data.reload) location.reload()
22
+ if (!response.ok) throw new Error(data.error || `服务返回 HTTP ${response.status},请刷新页面`)
23
+ return data
24
+ }
25
+
26
+ function showToast(message) {
27
+ const toast = byId("toast")
28
+ toast.textContent = message
29
+ toast.classList.add("show")
30
+ setTimeout(() => toast.classList.remove("show"), 2200)
31
+ }
32
+
33
+ function updateControls() {
34
+ const count = target.value ? 3 : models.filter(m => groups.checked || !m.group).length
35
+ startBtn.disabled = pending || busy || !count
36
+ stopBtn.hidden = !busy
37
+ target.disabled = busy || pending
38
+ groups.disabled = !!target.value || busy || pending
39
+ byId("plan-text").textContent = `预计 ${count} 次请求 · 串行测试 · 每次上限 256 输出 token`
40
+ }
41
+
42
+ function formatMs(ms) {
43
+ if (ms == null) return "—"
44
+ if (ms < 1000) return `${Math.round(ms)} ms`
45
+ return `${(ms / 1000).toFixed(2)} s`
46
+ }
47
+
48
+ function parseProvider(id) {
49
+ const parts = id.split("/")
50
+ if (parts.length > 1) {
51
+ const p = parts[0].toLowerCase()
52
+ if (p === "group") return { tag: "group", label: "路由组", name: parts.slice(1).join("/") }
53
+ if (p.includes("grok")) return { tag: "grok", label: "Grok", name: parts.slice(1).join("/") }
54
+ if (p.includes("deepseek")) return { tag: "deepseek", label: "DeepSeek", name: parts.slice(1).join("/") }
55
+ if (p.includes("google")) return { tag: "google", label: "Google", name: parts.slice(1).join("/") }
56
+ if (p.includes("codex")) return { tag: "codex", label: "Codex", name: parts.slice(1).join("/") }
57
+ return { tag: "provider", label: parts[0], name: parts.slice(1).join("/") }
58
+ }
59
+ return { tag: "model", label: "模型", name: id }
60
+ }
61
+
62
+ function computeE2eSpeed(row) {
63
+ if (row.e2eSpeed != null) return row.e2eSpeed
64
+ if (row.total > 0 && row.tokens > 0) return (row.tokens * 1000) / row.total
65
+ return null
66
+ }
67
+
68
+ function updateKpis(rows, state) {
69
+ byId("kpi-count").textContent = state.count ? `${state.completed} / ${state.count}` : (models.length ? `${models.length} 款` : "—")
70
+ byId("kpi-count-sub").textContent = busy ? `正在测试: ${state.current || "准备中"}` : (state.completed ? (state.stopped ? "测试已停止" : "全部测试完成") : "就绪")
71
+
72
+ const validE2e = rows.filter(r => computeE2eSpeed(r) != null).sort((a, b) => computeE2eSpeed(b) - computeE2eSpeed(a))
73
+ const validStream = rows.filter(r => r.speed != null).sort((a, b) => b.speed - a.speed)
74
+ const validTtft = rows.filter(r => r.ttft != null).sort((a, b) => a.ttft - b.ttft)
75
+
76
+ if (validE2e.length) {
77
+ const top = validE2e[0]
78
+ byId("kpi-top-e2e").textContent = `${computeE2eSpeed(top).toFixed(1)} tok/s`
79
+ byId("kpi-top-e2e-name").textContent = top.id
80
+ } else {
81
+ byId("kpi-top-e2e").textContent = "—"
82
+ byId("kpi-top-e2e-name").textContent = "等待测速数据"
83
+ }
84
+
85
+ if (validStream.length) {
86
+ const top = validStream[0]
87
+ byId("kpi-top-stream").textContent = `${top.speed.toFixed(1)} tok/s`
88
+ byId("kpi-top-stream-name").textContent = top.id
89
+ } else {
90
+ byId("kpi-top-stream").textContent = "—"
91
+ byId("kpi-top-stream-name").textContent = "等待测速数据"
92
+ }
93
+
94
+ if (validTtft.length) {
95
+ const best = validTtft[0]
96
+ byId("kpi-best-ttft").textContent = formatMs(best.ttft)
97
+ byId("kpi-best-ttft-name").textContent = best.id
98
+ } else {
99
+ byId("kpi-best-ttft").textContent = "—"
100
+ byId("kpi-best-ttft-name").textContent = "等待测速数据"
101
+ }
102
+ }
103
+
104
+ function renderTable() {
105
+ const rows = [...latestState.rows]
106
+ const filter = (filterInput.value || "").trim().toLowerCase()
107
+ const filtered = filter ? rows.filter(r => r.id.toLowerCase().includes(filter) || (r.actual && r.actual.toLowerCase().includes(filter))) : rows
108
+
109
+ // Sort
110
+ filtered.sort((a, b) => {
111
+ let valA, valB
112
+ switch (sortColumn) {
113
+ case "id":
114
+ valA = a.id; valB = b.id
115
+ return sortAscending ? valA.localeCompare(valB) : valB.localeCompare(valA)
116
+ case "e2eSpeed":
117
+ valA = computeE2eSpeed(a) ?? -1
118
+ valB = computeE2eSpeed(b) ?? -1
119
+ break
120
+ case "speed":
121
+ valA = a.speed ?? -1
122
+ valB = b.speed ?? -1
123
+ break
124
+ case "ttft":
125
+ valA = a.ttft ?? 999999
126
+ valB = b.ttft ?? 999999
127
+ break
128
+ case "tokens":
129
+ valA = a.tokens ?? -1
130
+ valB = b.tokens ?? -1
131
+ break
132
+ case "total":
133
+ valA = a.total ?? 999999
134
+ valB = b.total ?? 999999
135
+ break
136
+ default:
137
+ valA = computeE2eSpeed(a) ?? -1
138
+ valB = computeE2eSpeed(b) ?? -1
139
+ }
140
+ return sortAscending ? valA - valB : valB - valA
141
+ })
142
+
143
+ const maxE2e = Math.max(...rows.map(r => computeE2eSpeed(r) || 0), 1)
144
+ const maxStream = Math.max(...rows.map(r => r.speed || 0), 1)
145
+
146
+ const tbody = byId("table-body")
147
+ tbody.replaceChildren()
148
+
149
+ if (!filtered.length) {
150
+ const tr = tbody.insertRow()
151
+ const td = tr.insertCell()
152
+ td.colSpan = 7
153
+ td.style.textAlign = "center"
154
+ td.style.padding = "36px"
155
+ td.style.color = "var(--text-muted)"
156
+ td.textContent = rows.length ? "未匹配到符合过滤条件的模型" : "暂无测试结果,点击上方「开始测速」运行基准。"
157
+ return
158
+ }
159
+
160
+ for (const row of filtered) {
161
+ const tr = tbody.insertRow()
162
+ tr.className = "model-row"
163
+ if (busy && latestState.current === row.id) tr.classList.add("active-measuring")
164
+
165
+ // Col 1: Model & Provider
166
+ const tdModel = tr.insertCell()
167
+ const parsed = parseProvider(row.id)
168
+ const modelCell = document.createElement("div")
169
+ modelCell.className = "model-cell"
170
+
171
+ const tag = document.createElement("span")
172
+ tag.className = `provider-tag ${parsed.tag}`
173
+ tag.textContent = parsed.label
174
+
175
+ const nameWrap = document.createElement("div")
176
+ const name = document.createElement("span")
177
+ name.className = "model-name"
178
+ name.textContent = parsed.name
179
+ nameWrap.appendChild(name)
180
+
181
+ const metas = []
182
+ if (row.round > 1) metas.push(`第 ${row.round} 轮`)
183
+ if (row.actual && row.actual !== row.id) metas.push(`↳ 命中: ${row.actual}`)
184
+ if (metas.length) {
185
+ const meta = document.createElement("span")
186
+ meta.className = "model-meta"
187
+ meta.textContent = metas.join(" · ")
188
+ nameWrap.appendChild(meta)
189
+ }
190
+
191
+ modelCell.append(tag, nameWrap)
192
+ tdModel.appendChild(modelCell)
193
+
194
+ // Col 2: End-to-End Speed
195
+ const tdE2e = tr.insertCell()
196
+ tdE2e.className = "num"
197
+ const e2e = computeE2eSpeed(row)
198
+ if (e2e != null) {
199
+ const speedWrap = document.createElement("div")
200
+ speedWrap.className = "speed-cell"
201
+ const num = document.createElement("span")
202
+ num.className = "speed-num"
203
+ num.style.color = "var(--brand-purple)"
204
+ num.textContent = `${e2e.toFixed(1)} tok/s`
205
+ const track = document.createElement("div")
206
+ track.className = "speed-track"
207
+ const fill = document.createElement("div")
208
+ fill.className = "speed-fill-e2e"
209
+ fill.style.width = `${Math.min(100, Math.round((e2e / maxE2e) * 100))}%`
210
+ track.appendChild(fill)
211
+ speedWrap.append(num, track)
212
+ tdE2e.appendChild(speedWrap)
213
+ } else {
214
+ tdE2e.textContent = "—"
215
+ tdE2e.style.color = "var(--text-muted)"
216
+ }
217
+
218
+ // Col 3: Streaming Speed
219
+ const tdStream = tr.insertCell()
220
+ tdStream.className = "num"
221
+ if (row.speed != null) {
222
+ const speedWrap = document.createElement("div")
223
+ speedWrap.className = "speed-cell"
224
+ const num = document.createElement("span")
225
+ num.className = "speed-num"
226
+ num.style.color = "var(--brand-teal)"
227
+ num.textContent = `${row.speed.toFixed(1)} tok/s`
228
+ const track = document.createElement("div")
229
+ track.className = "speed-track"
230
+ const fill = document.createElement("div")
231
+ fill.className = "speed-fill-stream"
232
+ fill.style.width = `${Math.min(100, Math.round((row.speed / maxStream) * 100))}%`
233
+ track.appendChild(fill)
234
+ speedWrap.append(num, track)
235
+ tdStream.appendChild(speedWrap)
236
+ } else {
237
+ tdStream.textContent = "—"
238
+ tdStream.style.color = "var(--text-muted)"
239
+ }
240
+
241
+ // Col 4: TTFT
242
+ const tdTtft = tr.insertCell()
243
+ tdTtft.className = "num"
244
+ if (row.ttft != null) {
245
+ const span = document.createElement("span")
246
+ span.className = row.ttft < 1000 ? "ttft-fast" : (row.ttft < 2500 ? "ttft-med" : "ttft-slow")
247
+ span.textContent = formatMs(row.ttft)
248
+ tdTtft.appendChild(span)
249
+ } else {
250
+ tdTtft.textContent = "—"
251
+ }
252
+
253
+ // Col 5: Tokens
254
+ const tdTokens = tr.insertCell()
255
+ tdTokens.className = "num"
256
+ tdTokens.style.fontFamily = "var(--font-mono)"
257
+ if (row.tokens != null) {
258
+ tdTokens.textContent = `${row.exact ? "" : "≈ "}${row.tokens}`
259
+ } else {
260
+ tdTokens.textContent = "—"
261
+ }
262
+
263
+ // Col 6: Total Time
264
+ const tdTotal = tr.insertCell()
265
+ tdTotal.className = "num"
266
+ tdTotal.style.fontFamily = "var(--font-mono)"
267
+ tdTotal.textContent = formatMs(row.total)
268
+
269
+ // Col 7: Status / Note
270
+ const tdStatus = tr.insertCell()
271
+ const badge = document.createElement("span")
272
+ if (row.error) {
273
+ badge.className = "status-badge badge-error"
274
+ badge.textContent = `⚠ ${row.error}`
275
+ } else if (row.speed != null) {
276
+ badge.className = "status-badge badge-ok"
277
+ badge.textContent = "✓ 成功"
278
+ } else {
279
+ badge.className = "status-badge badge-warn"
280
+ badge.textContent = row.note || "整段返回"
281
+ }
282
+ tdStatus.appendChild(badge)
283
+ }
284
+
285
+ // Median summary for 3-round single model test
286
+ const samples = rows.filter(r => r.speed != null)
287
+ const isSingleModelRounds = !busy && samples.length === 3 && new Set(samples.map(r => r.id)).size === 1
288
+ if (isSingleModelRounds) {
289
+ const sortedE2e = [...samples].map(r => computeE2eSpeed(r)).sort((a, b) => a - b)
290
+ const sortedStream = [...samples].map(r => r.speed).sort((a, b) => a - b)
291
+ const sortedTtft = [...samples].map(r => r.ttft).sort((a, b) => a - b)
292
+ byId("median-summary").textContent = `三轮中位数:端到端 ≈ ${sortedE2e[1].toFixed(1)} tok/s · 正文流速 ≈ ${sortedStream[1].toFixed(1)} tok/s · 首字 ≈ ${formatMs(sortedTtft[1])}`
293
+ } else {
294
+ byId("median-summary").textContent = ""
295
+ }
296
+ }
297
+
298
+ function draw(state) {
299
+ latestState = state
300
+ busy = state.running
301
+ updateControls()
302
+
303
+ const progressContainer = byId("progress-container")
304
+ if (busy || state.count > 0) {
305
+ progressContainer.hidden = false
306
+ byId("status").textContent = busy ? `[${state.completed} / ${state.count}] 正在测 ${state.current || "..."}` : (state.stopped ? "测试已停止" : "测速已完成")
307
+ const pct = state.count ? Math.round((state.completed / state.count) * 100) : 0
308
+ byId("progress-bar").style.width = `${pct}%`
309
+ } else {
310
+ progressContainer.hidden = true
311
+ }
312
+
313
+ byId("target-summary").textContent = busy ? "测速中" : (state.completed ? "已完成" : "就绪")
314
+ byId("table-summary-info").textContent = `已采集 ${state.rows.length} 条测试记录 · 点击表头可切换排序`
315
+
316
+ updateKpis(state.rows, state)
317
+ renderTable()
318
+ }
319
+
320
+ async function loadModels() {
321
+ models = await api("models")
322
+ const previous = target.value
323
+ target.length = 1
324
+ for (const group of [false, true]) {
325
+ const section = document.createElement("optgroup")
326
+ section.label = group ? "路由组 · 单项复测 3 轮" : "单模型复测 · 3 轮"
327
+ for (const model of models.filter(item => item.group === group)) {
328
+ section.append(new Option(model.id, model.id))
329
+ }
330
+ target.append(section)
331
+ }
332
+ target.value = previous
333
+ updateControls()
334
+ }
335
+
336
+ function copyMarkdown() {
337
+ const rows = [...latestState.rows]
338
+ if (!rows.length) {
339
+ showToast("当前暂无数据可复制")
340
+ return
341
+ }
342
+ let md = "| 模型 / 供应商 | 端到端速度 | 正文流速 | 首字延迟 | 正文 Tokens | 总耗时 | 状态 |\n"
343
+ md += "|---|---:|---:|---:|---:|---:|---|\n"
344
+ for (const r of rows) {
345
+ const e2e = computeE2eSpeed(r)
346
+ const e2eStr = e2e != null ? `${e2e.toFixed(1)} tok/s` : "—"
347
+ const streamStr = r.speed != null ? `${r.speed.toFixed(1)} tok/s` : "—"
348
+ const ttftStr = formatMs(r.ttft)
349
+ const tokStr = r.tokens != null ? `${r.exact ? "" : "≈"}${r.tokens}` : "—"
350
+ const totalStr = formatMs(r.total)
351
+ const statusStr = r.error ? `错误: ${r.error}` : (r.note || "成功")
352
+ md += `| \`${r.id}\` | ${e2eStr} | ${streamStr} | ${ttftStr} | ${tokStr} | ${totalStr} | ${statusStr} |\n`
353
+ }
354
+ navigator.clipboard.writeText(md).then(() => {
355
+ showToast("✓ 已复制 Markdown 表格到剪贴板")
356
+ }).catch(() => {
357
+ showToast("复制失败,请手动导出")
358
+ })
359
+ }
360
+
361
+ function exportJson() {
362
+ const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(latestState, null, 2))
363
+ const downloadAnchor = document.createElement("a")
364
+ downloadAnchor.setAttribute("href", dataStr)
365
+ downloadAnchor.setAttribute("download", `magpie-benchmark-${new Date().toISOString().slice(0, 10)}.json`)
366
+ document.body.appendChild(downloadAnchor)
367
+ downloadAnchor.click()
368
+ downloadAnchor.remove()
369
+ showToast("✓ 已导出 JSON 数据")
370
+ }
371
+
372
+ // Event Listeners
373
+ target.onchange = groups.onchange = updateControls
374
+ filterInput.oninput = renderTable
375
+
376
+ document.querySelectorAll("th[data-col]").forEach(th => {
377
+ th.onclick = () => {
378
+ const col = th.getAttribute("data-col")
379
+ if (sortColumn === col) {
380
+ sortAscending = !sortAscending
381
+ } else {
382
+ sortColumn = col
383
+ sortAscending = (col === "ttft" || col === "total" || col === "id")
384
+ }
385
+ document.querySelectorAll("th[data-col]").forEach(h => {
386
+ h.classList.remove("sort-active")
387
+ h.removeAttribute("data-sort-arrow")
388
+ })
389
+ th.classList.add("sort-active")
390
+ th.setAttribute("data-sort-arrow", sortAscending ? "▲" : "▼")
391
+ renderTable()
392
+ }
393
+ })
394
+
395
+ startBtn.onclick = async () => {
396
+ pending = true
397
+ const id = target.value
398
+ updateControls()
399
+ try {
400
+ await loadModels()
401
+ draw(await api("start", { id, groups: groups.checked }))
402
+ } catch (error) {
403
+ showToast(`启动失败: ${error.message}`)
404
+ } finally {
405
+ pending = false
406
+ updateControls()
407
+ }
408
+ }
409
+
410
+ stopBtn.onclick = async () => {
411
+ try {
412
+ await api("stop")
413
+ showToast("已发送停止请求")
414
+ } catch (error) {
415
+ showToast(`停止失败: ${error.message}`)
416
+ }
417
+ }
418
+
419
+ byId("btn-copy-md").onclick = copyMarkdown
420
+ byId("btn-export-json").onclick = exportJson
421
+
422
+ async function poll() {
423
+ try {
424
+ if (!models.length) await loadModels()
425
+ if (!pending) draw(await api("state"))
426
+ } catch (error) {
427
+ console.error("Poll error:", error)
428
+ }
429
+ setTimeout(poll, busy ? 600 : 2000)
430
+ }
431
+
432
+ poll()
package/app/index.html ADDED
@@ -0,0 +1,689 @@
1
+ <!doctype html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <meta name="session" content="__SESSION__">
7
+ <title>Magpie 模型测速基准</title>
8
+ <style>
9
+ :root {
10
+ --bg-canvas: #0a0d14;
11
+ --bg-surface: #121722;
12
+ --bg-surface-elevated: #182030;
13
+ --bg-surface-hover: #1f293d;
14
+ --border-subtle: rgba(255, 255, 255, 0.08);
15
+ --border-focus: rgba(20, 184, 166, 0.5);
16
+ --text-primary: #f8fafc;
17
+ --text-secondary: #94a3b8;
18
+ --text-muted: #64748b;
19
+ --brand-teal: #14b8a6;
20
+ --brand-teal-dim: rgba(20, 184, 166, 0.12);
21
+ --brand-purple: #8b5cf6;
22
+ --brand-purple-dim: rgba(139, 92, 246, 0.14);
23
+ --brand-blue: #38bdf8;
24
+ --brand-emerald: #10b981;
25
+ --brand-amber: #f59e0b;
26
+ --brand-rose: #f43f5e;
27
+ --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
28
+ --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
29
+ --radius-sm: 6px;
30
+ --radius-md: 10px;
31
+ --radius-lg: 14px;
32
+ --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.2);
33
+ --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.25);
34
+ --shadow-glow: 0 0 20px rgba(20, 184, 166, 0.2);
35
+ }
36
+
37
+ @media (prefers-color-scheme: light) {
38
+ :root {
39
+ --bg-canvas: #f8fafc;
40
+ --bg-surface: #ffffff;
41
+ --bg-surface-elevated: #f1f5f9;
42
+ --bg-surface-hover: #e2e8f0;
43
+ --border-subtle: rgba(0, 0, 0, 0.08);
44
+ --border-focus: rgba(13, 148, 136, 0.5);
45
+ --text-primary: #0f172a;
46
+ --text-secondary: #475569;
47
+ --text-muted: #94a3b8;
48
+ --brand-teal: #0d9488;
49
+ --brand-teal-dim: rgba(13, 148, 136, 0.08);
50
+ --brand-purple: #7c3aed;
51
+ --brand-purple-dim: rgba(124, 58, 237, 0.08);
52
+ --brand-blue: #0284c7;
53
+ --brand-emerald: #059669;
54
+ --brand-amber: #d97706;
55
+ --brand-rose: #e11d48;
56
+ --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.05);
57
+ --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.07);
58
+ --shadow-glow: 0 0 20px rgba(13, 148, 136, 0.12);
59
+ }
60
+ }
61
+
62
+ * { box-sizing: border-box; margin: 0; padding: 0; }
63
+ body {
64
+ font-family: var(--font-sans);
65
+ font-size: 14px;
66
+ line-height: 1.5;
67
+ color: var(--text-primary);
68
+ background-color: var(--bg-canvas);
69
+ min-height: 100vh;
70
+ padding: clamp(20px, 3vw, 40px) 24px;
71
+ -webkit-font-smoothing: antialiased;
72
+ }
73
+
74
+ .app-container {
75
+ max-width: 1240px;
76
+ margin: 0 auto;
77
+ display: flex;
78
+ flex-direction: column;
79
+ gap: 20px;
80
+ }
81
+
82
+ /* Header */
83
+ header {
84
+ display: flex;
85
+ align-items: flex-start;
86
+ justify-content: space-between;
87
+ flex-wrap: wrap;
88
+ gap: 16px;
89
+ }
90
+ .brand-wrap {
91
+ display: flex;
92
+ align-items: center;
93
+ gap: 12px;
94
+ }
95
+ .brand-icon {
96
+ width: 40px;
97
+ height: 40px;
98
+ border-radius: var(--radius-md);
99
+ background: linear-gradient(135deg, var(--brand-teal), var(--brand-purple));
100
+ display: flex;
101
+ align-items: center;
102
+ justify-content: center;
103
+ color: white;
104
+ font-size: 20px;
105
+ box-shadow: var(--shadow-glow);
106
+ flex-shrink: 0;
107
+ }
108
+ .header-text h1 {
109
+ font-size: clamp(20px, 2.5vw, 26px);
110
+ font-weight: 700;
111
+ letter-spacing: -0.03em;
112
+ line-height: 1.2;
113
+ color: var(--text-primary);
114
+ }
115
+ .header-text p {
116
+ font-size: 13px;
117
+ color: var(--text-secondary);
118
+ margin-top: 3px;
119
+ }
120
+ .badge-pill {
121
+ display: inline-flex;
122
+ align-items: center;
123
+ gap: 5px;
124
+ padding: 3px 9px;
125
+ border-radius: 999px;
126
+ font-size: 11px;
127
+ font-weight: 600;
128
+ background: var(--bg-surface-elevated);
129
+ border: 1px solid var(--border-subtle);
130
+ color: var(--text-secondary);
131
+ }
132
+
133
+ /* KPI Stats Bar */
134
+ .kpi-grid {
135
+ display: grid;
136
+ grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
137
+ gap: 12px;
138
+ }
139
+ .kpi-card {
140
+ background: var(--bg-surface);
141
+ border: 1px solid var(--border-subtle);
142
+ border-radius: var(--radius-md);
143
+ padding: 14px 18px;
144
+ display: flex;
145
+ flex-direction: column;
146
+ justify-content: space-between;
147
+ box-shadow: var(--shadow-sm);
148
+ transition: transform 0.15s ease, border-color 0.15s ease;
149
+ }
150
+ .kpi-card:hover {
151
+ border-color: rgba(255, 255, 255, 0.16);
152
+ }
153
+ .kpi-label {
154
+ font-size: 12px;
155
+ font-weight: 500;
156
+ color: var(--text-secondary);
157
+ display: flex;
158
+ align-items: center;
159
+ justify-content: space-between;
160
+ }
161
+ .kpi-val {
162
+ font-size: 26px;
163
+ font-weight: 700;
164
+ letter-spacing: -0.02em;
165
+ font-family: var(--font-mono);
166
+ margin-top: 4px;
167
+ color: var(--text-primary);
168
+ }
169
+ .kpi-sub {
170
+ font-size: 11px;
171
+ color: var(--text-muted);
172
+ margin-top: 2px;
173
+ white-space: nowrap;
174
+ overflow: hidden;
175
+ text-overflow: ellipsis;
176
+ }
177
+ .kpi-val.purple { color: var(--brand-purple); }
178
+ .kpi-val.teal { color: var(--brand-teal); }
179
+ .kpi-val.blue { color: var(--brand-blue); }
180
+
181
+ /* Control Center Card */
182
+ .control-card {
183
+ background: var(--bg-surface);
184
+ border: 1px solid var(--border-subtle);
185
+ border-radius: var(--radius-lg);
186
+ padding: 16px 20px;
187
+ box-shadow: var(--shadow-md);
188
+ display: flex;
189
+ flex-direction: column;
190
+ gap: 14px;
191
+ }
192
+ .control-row {
193
+ display: flex;
194
+ align-items: center;
195
+ justify-content: space-between;
196
+ flex-wrap: wrap;
197
+ gap: 12px;
198
+ }
199
+ .control-left, .control-right {
200
+ display: flex;
201
+ align-items: center;
202
+ flex-wrap: wrap;
203
+ gap: 10px;
204
+ }
205
+
206
+ /* Inputs & Selects */
207
+ select, input[type="text"] {
208
+ font-family: var(--font-sans);
209
+ font-size: 13px;
210
+ color: var(--text-primary);
211
+ background: var(--bg-surface-elevated);
212
+ border: 1px solid var(--border-subtle);
213
+ border-radius: var(--radius-sm);
214
+ padding: 7px 12px;
215
+ outline: none;
216
+ transition: border-color 0.15s ease, box-shadow 0.15s ease;
217
+ }
218
+ select:focus, input[type="text"]:focus {
219
+ border-color: var(--border-focus);
220
+ box-shadow: 0 0 0 2px var(--brand-teal-dim);
221
+ }
222
+ select {
223
+ cursor: pointer;
224
+ max-width: 260px;
225
+ }
226
+ input[type="text"] {
227
+ min-width: 170px;
228
+ }
229
+
230
+ /* Checkbox Label Pill */
231
+ .checkbox-pill {
232
+ display: inline-flex;
233
+ align-items: center;
234
+ gap: 7px;
235
+ padding: 6px 12px;
236
+ background: var(--bg-surface-elevated);
237
+ border: 1px solid var(--border-subtle);
238
+ border-radius: var(--radius-sm);
239
+ cursor: pointer;
240
+ user-select: none;
241
+ font-size: 13px;
242
+ color: var(--text-secondary);
243
+ transition: all 0.15s ease;
244
+ }
245
+ .checkbox-pill:hover {
246
+ background: var(--bg-surface-hover);
247
+ color: var(--text-primary);
248
+ }
249
+ .checkbox-pill input[type="checkbox"] {
250
+ accent-color: var(--brand-teal);
251
+ cursor: pointer;
252
+ }
253
+
254
+ /* Buttons */
255
+ button {
256
+ font-family: var(--font-sans);
257
+ font-size: 13px;
258
+ font-weight: 600;
259
+ border-radius: var(--radius-sm);
260
+ padding: 7px 15px;
261
+ cursor: pointer;
262
+ display: inline-flex;
263
+ align-items: center;
264
+ gap: 6px;
265
+ border: 1px solid transparent;
266
+ transition: all 0.15s ease;
267
+ white-space: nowrap;
268
+ }
269
+ button:disabled {
270
+ opacity: 0.45;
271
+ cursor: not-allowed;
272
+ }
273
+ .btn-primary {
274
+ background: var(--brand-teal);
275
+ color: #ffffff;
276
+ box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
277
+ }
278
+ .btn-primary:not(:disabled):hover {
279
+ filter: brightness(1.1);
280
+ transform: translateY(-1px);
281
+ box-shadow: 0 4px 10px rgba(20, 184, 166, 0.3);
282
+ }
283
+ .btn-stop {
284
+ background: var(--brand-rose);
285
+ color: white;
286
+ }
287
+ .btn-stop:hover {
288
+ filter: brightness(1.1);
289
+ }
290
+ .btn-ghost {
291
+ background: var(--bg-surface-elevated);
292
+ color: var(--text-secondary);
293
+ border: 1px solid var(--border-subtle);
294
+ }
295
+ .btn-ghost:not(:disabled):hover {
296
+ background: var(--bg-surface-hover);
297
+ color: var(--text-primary);
298
+ }
299
+
300
+ /* Status and Progress */
301
+ .progress-section {
302
+ display: flex;
303
+ flex-direction: column;
304
+ gap: 6px;
305
+ }
306
+ .status-row {
307
+ display: flex;
308
+ align-items: center;
309
+ justify-content: space-between;
310
+ font-size: 12px;
311
+ color: var(--text-secondary);
312
+ }
313
+ .live-status {
314
+ display: flex;
315
+ align-items: center;
316
+ gap: 7px;
317
+ font-weight: 500;
318
+ }
319
+ .dot-live {
320
+ width: 8px;
321
+ height: 8px;
322
+ border-radius: 50%;
323
+ background: var(--brand-emerald);
324
+ animation: pulse 1.4s infinite;
325
+ }
326
+ @keyframes pulse {
327
+ 0% { transform: scale(0.9); opacity: 0.7; }
328
+ 50% { transform: scale(1.2); opacity: 1; box-shadow: 0 0 8px var(--brand-emerald); }
329
+ 100% { transform: scale(0.9); opacity: 0.7; }
330
+ }
331
+ .custom-progress {
332
+ width: 100%;
333
+ height: 5px;
334
+ background: var(--bg-surface-elevated);
335
+ border-radius: 999px;
336
+ overflow: hidden;
337
+ position: relative;
338
+ }
339
+ .custom-progress-bar {
340
+ height: 100%;
341
+ background: linear-gradient(90deg, var(--brand-teal), var(--brand-purple));
342
+ border-radius: 999px;
343
+ width: 0%;
344
+ transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1);
345
+ }
346
+
347
+ /* Table Card */
348
+ .table-card {
349
+ background: var(--bg-surface);
350
+ border: 1px solid var(--border-subtle);
351
+ border-radius: var(--radius-lg);
352
+ box-shadow: var(--shadow-sm);
353
+ overflow: hidden;
354
+ }
355
+ .table-toolbar {
356
+ padding: 12px 18px;
357
+ display: flex;
358
+ align-items: center;
359
+ justify-content: space-between;
360
+ border-bottom: 1px solid var(--border-subtle);
361
+ font-size: 12px;
362
+ color: var(--text-secondary);
363
+ flex-wrap: wrap;
364
+ gap: 8px;
365
+ }
366
+ .table-scroll {
367
+ overflow-x: auto;
368
+ }
369
+ table {
370
+ width: 100%;
371
+ border-collapse: collapse;
372
+ font-variant-numeric: tabular-nums;
373
+ text-align: left;
374
+ }
375
+ th {
376
+ background: var(--bg-surface-elevated);
377
+ font-size: 11px;
378
+ font-weight: 600;
379
+ text-transform: uppercase;
380
+ letter-spacing: 0.04em;
381
+ color: var(--text-muted);
382
+ padding: 11px 14px;
383
+ white-space: nowrap;
384
+ cursor: pointer;
385
+ user-select: none;
386
+ border-bottom: 1px solid var(--border-subtle);
387
+ transition: color 0.15s ease;
388
+ }
389
+ th:hover {
390
+ color: var(--text-primary);
391
+ }
392
+ th.sort-active {
393
+ color: var(--brand-teal);
394
+ }
395
+ th.sort-active::after {
396
+ content: " " attr(data-sort-arrow);
397
+ font-size: 10px;
398
+ }
399
+ th.num, td.num {
400
+ text-align: right;
401
+ }
402
+ td {
403
+ padding: 11px 14px;
404
+ border-bottom: 1px solid var(--border-subtle);
405
+ font-size: 13px;
406
+ color: var(--text-primary);
407
+ vertical-align: middle;
408
+ white-space: nowrap;
409
+ }
410
+ tr:last-child td {
411
+ border-bottom: none;
412
+ }
413
+ tr.model-row:hover td {
414
+ background: var(--bg-surface-hover);
415
+ }
416
+ tr.active-measuring td {
417
+ background: var(--brand-teal-dim);
418
+ }
419
+
420
+ /* Model Name Cell & Badges */
421
+ .model-cell {
422
+ display: flex;
423
+ align-items: center;
424
+ gap: 8px;
425
+ }
426
+ .provider-tag {
427
+ display: inline-block;
428
+ font-size: 10px;
429
+ font-family: var(--font-mono);
430
+ font-weight: 600;
431
+ text-transform: uppercase;
432
+ padding: 2px 6px;
433
+ border-radius: var(--radius-sm);
434
+ background: var(--bg-surface-elevated);
435
+ color: var(--text-secondary);
436
+ border: 1px solid var(--border-subtle);
437
+ letter-spacing: 0.02em;
438
+ }
439
+ .provider-tag.grok { color: #f43f5e; background: rgba(244, 63, 94, 0.1); border-color: rgba(244, 63, 94, 0.2); }
440
+ .provider-tag.deepseek { color: #3b82f6; background: rgba(59, 130, 246, 0.1); border-color: rgba(59, 130, 246, 0.2); }
441
+ .provider-tag.google { color: #10b981; background: rgba(16, 185, 129, 0.1); border-color: rgba(16, 185, 129, 0.2); }
442
+ .provider-tag.codex { color: #a855f7; background: rgba(168, 85, 247, 0.1); border-color: rgba(168, 85, 247, 0.2); }
443
+ .provider-tag.group { color: #f59e0b; background: rgba(245, 158, 11, 0.1); border-color: rgba(245, 158, 11, 0.2); }
444
+
445
+ .model-name {
446
+ font-weight: 600;
447
+ font-size: 13px;
448
+ color: var(--text-primary);
449
+ }
450
+ .model-meta {
451
+ display: block;
452
+ font-size: 11px;
453
+ color: var(--text-muted);
454
+ margin-top: 1px;
455
+ }
456
+
457
+ /* Throughput Speed Bars */
458
+ .speed-cell {
459
+ display: flex;
460
+ flex-direction: column;
461
+ align-items: flex-end;
462
+ gap: 3px;
463
+ min-width: 110px;
464
+ }
465
+ .speed-num {
466
+ font-family: var(--font-mono);
467
+ font-weight: 700;
468
+ font-size: 13px;
469
+ }
470
+ .speed-track {
471
+ width: 100%;
472
+ height: 4px;
473
+ background: var(--bg-surface-elevated);
474
+ border-radius: 99px;
475
+ overflow: hidden;
476
+ }
477
+ .speed-fill-e2e {
478
+ height: 100%;
479
+ background: linear-gradient(90deg, #6366f1, #8b5cf6);
480
+ border-radius: 99px;
481
+ }
482
+ .speed-fill-stream {
483
+ height: 100%;
484
+ background: linear-gradient(90deg, #0d9488, #14b8a6);
485
+ border-radius: 99px;
486
+ }
487
+
488
+ /* Status Badges */
489
+ .status-badge {
490
+ display: inline-flex;
491
+ align-items: center;
492
+ gap: 4px;
493
+ padding: 2px 7px;
494
+ border-radius: var(--radius-sm);
495
+ font-size: 11px;
496
+ font-weight: 500;
497
+ }
498
+ .badge-ok { background: rgba(16, 185, 129, 0.12); color: var(--brand-emerald); }
499
+ .badge-warn { background: rgba(245, 158, 11, 0.12); color: var(--brand-amber); }
500
+ .badge-error { background: rgba(244, 63, 94, 0.12); color: var(--brand-rose); }
501
+
502
+ /* TTFT Color Highlights */
503
+ .ttft-fast { color: var(--brand-emerald); font-weight: 600; font-family: var(--font-mono); }
504
+ .ttft-med { color: var(--text-primary); font-family: var(--font-mono); }
505
+ .ttft-slow { color: var(--brand-amber); font-family: var(--font-mono); }
506
+
507
+ /* Toast */
508
+ .toast {
509
+ position: fixed;
510
+ bottom: 24px;
511
+ right: 24px;
512
+ background: var(--bg-surface-elevated);
513
+ border: 1px solid var(--border-subtle);
514
+ box-shadow: var(--shadow-md);
515
+ padding: 10px 18px;
516
+ border-radius: var(--radius-md);
517
+ font-size: 13px;
518
+ font-weight: 500;
519
+ color: var(--text-primary);
520
+ display: flex;
521
+ align-items: center;
522
+ gap: 8px;
523
+ opacity: 0;
524
+ transform: translateY(10px);
525
+ transition: all 0.2s ease;
526
+ pointer-events: none;
527
+ z-index: 1000;
528
+ }
529
+ .toast.show {
530
+ opacity: 1;
531
+ transform: translateY(0);
532
+ }
533
+
534
+ /* Explanation card */
535
+ .info-footer {
536
+ display: grid;
537
+ grid-template-columns: 1fr 1fr;
538
+ gap: 16px;
539
+ font-size: 12px;
540
+ color: var(--text-secondary);
541
+ line-height: 1.6;
542
+ margin-top: 8px;
543
+ }
544
+ @media (max-width: 768px) {
545
+ .info-footer { grid-template-columns: 1fr; }
546
+ }
547
+ .info-box {
548
+ background: var(--bg-surface);
549
+ border: 1px solid var(--border-subtle);
550
+ border-radius: var(--radius-md);
551
+ padding: 14px 16px;
552
+ }
553
+ .info-box strong {
554
+ color: var(--text-primary);
555
+ display: block;
556
+ margin-bottom: 4px;
557
+ }
558
+ </style>
559
+ </head>
560
+ <body>
561
+ <div class="app-container">
562
+ <!-- Header -->
563
+ <header>
564
+ <div class="brand-wrap">
565
+ <div class="brand-icon">⚡</div>
566
+ <div class="header-text">
567
+ <h1>模型测速基准</h1>
568
+ <p>对比可见正文流式输出速率与端到端综合吞吐量 · 串行实测</p>
569
+ </div>
570
+ </div>
571
+ <div class="header-right">
572
+ <span class="badge-pill" id="target-summary">就绪</span>
573
+ </div>
574
+ </header>
575
+
576
+ <!-- KPI Dashboard Cards -->
577
+ <section class="kpi-grid">
578
+ <div class="kpi-card">
579
+ <div class="kpi-label">已测模型 / 进度</div>
580
+ <div class="kpi-val" id="kpi-count">— / —</div>
581
+ <div class="kpi-sub" id="kpi-count-sub">等待开始</div>
582
+ </div>
583
+ <div class="kpi-card">
584
+ <div class="kpi-label">最高端到端速度 (E2E)</div>
585
+ <div class="kpi-val purple" id="kpi-top-e2e">—</div>
586
+ <div class="kpi-sub" id="kpi-top-e2e-name">综合排队/思考/首字与正文</div>
587
+ </div>
588
+ <div class="kpi-card">
589
+ <div class="kpi-label">最高正文流速 (Stream)</div>
590
+ <div class="kpi-val teal" id="kpi-top-stream">—</div>
591
+ <div class="kpi-sub" id="kpi-top-stream-name">纯正文吐字窗口速率</div>
592
+ </div>
593
+ <div class="kpi-card">
594
+ <div class="kpi-label">最佳首字延迟 (TTFT)</div>
595
+ <div class="kpi-val blue" id="kpi-best-ttft">—</div>
596
+ <div class="kpi-sub" id="kpi-best-ttft-name">从发起到首个正文字符</div>
597
+ </div>
598
+ </section>
599
+
600
+ <!-- Controls Card -->
601
+ <section class="control-card">
602
+ <div class="control-row">
603
+ <div class="control-left">
604
+ <select id="target" aria-label="选择测速范围">
605
+ <option value="">全部模型 · 1 轮</option>
606
+ </select>
607
+ <label class="checkbox-pill">
608
+ <input type="checkbox" id="groups">
609
+ <span>包含路由组</span>
610
+ </label>
611
+ <button id="start" class="btn-primary" disabled>
612
+ <span>▶ 开始测速</span>
613
+ </button>
614
+ <button id="stop" class="btn-stop" hidden>
615
+ <span>⏹ 停止</span>
616
+ </button>
617
+ </div>
618
+
619
+ <div class="control-right">
620
+ <input type="text" id="filter" placeholder="🔍 搜索模型或供应商…" aria-label="搜索模型">
621
+ <button id="btn-copy-md" class="btn-ghost" title="复制当前数据为 Markdown 表格">
622
+ <span>📋 复制表格</span>
623
+ </button>
624
+ <button id="btn-export-json" class="btn-ghost" title="导出测试结果为 JSON">
625
+ <span>💾 导出 JSON</span>
626
+ </button>
627
+ </div>
628
+ </div>
629
+
630
+ <!-- Progress & Live Status -->
631
+ <div class="progress-section" id="progress-container" hidden>
632
+ <div class="status-row">
633
+ <div class="live-status">
634
+ <span class="dot-live"></span>
635
+ <span id="status">正在测速…</span>
636
+ </div>
637
+ <span id="plan-text">预计 23 次请求 · 每次最多 256 输出 token</span>
638
+ </div>
639
+ <div class="custom-progress">
640
+ <div class="custom-progress-bar" id="progress-bar"></div>
641
+ </div>
642
+ </div>
643
+ </section>
644
+
645
+ <!-- Benchmark Data Table Card -->
646
+ <section class="table-card">
647
+ <div class="table-toolbar">
648
+ <span id="table-summary-info">点击任意表头可快速排序</span>
649
+ <span id="median-summary" style="font-weight: 600; color: var(--brand-teal);"></span>
650
+ </div>
651
+ <div class="table-scroll">
652
+ <table id="benchmark-table">
653
+ <thead>
654
+ <tr>
655
+ <th data-col="id" style="min-width: 200px;">模型 / 供应商</th>
656
+ <th data-col="e2eSpeed" class="num sort-active" data-sort-arrow="▼" style="min-width: 140px;">端到端速度</th>
657
+ <th data-col="speed" class="num" style="min-width: 140px;">正文流速</th>
658
+ <th data-col="ttft" class="num" style="min-width: 90px;">首字延迟</th>
659
+ <th data-col="tokens" class="num" style="min-width: 90px;">正文 Token</th>
660
+ <th data-col="total" class="num" style="min-width: 90px;">总耗时</th>
661
+ <th data-col="status" style="min-width: 120px;">状态 / 备注</th>
662
+ </tr>
663
+ </thead>
664
+ <tbody id="table-body">
665
+ <tr>
666
+ <td colspan="7" style="text-align: center; color: var(--text-muted); padding: 36px;">正在连接模型服务并读取配置…</td>
667
+ </tr>
668
+ </tbody>
669
+ </table>
670
+ </div>
671
+ </section>
672
+
673
+ <!-- Footnote: Impeccable metrics explanation -->
674
+ <footer class="info-footer">
675
+ <div class="info-box">
676
+ <strong>端到端速度 (Tokens / Total Duration)</strong>
677
+ 包含了网络建立、队列排队、Prompt 预填充、模型内部思考(Reasoning)以及正文输出的完整耗时。反映从发出指令到完整拿到答案的真实综合速率。
678
+ </div>
679
+ <div class="info-box">
680
+ <strong>正文流速 (Tokens / Streaming Window)</strong>
681
+ 排除了首字等待与思考延迟,仅统计首个可见字符到达至最后一个字符到达之间的打字机喷发流速。Fast 类投机采样模型在此项往往极高。
682
+ </div>
683
+ </footer>
684
+ </div>
685
+
686
+ <div class="toast" id="toast">已复制到剪贴板</div>
687
+ <script src="app.js"></script>
688
+ </body>
689
+ </html>
package/benchmark.mjs ADDED
@@ -0,0 +1,84 @@
1
+ const prompt = `Copy the passage below verbatim. Begin with The.
2
+
3
+ The path from the house to the river runs through a quiet garden. Small birds rest on the fence and watch the water. In the morning I walk slowly past the old apple tree and turn left at the wooden gate. A narrow bridge leads to a field of green grass. Beyond the field a small boat is waiting at the edge of the river. The water is clear enough to see stones on the river bed. I sit on a flat rock and listen to the wind in the trees.`
4
+ const headers = { authorization: "Bearer magpie-model-speedtest", "content-type": "application/json" }
5
+
6
+ export async function catalog(base) {
7
+ const response = await fetch(`${base}/v1/models`, { headers, signal: AbortSignal.timeout(10000), redirect: "error" })
8
+ if (!response.ok) throw new Error(`模型目录 HTTP ${response.status}`)
9
+ const { data } = await response.json()
10
+ if (!Array.isArray(data)) throw new Error("网关未返回模型目录")
11
+ return data.filter(model => typeof model.id === "string" && (!model.modalities?.output || model.modalities.output.includes("text")))
12
+ .map(model => ({
13
+ id: model.id, group: model.id.startsWith("group/"),
14
+ effort: ["none", "minimal", "low"].find(level => model.reasoning_levels?.includes(level)),
15
+ cap: Math.min(256, model.max_output_tokens || 256)
16
+ }))
17
+ }
18
+
19
+ export async function measure(base, model, signal) {
20
+ const started = performance.now()
21
+ const response = await fetch(`${base}/v1/chat/completions`, {
22
+ method: "POST", headers, redirect: "error", signal,
23
+ body: JSON.stringify({ model: model.id, messages: [{ role: "user", content: prompt }], stream: true,
24
+ stream_options: { include_usage: true }, max_tokens: model.cap, reasoning_effort: model.effort })
25
+ })
26
+ if (!response.ok) {
27
+ await response.body?.cancel()
28
+ throw new Error(`HTTP ${response.status} ${response.statusText}`.trim())
29
+ }
30
+ const routed = response.headers.get("x-magpie-model")
31
+ if (response.headers.get("content-type")?.includes("application/json")) {
32
+ const reply = await response.json()
33
+ if (reply.error || !reply.choices?.[0]?.message?.content) throw new Error("网关未返回可见正文")
34
+ const total = performance.now() - started
35
+ const text = reply.choices[0].message.content
36
+ const reasoning = reply.usage?.completion_tokens_details?.reasoning_tokens
37
+ const exact = Number.isInteger(reasoning) && reasoning >= 0 && Number.isInteger(reply.usage?.completion_tokens) && reply.usage.completion_tokens > reasoning
38
+ const tokens = exact ? reply.usage.completion_tokens - reasoning : Math.max(1, Math.round(Buffer.byteLength(text) / 4))
39
+ const e2eSpeed = total >= 10 && tokens > 0 ? tokens * 1000 / total : null
40
+ return { actual: routed || reply.model, total, ttft: total, speed: null, e2eSpeed, tokens, exact, note: "整段返回" }
41
+ }
42
+ return readStream(response.body, started, routed)
43
+ }
44
+
45
+ export async function readStream(body, started, routed, now = () => performance.now()) {
46
+ const decoder = new TextDecoder()
47
+ let buffer = "", first, last, firstBytes = 0, bytes = 0, model, usage, finished = false, done = false
48
+ for await (const chunk of body) {
49
+ const arrival = now()
50
+ buffer += decoder.decode(chunk, { stream: true })
51
+ const frames = buffer.split(/\r?\n\r?\n/)
52
+ buffer = frames.pop()
53
+ for (const frame of frames) {
54
+ const payload = frame.split(/\r?\n/).filter(line => line.startsWith("data:")).map(line => line.slice(5).trimStart()).join("\n")
55
+ if (!payload) continue
56
+ if (payload === "[DONE]") { done = true; break }
57
+ const event = JSON.parse(payload)
58
+ if (event.error) throw new Error(`网关流错误${event.error.code ? ` (${String(event.error.code).slice(0, 60)})` : ""}`)
59
+ model = event.model || model
60
+ usage = event.usage || usage
61
+ const choice = event.choices?.[0]
62
+ if (choice?.finish_reason === "error" || choice?.finish_reason === "content_filter") throw new Error(`生成终止 (${choice.finish_reason})`)
63
+ finished ||= !!choice?.finish_reason
64
+ const text = choice?.delta?.content
65
+ if (typeof text !== "string" || !text) continue
66
+ bytes += Buffer.byteLength(text)
67
+ first ??= arrival
68
+ last = arrival
69
+ if (first === arrival) firstBytes = bytes
70
+ }
71
+ if (done) break
72
+ }
73
+ if (!done && (!finished || buffer.trim())) throw new Error("流提前中断")
74
+ if (!bytes) throw new Error("没有可见正文(可能已用完输出预算)")
75
+ const reasoning = usage?.completion_tokens_details?.reasoning_tokens
76
+ const exact = Number.isInteger(reasoning) && reasoning >= 0 && Number.isInteger(usage?.completion_tokens) && usage.completion_tokens > reasoning
77
+ const tokens = exact ? usage.completion_tokens - reasoning : Math.max(1, Math.round(bytes / 4))
78
+ const window = last - first
79
+ const speed = window >= 10 ? tokens * (1 - firstBytes / bytes) * 1000 / window : null
80
+ const total = now() - started
81
+ const e2eSpeed = total >= 10 && tokens > 0 ? tokens * 1000 / total : null
82
+ return { actual: routed || model, total, ttft: first - started, speed, e2eSpeed, tokens, exact,
83
+ note: speed === null ? "正文集中到达" : exact ? "用量计数,速率为估算" : "按英文正文约 4 字节/token 估算" }
84
+ }
package/index.mjs ADDED
@@ -0,0 +1,99 @@
1
+ import { createServer } from "node:http"
2
+ import { randomBytes } from "node:crypto"
3
+ import { readFile } from "node:fs/promises"
4
+ import { join } from "node:path"
5
+ import { catalog, measure } from "./benchmark.mjs"
6
+
7
+ async function gateway(directory) {
8
+ const { addr } = JSON.parse(await readFile(join(directory, "gateway.json"), "utf8"))
9
+ const port = new URL(`http://${addr}`).port
10
+ if (!port) throw new Error("无法读取 Magpie 网关端口")
11
+ return `http://127.0.0.1:${port}`
12
+ }
13
+
14
+ let server
15
+ export default {
16
+ id: "magpie-model-speedtest",
17
+ async server(input, options = {}) {
18
+ if (server?.listening) return {}
19
+ const port = Number(options.port || 3487)
20
+ const token = randomBytes(32).toString("hex")
21
+ let state = { rows: [], count: 0, completed: 0, running: false }
22
+ let control
23
+ async function run(base, models, rounds, signal) {
24
+ try {
25
+ for (let round = 1; round <= rounds; round++) {
26
+ for (const model of models) {
27
+ if (signal.aborted) return
28
+ state.current = model.id
29
+ const row = { id: model.id, round }
30
+ try {
31
+ const deadline = AbortSignal.timeout(60000)
32
+ Object.assign(row, await measure(base, model, AbortSignal.any([signal, deadline])))
33
+ } catch (error) {
34
+ if (signal.aborted) return
35
+ row.error = error.name === "TimeoutError" ? "60 秒超时" : error.message
36
+ }
37
+ state.rows.push(row)
38
+ state.completed++
39
+ }
40
+ }
41
+ } finally {
42
+ state.running = false
43
+ state.current = null
44
+ state.stopped = signal.aborted
45
+ control = undefined
46
+ }
47
+ }
48
+ server = createServer(async (req, res) => {
49
+ const path = req.url?.split("?")[0]
50
+ const reply = (status, value) => { res.writeHead(status, { "Content-Type": "application/json" }); res.end(JSON.stringify(value)) }
51
+ res.setHeader("Cache-Control", "no-store")
52
+ res.setHeader("Referrer-Policy", "no-referrer")
53
+ res.setHeader("X-Content-Type-Options", "nosniff")
54
+ if (![`127.0.0.1:${port}`, `localhost:${port}`].includes(req.headers.host)) return reply(403, {})
55
+ const origin = req.headers.origin
56
+ if (origin && origin !== "null" && origin !== `http://${req.headers.host}`) return reply(403, {})
57
+ try {
58
+ if (req.method === "GET" && path === "/") {
59
+ const html = await readFile(new URL("./app/index.html", import.meta.url), "utf8")
60
+ res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" })
61
+ return res.end(html.replace("__SESSION__", token))
62
+ }
63
+ if (req.method === "GET" && path === "/app.js") {
64
+ res.writeHead(200, { "Content-Type": "application/javascript; charset=utf-8" })
65
+ return res.end(await readFile(new URL("./app/app.js", import.meta.url)))
66
+ }
67
+ if (req.method !== "POST") return reply(405, {})
68
+ let text = ""
69
+ for await (const part of req) {
70
+ text += part
71
+ if (text.length > 8192) return reply(413, {})
72
+ }
73
+ let args
74
+ try { args = JSON.parse(text) } catch { return reply(400, {}) }
75
+ if (origin) res.setHeader("Access-Control-Allow-Origin", origin)
76
+ if (args?.token !== token) return reply(403, { reload: true })
77
+ if (path === "/state") return reply(200, state)
78
+ if (path === "/stop") { control?.abort(); return reply(200, {}) }
79
+ const base = await gateway(input.directory)
80
+ if (path === "/models") return reply(200, await catalog(base))
81
+ if (path !== "/start") return reply(404, {})
82
+ const available = await catalog(base)
83
+ if (state.running) return reply(409, { error: "已有测速正在运行" })
84
+ const models = available.filter(model => args.id ? model.id === args.id : args.groups === true || !model.group)
85
+ if (!models.length) return reply(400, { error: "当前没有符合条件的模型" })
86
+ const rounds = args.id ? 3 : 1
87
+ control = new AbortController()
88
+ state = { rows: [], running: true, count: models.length * rounds, completed: 0 }
89
+ void run(base, models, rounds, control.signal)
90
+ reply(200, state)
91
+ } catch (error) {
92
+ if (!res.headersSent) reply(502, { error: error.message })
93
+ else res.end()
94
+ }
95
+ })
96
+ await new Promise((resolve, reject) => server.once("error", reject).listen(port, "127.0.0.1", resolve))
97
+ return {}
98
+ }
99
+ }
package/package.json CHANGED
@@ -1,6 +1,21 @@
1
1
  {
2
2
  "name": "magpie-model-speedtest",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "description": "One-click end-to-end throughput, streaming token speed, and first-text latency benchmarks for models in your Magpie gateway.",
5
+ "type": "module",
6
+ "main": "./index.mjs",
7
+ "files": ["index.mjs", "benchmark.mjs", "page", "app", "README.md"],
8
+ "keywords": ["magpie", "magpie-plugin", "opencode", "opencode-plugin", "benchmark", "tokens-per-second"],
9
+ "scripts": {"test": "node --test test/*.test.mjs"},
10
+ "magpie": {
11
+ "page": {
12
+ "title": "Model Speed Test",
13
+ "titles": {"zh": "模型测速", "zh-TW": "模型測速"},
14
+ "file": "page/index.html",
15
+ "service": {"port": 3487, "option": "port"}
16
+ },
17
+ "options": {"port": 3487},
18
+ "permissions": [],
19
+ "summary": {"zh": "一键比较模型端到端综合速度、正文输出速度和首字延迟,可选路由组和三轮复测。", "en": "Benchmark end-to-end throughput, streaming token speed, and time to first text across enabled models."}
20
+ }
21
+ }
package/page/boot.js ADDED
@@ -0,0 +1,6 @@
1
+ (async () => {
2
+ await window.magpie?.ready
3
+ const options = await window.magpie?.options?.get?.()
4
+ const base = window.magpie?.service || `http://127.0.0.1:${Number(options?.port) || 3487}`
5
+ location.replace(base.replace(/\/?$/, "/"))
6
+ })().catch(() => { document.querySelector("#loading").textContent = "连接失败,请重新打开模型测速。" })
@@ -0,0 +1,5 @@
1
+ <!doctype html>
2
+ <html lang="zh-CN"><meta charset="utf-8"><title>模型测速</title>
3
+ <p id="loading">正在连接测速服务…</p>
4
+ <script src="_magpie/bridge.js"></script><script src="boot.js"></script>
5
+ </html>