系统调优是商用级别系统都需要用到的一个技术,是属于看不到又很重要的功能,因为新手写出一个程序相对比较简单,但是如果系统要拿来商用,99%新手写出来的代码都有性能问题;而如何发现和解决性能问题就是一个高级工程师必备的技能了;
依赖
建议先阅读上一篇
原理
xdebug扩展可以分析php运行过程中的状态数据,然后把数据写入到指定文件,再通过专门的工具(kcachegrind,webgrind等)分析数据,从而找出程序中的瓶颈;本文结合kcachegrind工具进行操作说明
配置php. ini
创建数据文件存储目录
mkdir -p /tmp/xdebug [xDebug] zend_extension="/usr/lib/php/20151012/xdebug.so" xdebug.remote_enable = 1 xdebug.remote_autostart=on ; 以下部分为调优的配置 xdebug.profiler_enable = 1 xdebug.profiler_enable_trigger = 1 xdebug.profiler_output_dir = "/tmp/xdebug"
安装分析工具kcachegrind
sudo apt install kcachegrind
测试的源码文件
编辑inc.php
<?php function SayHello(){ echo "inc 中的sayhello|||"; } ?>
编辑index.php
<?php
include "./inc.php"; //引入模块
echo "hello world";
SayHello(); //调用模块的函数
?>
启动服务
php -S 0.0.0.0:3000
生成数据
浏览器中输入url
分析数据
运行kcachegrind;打开/tmp/xdebug中的文件
可以看到函数的执行时间,调用次数,占用内存的情况