Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
255 changes: 255 additions & 0 deletions examples/scenarios/agent-workflow-latency-diagnosis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
/**
* Agent 工作流耗时诊断报告
* 场景:在单个串行工作流中定位慢请求的主要耗时阶段。
* 要点:从同一批模拟运行记录计算 P95、超时率、阶段占比和最慢请求明细。
*/
import { Chart } from '@antv/g2';

const stages = [
{ key: 'planning', label: '规划' },
{ key: 'retrieval', label: '检索' },
{ key: 'tools', label: '工具执行' },
{ key: 'generation', label: '结果生成' },
] as const;

const slowThresholdMs = 2000;
const timeoutThresholdMs = 3000;

const runs = [
...Array.from({ length: 16 }, (_, index) => {
const sample = index + 1;
return {
id: `run-${String(sample).padStart(2, '0')}`,
planning: 180 + 10 * (sample % 4),
retrieval: 260 + 20 * (sample % 3),
tools: 480 + 30 * (sample % 5),
generation: 420 + 20 * (sample % 4),
};
}),
{ id: 'run-17', planning: 210, retrieval: 300, tools: 1280, generation: 450 },
{ id: 'run-18', planning: 220, retrieval: 350, tools: 1630, generation: 500 },
{ id: 'run-19', planning: 220, retrieval: 360, tools: 2010, generation: 610 },
{ id: 'run-20', planning: 230, retrieval: 420, tools: 2250, generation: 700 },
].map((run) => ({
...run,
totalMs: stages.reduce((total, { key }) => total + run[key], 0),
}));

const orderedRuns = [...runs].sort((a, b) => a.totalMs - b.totalMs);
const p95 = orderedRuns[Math.ceil(0.95 * orderedRuns.length) - 1].totalMs;
const timeoutCount = runs.filter((run) => run.totalMs >= timeoutThresholdMs).length;
const timeoutRate = timeoutCount / runs.length;
const slowRuns = runs.filter((run) => run.totalMs >= slowThresholdMs);
const totalSlowMs = slowRuns.reduce((total, run) => total + run.totalMs, 0);
const stageShares = stages
.map(({ key, label }) => {
const durationMs = slowRuns.reduce((total, run) => total + run[key], 0);
return { label, durationMs, share: totalSlowMs ? durationMs / totalSlowMs : 0 };
})
.sort((a, b) => b.durationMs - a.durationMs);
const leadingStage = stageShares[0];
const slowestRuns = [...runs]
.sort((a, b) => b.totalMs - a.totalMs || a.id.localeCompare(b.id))
.slice(0, 4);

const formatMs = new Intl.NumberFormat('zh-CN').format;
const formatPercent = new Intl.NumberFormat('zh-CN', {
style: 'percent',
maximumFractionDigits: 1,
}).format;

const container = document.getElementById('container');
if (!container) throw new Error('Agent 耗时诊断报告需要一个 #container 容器。');

const styles = document.createElement('style');
styles.textContent = `
.agent-latency-report {
color: #1c2c3f;
background: #f1f5f9;
box-sizing: border-box;
font: 15px/1.55 system-ui, "Microsoft YaHei", sans-serif;
padding: clamp(14px, 4vw, 42px);
}
.agent-latency-report *, .agent-latency-report *::before,
.agent-latency-report *::after { box-sizing: inherit; }
.agent-latency-report .report-body {
display: grid;
gap: 16px;
max-width: 1000px;
margin-inline: auto;
}
.agent-latency-report h1, .agent-latency-report h2, .agent-latency-report p {
margin: 0;
}
.agent-latency-report h1 {
font-size: clamp(23px, 4vw, 38px);
line-height: 1.2;
margin-bottom: 8px;
}
.agent-latency-report h2 { font-size: 19px; line-height: 1.35; }
.agent-latency-report .detail { color: #516279; font-size: 13px; }
.agent-latency-report .metrics {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 12px;
}
.agent-latency-report .card {
background: #fff;
border: 1px solid #dce5ef;
border-radius: 12px;
min-width: 0;
padding: clamp(12px, 2.2vw, 20px);
}
.agent-latency-report .metric-value {
display: block;
font-size: clamp(23px, 4.2vw, 34px);
font-variant-numeric: tabular-nums;
line-height: 1.3;
margin-block: 3px;
}
.agent-latency-report .metric-label { color: #42596f; font-weight: 600; }
.agent-latency-report .diagnosis {
color: #23499b;
font-weight: 600;
margin-top: 5px;
}
.agent-latency-report .chart-host { height: 245px; min-width: 0; }
.agent-latency-report .table-scroll {
overflow-x: auto;
margin-top: 10px;
}
.agent-latency-report table {
border-collapse: collapse;
font-size: 13px;
font-variant-numeric: tabular-nums;
min-width: 520px;
text-align: right;
white-space: nowrap;
width: 100%;
}
.agent-latency-report th, .agent-latency-report td {
border-bottom: 1px solid #e2e9f0;
padding: 9px 12px;
}
.agent-latency-report thead { color: #42596f; background: #f2f6fb; }
.agent-latency-report th:first-child { text-align: left; }
.agent-latency-report tr:last-child td,
.agent-latency-report tbody tr:last-child th { border-bottom: 0; }
.agent-latency-report .timeout { color: #8a351c; font-weight: 600; }
.agent-latency-report .mobile-hint { display: none; }
@media (max-width: 650px) {
.agent-latency-report .mobile-hint { display: inline; }
.agent-latency-report .chart-host { height: 222px; }
.agent-latency-report table { min-width: 450px; }
.agent-latency-report th, .agent-latency-report td { padding-inline: 7px; }
}
`;

