Skip to main content
Glama

perf

Analyze code performance bottlenecks and provide optimization suggestions for algorithms, memory usage, React applications, or database queries.

Instructions

【性能分析】分析代码性能瓶颈并提供优化建议

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeNo需要性能分析的代码
typeNo分析类型:algorithm, memory, react, database, all(默认 all)

Implementation Reference

  • The core handler function for the 'perf' tool. It takes args with code and type, constructs an extensive performance analysis prompt including guidelines, examples for algorithm, memory, React, database optimizations, benchmarks, and returns it as MCP content for LLM processing.
    export async function perf(args: any) {
      try {
        const code = args?.code || "";
        const type = args?.type || "all"; // algorithm, memory, react, database
    
        const message = `请分析以下代码的性能问题并提供优化建议:
    
    📝 **代码内容**:
    ${code || "请提供需要性能分析的代码"}
    
    🎯 **分析重点**:${type}
    
    ---
    
    ## 性能分析流程
    
    ### 第一步:识别性能瓶颈
    
    **常见性能问题**:
    1. **时间复杂度过高**
       - 嵌套循环 O(n²) 或更差
       - 未优化的算法
       - 重复计算
    
    2. **空间复杂度问题**
       - 内存泄漏
       - 大对象频繁创建
       - 缓存滥用
    
    3. **I/O 阻塞**
       - 同步 I/O 操作
       - 未使用连接池
       - 缺少缓存机制
    
    4. **渲染性能**
       - 不必要的重渲染
       - 大列表未虚拟化
       - 图片未优化
    
    ---
    
    ## 性能优化指南
    
    ### 1️⃣ 算法优化
    
    **复杂度分析**:
    \`\`\`
    O(1)    - 常数时间      ✅ 最优
    O(log n) - 对数时间      ✅ 很好
    O(n)     - 线性时间      ✅ 良好
    O(n log n) - 线性对数   ⚠️ 可接受
    O(n²)    - 平方时间      ⚠️ 注意
    O(2ⁿ)    - 指数时间      ❌ 避免
    O(n!)    - 阶乘时间      ❌ 严禁
    \`\`\`
    
    **优化技巧**:
    
    **示例 1:使用 Map 替代数组查找**
    \`\`\`typescript
    // Before - O(n²)
    function findDuplicates(arr) {
      const duplicates = [];
      for (let i = 0; i < arr.length; i++) {
        for (let j = i + 1; j < arr.length; j++) {
          if (arr[i] === arr[j]) {
            duplicates.push(arr[i]);
          }
        }
      }
      return duplicates;
    }
    
    // After - O(n)
    function findDuplicates(arr) {
      const seen = new Map();
      const duplicates = new Set();
      
      for (const item of arr) {
        if (seen.has(item)) {
          duplicates.add(item);
        } else {
          seen.set(item, true);
        }
      }
      
      return Array.from(duplicates);
    }
    \`\`\`
    
    **示例 2:缓存计算结果**
    \`\`\`typescript
    // Before - 重复计算
    function fibonacci(n) {
      if (n <= 1) return n;
      return fibonacci(n - 1) + fibonacci(n - 2);
    }
    
    // After - 使用 memoization
    const memo = new Map();
    function fibonacci(n) {
      if (n <= 1) return n;
      if (memo.has(n)) return memo.get(n);
      
      const result = fibonacci(n - 1) + fibonacci(n - 2);
      memo.set(n, result);
      return result;
    }
    \`\`\`
    
    ### 2️⃣ 内存优化
    
    **避免内存泄漏**:
    \`\`\`typescript
    // ❌ 内存泄漏
    class EventManager {
      listeners = [];
      
      addEventListener(listener) {
        this.listeners.push(listener);
        // 忘记移除监听器
      }
    }
    
    // ✅ 正确做法
    class EventManager {
      listeners = new Set();
      
      addEventListener(listener) {
        this.listeners.add(listener);
      }
      
      removeEventListener(listener) {
        this.listeners.delete(listener);
      }
      
      destroy() {
        this.listeners.clear();
      }
    }
    \`\`\`
    
    **对象池复用**:
    \`\`\`typescript
    // ❌ 频繁创建对象
    for (let i = 0; i < 10000; i++) {
      const obj = { x: i, y: i * 2 };
      process(obj);
    }
    
    // ✅ 对象池
    class ObjectPool {
      pool = [];
      
      acquire() {
        return this.pool.pop() || {};
      }
      
      release(obj) {
        this.pool.push(obj);
      }
    }
    
    const pool = new ObjectPool();
    for (let i = 0; i < 10000; i++) {
      const obj = pool.acquire();
      obj.x = i;
      obj.y = i * 2;
      process(obj);
      pool.release(obj);
    }
    \`\`\`
    
    ### 3️⃣ React 性能优化
    
    **useMemo 和 useCallback**:
    \`\`\`typescript
    // ❌ 每次渲染都创建新函数
    function MyComponent({ data }) {
      const processedData = expensiveComputation(data);
      const handleClick = () => { /* ... */ };
      
      return <ChildComponent data={processedData} onClick={handleClick} />;
    }
    
    // ✅ 使用 hooks 优化
    function MyComponent({ data }) {
      const processedData = useMemo(
        () => expensiveComputation(data),
        [data]
      );
      
      const handleClick = useCallback(() => {
        /* ... */
      }, []);
      
      return <ChildComponent data={processedData} onClick={handleClick} />;
    }
    \`\`\`
    
    **React.memo 避免重渲染**:
    \`\`\`typescript
    // ✅ 使用 memo
    const ChildComponent = React.memo(({ data, onClick }) => {
      return <div onClick={onClick}>{data}</div>;
    });
    \`\`\`
    
    **虚拟列表**:
    \`\`\`typescript
    // ❌ 渲染 10000 个元素
    function List({ items }) {
      return (
        <div>
          {items.map(item => <Item key={item.id} data={item} />)}
        </div>
      );
    }
    
    // ✅ 使用虚拟列表
    import { FixedSizeList } from 'react-window';
    
    function List({ items }) {
      return (
        <FixedSizeList
          height={600}
          itemCount={items.length}
          itemSize={50}
        >
          {({ index, style }) => (
            <div style={style}>
              <Item data={items[index]} />
            </div>
          )}
        </FixedSizeList>
      );
    }
    \`\`\`
    
    ### 4️⃣ 数据库优化
    
    **添加索引**:
    \`\`\`sql
    -- Before
    SELECT * FROM users WHERE email = 'user@example.com';
    
    -- After
    CREATE INDEX idx_users_email ON users(email);
    SELECT * FROM users WHERE email = 'user@example.com';
    \`\`\`
    
    **查询优化**:
    \`\`\`typescript
    // ❌ N+1 查询
    const users = await User.findAll();
    for (const user of users) {
      user.posts = await Post.findAll({ where: { userId: user.id } });
    }
    
    // ✅ 使用 JOIN
    const users = await User.findAll({
      include: [{ model: Post }]
    });
    \`\`\`
    
    **批量操作**:
    \`\`\`typescript
    // ❌ 逐条插入
    for (const item of items) {
      await db.insert(item);
    }
    
    // ✅ 批量插入
    await db.insertMany(items);
    \`\`\`
    
    ---
    
    ## 性能测试
    
    ### Benchmark 测试
    \`\`\`typescript
    console.time('operation');
    // 执行操作
    console.timeEnd('operation');
    
    // 或使用 performance API
    const start = performance.now();
    // 执行操作
    const end = performance.now();
    console.log(\`耗时: \${end - start}ms\`);
    \`\`\`
    
    ### 性能指标
    
    **目标值**:
    - 首屏加载:< 1s
    - 交互响应:< 100ms
    - 动画帧率:60 FPS
    - API 响应:< 200ms
    
    ---
    
    ## 性能优化报告模板
    
    ### 📊 性能分析
    
    **当前性能**:
    - 时间复杂度:O(n²)
    - 内存占用:150MB
    - 响应时间:800ms
    
    **瓶颈识别**:
    1. 嵌套循环导致复杂度过高
    2. 未缓存计算结果
    3. 数组查找效率低
    
    ### 🎯 优化方案
    
    **方案 1:算法优化**
    - 使用 Map 替代数组
    - 预期提升:70%
    
    **方案 2:添加缓存**
    - 使用 memoization
    - 预期提升:50%
    
    ### 📈 优化效果
    
    **优化后性能**:
    - 时间复杂度:O(n)
    - 内存占用:50MB ↓ 67%
    - 响应时间:200ms ↓ 75%
    
    ---
    
    现在请分析代码,提供详细的性能优化建议。`;
    
        return {
          content: [
            {
              type: "text",
              text: message,
            },
          ],
        };
      } catch (error) {
        const errorMessage =
          error instanceof Error ? error.message : String(error);
        return {
          content: [
            {
              type: "text",
              text: `❌ 性能分析失败: ${errorMessage}`,
            },
          ],
          isError: true,
        };
      }
    }
  • The input schema definition for the 'perf' tool, specifying parameters 'code' (string) and 'type' (string with options).
    inputSchema: {
      type: "object",
      properties: {
        code: {
          type: "string",
          description: "需要性能分析的代码",
        },
        type: {
          type: "string",
          description: "分析类型:algorithm, memory, react, database, all(默认 all)",
        },
      },
      required: [],
    },
  • src/index.ts:265-282 (registration)
    Registration of the 'perf' tool in the ListTools handler, including name, description, and input schema.
    {
      name: "perf",
      description: "【性能分析】分析代码性能瓶颈并提供优化建议",
      inputSchema: {
        type: "object",
        properties: {
          code: {
            type: "string",
            description: "需要性能分析的代码",
          },
          type: {
            type: "string",
            description: "分析类型:algorithm, memory, react, database, all(默认 all)",
          },
        },
        required: [],
      },
    },
  • src/index.ts:498-500 (registration)
    Dispatch registration in the CallTool handler switch statement, calling the perf function.
    case "perf":
      return await perf(args);
  • Export of the perf function from its module for use in the main index.
    export { perf } from "./perf.js";
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It mentions analysis and optimization suggestions but doesn't describe what the tool actually does behaviorally—e.g., whether it runs the code, simulates execution, returns detailed reports, or has side effects like modifying code. For a tool with no annotation coverage, this leaves significant gaps in understanding its operation.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is very concise and front-loaded, consisting of a single sentence in Chinese that directly states the tool's purpose. There is no wasted text, and it efficiently communicates the core functionality without unnecessary elaboration.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of performance analysis (which could involve various methods and outputs), the lack of annotations, and no output schema, the description is incomplete. It doesn't explain what the analysis entails, what format the optimization suggestions take, or any limitations (e.g., supported languages, runtime requirements). This makes it inadequate for a tool that likely produces detailed results.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The schema description coverage is 100%, with clear descriptions for both parameters ('code' as the code to analyze and 'type' as the analysis type with options). The description adds no additional parameter semantics beyond what's in the schema, such as explaining the 'type' options further or providing examples. Since the schema does the heavy lifting, the baseline score of 3 is appropriate.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: '分析代码性能瓶颈并提供优化建议' (analyze code performance bottlenecks and provide optimization suggestions). It specifies the verb '分析' (analyze) and resource '代码性能瓶颈' (code performance bottlenecks), with the added outcome of providing optimization suggestions. However, it doesn't explicitly differentiate from sibling tools like 'analyze_project' or 'debug', which might also involve analysis.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention sibling tools like 'analyze_project' (which might analyze broader project aspects) or 'debug' (which might focus on errors rather than performance). There's no context about prerequisites, such as needing code input or when performance analysis is appropriate.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mybolide/mcp-probe-kit'

If you have feedback or need assistance with the MCP directory API, please join our Discord server