-
Notifications
You must be signed in to change notification settings - Fork 282
Open
Description
问题描述
- 自己部署到 Cloudflare,发现 PromptX MCP 工具性能问题
- 在 deepractice.ai 网站集成 PromptX MCP 时,发现
action工具调用非常慢,严重影响用户体验 action工具慢(15秒):资源串行加载,建议改Promise.all
测试数据
单次调用耗时
Initialize: 475 ms
Action (jiangziya): 14906 ms // 将近 15 秒!
连续调用耗时
Action #1: 2011 ms
Action #2: 4093 ms
Action #3: 4995 ms
连续调用反而变慢,说明不是冷启动问题。
对比:discover 工具
discover: 671 ms // 不到 1 秒
问题根因分析
通过阅读 packages/core/src/pouch/commands/ActionCommand.js 源码,发现以下性能瓶颈:
1. 资源加载串行执行(主要瓶颈)
// ActionCommand.js 第 196-210 行
for (const ref of allRefs) {
try {
const resourceUrl = `@${ref.protocol}://${ref.resource}`
const result = await this.resourceManager.loadResource(resourceUrl) // 串行!
// ...
}
}一个角色可能有多个 thought、execution、knowledge 引用,每个都是串行加载,累积延迟。
2. ResourceManager 每次初始化
// ActionCommand.js 第 48-50 行
if (!this.resourceManager.initialized) {
await this.resourceManager.initializeWithNewArchitecture()
}3. 缺少资源缓存
已加载的资源没有缓存,重复激活同一角色仍需重新加载所有资源。
建议优化方案
方案 1:并行加载资源(推荐,改动小)
// 将串行改为并行
const loadPromises = allRefs.map(async (ref) => {
const resourceUrl = `@${ref.protocol}://${ref.resource}`
const result = await this.resourceManager.loadResource(resourceUrl)
return { ref, result }
})
const results = await Promise.all(loadPromises)
for (const { ref, result } of results) {
if (result && result.success) {
// 分类处理...
}
}预期提升:如果有 5 个资源引用,每个 1 秒,串行需要 5 秒,并行只需 1 秒。
方案 2:添加资源缓存
// ResourceManager 添加缓存
const resourceCache = new Map()
async loadResource(url) {
if (resourceCache.has(url)) {
return resourceCache.get(url)
}
const result = await this._loadResourceInternal(url)
resourceCache.set(url, result)
return result
}方案 3:预加载常用角色
服务启动时预热常用角色的资源:
const PRELOAD_ROLES = ['luban', 'nuwa', 'sean', 'writer', 'jiangziya']
async function preloadRoles() {
await Promise.all(PRELOAD_ROLES.map(role =>
resourceManager.loadResource(`@role://${role}`)
))
}影响范围
- deepractice.ai 官网 Chat 功能
- 所有使用 PromptX Cloud MCP 的应用
- 用户体验:激活角色需等待 10-15 秒,经常超时失败
复现步骤
- 调用 PromptX Cloud MCP 的
action工具 - 传入参数
{ role: 'jiangziya' } - 观察响应时间
环境信息
- PromptX 版本: 1.27.7
- 部署环境: Cloudflare Workers
相关代码
packages/core/src/pouch/commands/ActionCommand.jspackages/mcp-server/src/tools/action.ts
Metadata
Metadata
Assignees
Labels
No labels