const report = document.createElement('main');
report.className = 'agent-latency-report';
report.innerHTML = `
<div class="report-body">
<header>
<h1>Agent 工作流耗时诊断报告</h1>
<p class="detail" data-role="scope"></p>
</header>
<section class="metrics" aria-label="耗时摘要">
<div class="card">
<p class="metric-label">端到端 P95</p>
<strong class="metric-value" data-role="p95"></strong>
<p class="detail">全量请求 · 最近秩法</p>
</div>
<div class="card">
<p class="metric-label">模拟超时率</p>
<strong class="metric-value" data-role="timeout-rate"></strong>
<p class="detail" data-role="timeout-scope"></p>
</div>
</section>
<section class="card" aria-labelledby="agent-latency-stage-title">
<h2 id="agent-latency-stage-title">慢请求的阶段耗时占比</h2>
<p class="diagnosis" data-role="diagnosis"></p>
<p class="detail" data-role="slow-scope"></p>
<div class="chart-host" role="img"></div>
</section>
<section class="card" aria-labelledby="agent-latency-run-title">
<h2 id="agent-latency-run-title">最慢的四次运行</h2>
<p class="detail">耗时单位为 ms,按端到端耗时从高到低排列。<span class="mobile-hint">横向滚动查看全部阶段。</span></p>
<div class="table-scroll" tabindex="0" role="region" aria-label="分阶段耗时明细">
<table aria-labelledby="agent-latency-run-title">
<thead><tr>
<th scope="col">运行 ID</th><th scope="col">端到端</th>
<th scope="col">规划</th><th scope="col">检索</th>
<th scope="col">工具执行</th><th scope="col">结果生成</th>
<th scope="col">状态</th>
</tr></thead>
<tbody></tbody>
</table>
</div>
</section>
</div>
`;
container.append(styles, report);

report.querySelector('[data-role="scope"]')!.textContent =
`串行工作流 · ${runs.length} 次模拟运行 · 端到端耗时为四个阶段之和`;
report.querySelector('[data-role="p95"]')!.textContent = `${formatMs(p95)} ms`;
report.querySelector('[data-role="timeout-rate"]')!.textContent = formatPercent(timeoutRate);
report.querySelector('[data-role="timeout-scope"]')!.textContent =
`${timeoutCount}/${runs.length} 条 ≥ ${formatMs(timeoutThresholdMs)} ms`;
report.querySelector('[data-role="diagnosis"]')!.textContent = totalSlowMs
? `优先排查${leadingStage.label},占慢请求累计耗时的 ${formatPercent(leadingStage.share)}。`
: '本批样本没有慢请求。';
report.querySelector('[data-role="slow-scope"]')!.textContent =
`${slowRuns.length} 条慢请求(≥ ${formatMs(slowThresholdMs)} ms),累计 ${formatMs(totalSlowMs)} ms`;

const tbody = report.querySelector('tbody')!;
for (const run of slowestRuns) {
const row = tbody.insertRow();
const rowHeading = document.createElement('th');
rowHeading.scope = 'row';
rowHeading.textContent = run.id;
row.append(rowHeading);
for (const duration of [run.totalMs, ...stages.map(({ key }) => run[key])]) {
row.insertCell().textContent = formatMs(duration);
}
const status = row.insertCell();
const timedOut = run.totalMs >= timeoutThresholdMs;
status.textContent = timedOut ? '模拟超时' : '未超时';
status.classList.toggle('timeout', timedOut);
}

const chartHost = report.querySelector<HTMLDivElement>('.chart-host')!;
chartHost.setAttribute(
'aria-label',
`慢请求阶段耗时占比:${stageShares.map(({ label, share }) => `${label} ${formatPercent(share)}`).join('、')}`,
);
const chart = new Chart({ container: chartHost, autoFit: true });
chart.options({
type: 'interval',
data: stageShares,
coordinate: { transform: [{ type: 'transpose' }] },
encode: { x: 'label', y: 'share', color: 'label' },
scale: {
x: { domain: stageShares.map(({ label }) => label) },
y: { domain: [0, 1], nice: false, tickCount: 6 },
color: { domain: stageShares.map(({ label }) => label), range: ['#355BD6', '#8198BF', '#8198BF', '#8198BF'] },
},
axis: {
x: { title: false, labelAutoHide: false, labelFill: '#516279' },
y: { title: false, labelFormatter: '.0%', labelAutoRotate: false, labelFill: '#516279', grid: true },
},
legend: false,
labels: [{
text: ({ share }: { share: number }) => formatPercent(share),
position: 'right',
style: { fill: '#1c2c3f', fontWeight: 600, textAlign: 'start', dx: 6 },
}],
tooltip: {
title: 'label',
items: [
{ field: 'durationMs', name: '累计耗时', valueFormatter: (value) => `${formatMs(Number(value))} ms` },
{ field: 'share', name: '阶段占比', valueFormatter: (value) => formatPercent(Number(value)) },
],
},
});
chart.render();
40 changes: 40 additions & 0 deletions scenarios/agent-workflow-latency-diagnosis.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Agent 工作流耗时诊断报告

