logo
1
0
WeChat Login

feat: 添加启动关键路径性能埋点#11

Closed
created 3 weeks ago
main
feat/performance-tracing-1773205425
Edit
OverviewCommits
1
Files changed
2
AttachmentsTAPD
  • 新增 src/utils/performance.ts 性能工具模块
  • 使用 process.hrtime.bigint() 实现微秒级计时
  • 提供 startPerformanceTrace/endPerformanceTrace API
  • 在关键路径埋点:配置加载、依赖初始化、端口绑定、MCP 服务调用
  • 结构化日志输出(JSON 格式),包含时间戳字段
  • 预留性能阈值告警功能接口
  • 支持性能统计摘要输出

关联 Issue: #6

OpenClaw

NPC
referenced pull request
NPC
reviewed
src/app.ts

问题: 导入的 configurePerformance 函数未被使用

建议: 如果计划在后续版本中使用,建议先不导入;或者立即使用它来配置 PERFORMANCE_THRESHOLDS

configurePerformance({
    thresholds: PERFORMANCE_THRESHOLDS,
    onThresholdExceeded: (trace, threshold) => {
        console.warn(`[performance] ${trace.name} 超过阈值: ${trace.duration}us > ${threshold}us`);
    }
});
src/app.ts

问题: 定义了 PERFORMANCE_THRESHOLDS 但未实际使用

建议: 配置项已定义但未调用 configurePerformance 进行应用。阈值配置应该在某处初始化,否则告警功能无法生效。建议在 main() 函数开始时添加配置初始化。

src/app.ts

问题: 用户输入为空时提前返回,但 CONFIG_LOAD 追踪记录仍在活跃状态

建议: 在提前返回前应结束追踪:

if (!userQuestion.trim()) {
    endPerformanceTrace(TRACE_NAMES.CONFIG_LOAD);
    console.log('[main] 用户输入为空,跳过');
    return;
}
src/utils/performance.ts

问题: 全局变量 completedTraces 持续累积数据,可能导致内存泄漏

建议: 对于长期运行的服务,考虑添加容量限制或定期清理机制。例如:

const MAX_TRACES = 1000;
if (completedTraces.length >= MAX_TRACES) {
    completedTraces.shift(); // 移除最旧的记录
}
completedTraces.push(trace);
src/utils/performance.ts

问题: logger 配置项与默认值重复

建议: PerformanceConfiglogger 已有默认值,logTrace 中又用 || console.log 处理。建议统一处理方式,避免混淆:

const logger = globalConfig.logger; // 已在 configurePerformance 中确保非空

评审结果: 通过

代码质量良好,未发现明显问题。

优点

  1. 提供了预定义的关键路径追踪名称常量 TRACE_NAMES,便于统一管理
  2. 支持性能阈值告警机制,可配置 thresholdsonThresholdExceeded 回调
  3. 提供了完整的性能统计摘要,包括次数、总耗时、平均耗时、最大/最小值
  4. 使用点分格式命名追踪点,语义清晰
  5. 支持同步/异步函数测量

建议(可选):

  • 可以考虑添加 measureAsync 返回耗时信息,方便后续使用
  • PERFORMANCE_THRESHOLDS 可以移到 performance.ts 中统一管理
closed the pull request
Pull request has conflict
Reviewer
None yet
Assignee
None yet
Label
None yet
Participant