Files
cindy/apps/mobile/scripts/render-trace-analyze.mjs
Lizi bb3f71d084 chore: Cindy 首次开源提交 / initial open-source release
Cindy 是心动网络 (X.D. Network Inc.) 的跨端 AI Agent 客户端。
本次提交包含 Desktop(Electron)、Mobile(Expo)及全部共享 packages
的完整源码,以 Apache-2.0 协议开源。

This is the first public release of Cindy, X.D. Network's cross-platform
AI agent client: the Electron desktop app, the Expo mobile app, and all
shared workspace packages, licensed under Apache-2.0.

- 许可与合规:根 LICENSE(Apache-2.0)+ NOTICE;第三方组件归属声明与
  SPDX SBOM 收口在 docs/legal/notices/,由 pnpm licenses:generate 生成。
- 社区文件:README / CONTRIBUTING / CODE_OF_CONDUCT / SECURITY / SUPPORT
  中英双语;issue / PR 模板与 CI workflow 在 .github/。
- 协议层以公开 submodule 引入:github.com/makecindy/cindy-protocol。
- 提交前已通过仓库门禁:全量 pnpm test:unit 与涉及 package 的 typecheck。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-24 08:30:44 +08:00

95 lines
3.7 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
/**
* 渲染 trace 组件级归因(配合 render-trace.mjs,dev-only 排查工具)。
* ---------------------------------------------------------------------------
* 用法:
* node apps/mobile/scripts/render-trace-analyze.mjs <trace.json> [--component HomeSessionRow]
* 输出:
* 1. 各组件总渲染耗时 / 次数 top-20(b/e 配对求和,嵌套父含子);
* 2. 目标组件的 renders/秒时间线(风暴 = 持续高值;健康 = 接近全零);
* 3. Changed Props 供词 top-10(React DevTools 标注的重渲染原因,
* "referentially unequal but deeply equal" = 引用不稳定,优先修)。
*
* 判定基准(2026-07-18 风暴修复实测,~2 分钟采样、桌面端流式输出场景):
* - 病态:HomeSessionRow 7586 次、SectionList 全列表 37 次 × ~1.4s;
* - 修复后:HomeSessionRow ~70 次、壳层 pass 收敛到变化行。
* 重构后跑同场景对比这两组量级即可判定回归。大 trace 建议
* node --max-old-space-size=8192 运行。
*/
import { readFileSync } from 'node:fs';
const args = process.argv.slice(2);
const file = args.find((a) => !a.startsWith('--'));
if (!file) {
console.error('用法: node render-trace-analyze.mjs <trace.json> [--component HomeSessionRow]');
process.exit(1);
}
const idx = args.indexOf('--component');
const targetComponent = idx >= 0 && args[idx + 1] ? args[idx + 1] : 'HomeSessionRow';
const { traceEvents } = JSON.parse(readFileSync(file, 'utf8'));
const clean = (name) => name.replace(/​/g, '');
let tMin = Infinity;
let tMax = -Infinity;
for (const ev of traceEvents) {
if (typeof ev.ts === 'number') {
tMin = Math.min(tMin, ev.ts);
tMax = Math.max(tMax, ev.ts);
}
}
console.log(`窗口时长 ${((tMax - tMin) / 1e6).toFixed(1)}s,事件 ${traceEvents.length} 条`);
// b/e 配对:按 name|id 记 open 时刻,配对求和(React 组件 span 嵌套良好,父含子)
const open = new Map();
const total = new Map();
const count = new Map();
const buckets = new Map();
for (const ev of traceEvents) {
if (ev.ph !== 'b' && ev.ph !== 'e') continue;
const name = clean(ev.name);
const key = `${name}|${ev.id}`;
if (ev.ph === 'b') {
open.set(key, ev.ts);
if (name.includes(targetComponent)) {
const sec = Math.floor((ev.ts - tMin) / 1e6);
buckets.set(sec, (buckets.get(sec) ?? 0) + 1);
}
} else {
const t0 = open.get(key);
if (t0 === undefined) continue;
open.delete(key);
total.set(name, (total.get(name) ?? 0) + (ev.ts - t0));
count.set(name, (count.get(name) ?? 0) + 1);
}
}
console.log('\n== 组件总渲染耗时 top-20(父含子)==');
[...total.entries()].sort((a, b) => b[1] - a[1]).slice(0, 20).forEach(([name, dur]) => {
console.log(`${String((dur / 1000).toFixed(0)).padStart(8)} ms ${String(count.get(name)).padStart(7)}x ${name.slice(0, 56)}`);
});
console.log(`\n== ${targetComponent} renders/秒时间线 ==`);
const maxSec = buckets.size > 0 ? Math.max(...buckets.keys()) : -1;
const line = [];
for (let s = 0; s <= maxSec; s += 1) line.push(buckets.get(s) ?? 0);
console.log(line.length > 0 ? line.join(' ') : '(无渲染记录)');
console.log(`合计 ${[...buckets.values()].reduce((a, b) => a + b, 0)} 次`);
console.log('\n== Changed Props 供词 top-10 ==');
const reasons = new Map();
for (const ev of traceEvents) {
if (ev.ph !== 'b' || !ev.args?.detail) continue;
try {
const props = JSON.parse(ev.args.detail)?.devtools?.properties;
if (!props) continue;
const key = `${clean(ev.name)} :: ${JSON.stringify(props).slice(0, 110)}`;
reasons.set(key, (reasons.get(key) ?? 0) + 1);
} catch {
// detail 非 JSON 的事件直接跳过
}
}
[...reasons.entries()].sort((a, b) => b[1] - a[1]).slice(0, 10).forEach(([key, n]) => {
console.log(`${String(n).padStart(7)}x ${key.slice(0, 150)}`);
});