## 适用场景

面向负责排查 Agent 请求延迟的开发者。先用端到端 P95 与超时率了解这批请求的整体表现,再用慢请求的阶段占比定位累计耗时最高的步骤,最后在最慢的几次运行中核对分阶段记录。

示例假设规划、检索、工具执行和结果生成依次串行执行。存在并行节点的工作流应使用跟踪系统记录的实际起止时间计算端到端耗时,并采用能够反映重叠时间的归因口径。

## 输入与数据口径

案例内置 20 次模拟运行。每条记录包含唯一运行 ID,以及四个阶段的毫秒耗时。所有指标、条形图和明细都从这批记录计算。

| 指标 | 统计范围和算法 | 样例结果 |
|---|---|---|
| 端到端耗时 | 每次运行的四阶段耗时之和,单位为 ms | 最慢一次为 3,600 ms |
| 端到端 P95 | 全部 20 条按端到端耗时升序排列,最近秩为 `ceil(0.95 × N)`,取第 19 条 | 3,200 ms |
| 模拟超时率 | 端到端耗时达到 3,000 ms 的运行数除以总运行数。模拟记录保留全段观测 | 2 / 20 = 10% |
| 慢请求 | 端到端耗时达到 2,000 ms 的运行 | 4 条,共 11,740 ms |
| 慢请求的阶段占比 | 该阶段在 4 条慢请求中的累计耗时除以 11,740 ms。按原始占比排序,展示时再取一位小数 | 工具执行为 7,170 / 11,740,显示为 61.1% |
| 最慢运行明细 | 按端到端耗时降序取前四条。同耗时按运行 ID 排序 | run-20、run-19、run-18、run-17 |

## 实现步骤

1. 为每条运行汇总端到端毫秒数,再按阈值分别计算全量超时率和慢请求样本。
2. 将 P95 与超时率放在指标卡中。按阶段汇总慢请求的耗时,按占比降序绘制横向条形图。图的比例尺从 0% 到 100%,重点色标出占比最高的阶段,结论文字给出同一阶段及占比。
3. 在明细表中逐条给出慢请求的端到端耗时、分阶段耗时和模拟超时状态。窄屏下横向滚动表格,保留完整字段。
4. 让页面加载 [TypeScript 示例](../examples/scenarios/agent-workflow-latency-diagnosis.ts),并提供空的 `<div id="container"></div>`。示例使用原生 DOM 创建容器和表格,再使用 G2 v5 渲染条形图。参考 [条形图组件](../charts/bar.md)与 [G2 v5 API 速查](../references/g2-v5-cheatsheet.md)。

## 易错点

- P95 基于各次运行的端到端耗时。将各阶段各自的 P95 相加会改变指标含义。
- 阶段占比是慢请求子集中的「总和除以总和」。逐条算占比再取平均会给快慢不同的请求相同权重。
- 两个阈值都包含边界。耗时达到 2,000 ms 的请求进入慢请求分组,达到 3,000 ms 的请求计入模拟超时。
- 图表和文字使用同一份计算结果。占比展示四舍五入后,各项之和可能与 100% 相差 0.1 个百分点。

## 完整案例

| 案例 | 场景 | 关键配置 |
|---|---|---|
| [agent-workflow-latency-diagnosis.ts](../examples/scenarios/agent-workflow-latency-diagnosis.ts) | 从运行摘要定位慢请求耗时最高的阶段,再追溯到具体运行 | 原生 DOM 指标卡和明细表、G2 v5 `interval` + `transpose`、占比量程 0–100%、自包含模拟数据 |
4 changes: 4 additions & 0 deletions skills/g2plot-v3/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ description: G2Plot v3 可视化组件库,提示词即组件。当用户需要

选型拿不准时,读对应 `charts/<chart>.md` 的「何时使用 / 何时不用」小节。

## 复合场景示例

- [Agent 工作流耗时诊断报告](../../scenarios/agent-workflow-latency-diagnosis.md):用同一批请求组合 P95、超时率、阶段耗时占比和慢请求明细。

## 共享规范(生成前必读)

- G2 v5 API 速查:[references/g2-v5-cheatsheet.md](../../references/g2-v5-cheatsheet.md)
Expand Down
Loading