# 接口性能分析实践

### **背景**

风险：项目出了几次问题，减少风险

代码：之前代码没考虑未来需要，性能急剧下降

监控平台：Nginx日志发现，请求很慢

我们的目标：要追求高性能

### **问题**

你们是如何做性能分析的？

### **怎么做的**

|      | xhprof                                       | xdebug          | tideway + xhgui                                           |
| ---- | -------------------------------------------- | --------------- | --------------------------------------------------------- |
| 搭建   | 安装xhprof扩展，即开即用                              | 安装xdebug即可，即开即用 | 复杂(依赖mongodb)                                             |
| 采集   | 1. 只能看单次调试，一次经常说明不了问题2. 常常和用户有关系，并不是所有函数都会触发 | ———             | 1. 可以抽象采集，可以完整采集2. 信息记录完整，比如请求信息                          |
| 信息展示 | 看不了SQL执行                                     | ———             | 1. 支持SQL展示2. 支持请求信息展示3. 可视化信息丰富支持火焰图、瀑布图（单次请求）4. 支持请求性能对比 |

#### 备注

一、新版才支持SQL

1. tideways\_xhprof.so => tideways.so
2. 调整header.php 的文件 把相关代码都调整成扩展为tideways的流程即可！

二、如何搭建非侵入式分析平台

<https://blog.it2048.cn/article-tideways-xhgui/>

三、生成classmap + apcu

<https://blog.csdn.net/zhouyuqi1/article/details/81098650>

### **结果**

1. 流量减少了一半
2. TOP10，部分接口性能提升1倍，剩余还在优化中

### **方案总结**

1. 多级缓存：apcu
2. 静态变量（主要是针对Web）

一次接口只会调用一次用户信息

1. 减少循环调用

多次hget调整为hmget

1. 不要过度优化
2. 性能优化，避免自嗨，需要抓重点，用数据说明


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bing.gitbook.io/phper/php/5.-xing-neng/xing-neng-fen-xi/jie-kou-xing-neng-fen-xi-shi-jian.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
