PHP7中如何配置与运用xhprof进行代码性能剖析?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1313个文字,预计阅读时间需要6分钟。
本篇文章将简要介绍在PHP7环境下安装并使用xhprof性能分析工具的方法。xhprof是一个强大的性能分析工具,可以帮助开发者定位PHP应用的性能瓶颈。
安装xhprof
1. 首先,从GitHub获取xhprof的源代码:[https://github.com/longxinH/x](https://github.com/longxinH/x)
2.解压下载的源代码包。
3.在命令行中,进入源代码目录,执行以下命令安装依赖:
bash
sudo apt-get install php7.x-dev4. 编译并安装xhprof:
bash ./configure make sudo make install
配置xhprof
1. 在php.ini文件中启用xhprof:
ini extension=xhprof.so
2. 设置xhprof的存储路径:
ini xhprof.output_dir=/path/to/xhprof/profiles
3. 重启Apache服务器使配置生效。
使用xhprof
1. 在PHP代码中,添加以下代码来开始和结束性能分析:
php xhprof_start(my_trace); // ... 你的PHP代码 ... xhprof_stop(my_trace);
2. 访问存储路径下的文件,查看分析结果:
bash xhprof_view_profiler.php?file=/path/to/xhprof/profiles/my_trace.xhprof
参考价值
- xhprof可以帮助你快速定位性能瓶颈,优化代码。- 对于有性能优化需求的PHP开发者,xhprof是一个非常有价值的工具。
需要的朋友
- 如果你有关于xhprof的问题或需要帮助,欢迎随时提问。希望这篇文章对你有所帮助。
本篇文章给大家介绍一下PHP7下安装并使用xhprof性能分析工具的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。该 xhprof 版本是从 github.com/longxinH/xhprof 获取
git clone github.com/longxinH/xhprof
安装 xhprof
cd xhprof/extension/ phpize ./configure make make install
然后在/etc/php.ini中根据情况加入
extension=xhprof.so
执行
php -m | grep xhprof
可以看见输出,说明php扩展安装成功,然后重启Apache或者php-fpm
运行
可以直接运行从github上clone下来的文件里面example目录下的那个例子
输出如下
Array ( [main()] => Array ( [ct] => 1 [wt] => 9 ) ) --------------- Assuming you have set up the <xhprof-ui-address>/index.php?run=592567308784c&source=xhprof_foo ---------------
然后复制index.php后面的?run=592567308784c&source=xhprof_foo
访问
xhprof_html/index.php?run=592567308784c&source=xhprof_foo
可看见输出
点击中间的 View Full Callgraph 即可看见性能分析图片
报错
failed to execute cmd:" dot -Tpng". stderr:sh: dot:command not found。
//解决方案 yum install graphviz
随机应变
比如想测试自己的项目,例如一款框架的性能分析。
复制xhprof_lib/utils/下的两个文件
xhprof_lib.php和xhprof_runs.php到入口文件同级目录,然后在入口文件起始位置添加
// start profiling xhprof_enable();
结束位置添加
// stop profiler $xhprof_data = xhprof_disable(); // display raw xhprof data for the profiler run print_r($xhprof_data); include_once "xhprof_lib.php"; include_once "xhprof_runs.php"; // save raw data for this profiler run using default // implementation of iXHProfRuns. $xhprof_runs = new XHProfRuns_Default(); // save the run under a namespace "xhprof_foo" $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo"); echo "---------------\n". "Assuming you have set up the <xhprof-ui-address>/index.php?run=$run_id&source=xhprof_foo\n". "---------------\n";
即可得到如上所示的那个url,然后再次去访问
***/xhprof_html/index.php?run=*****&source=xhprof_foo
得到如下所示页面
查看图片
图中红色的部分为性能比较低,耗时比较长的部分,我们可以根据根据哪些函数被标记为红色对系统的代码进行优化
补充
Function Name:方法名称。
Calls:方法被调用的次数。
Calls%:方法调用次数在同级方法总数调用次数中所占的百分比。
Incl.Wall Time(microsec):方法执行花费的时间,包括子方法的执行时间。(单位:微秒)
IWall%:方法执行花费的时间百分比。
Excl. Wall Time(microsec):方法本身执行花费的时间,不包括子方法的执行时间。(单位:微秒)
EWall%:方法本身执行花费的时间百分比。
Incl. CPU(microsecs):方法执行花费的CPU时间,包括子方法的执行时间。(单位:微秒)
ICpu%:方法执行花费的CPU时间百分比。
Excl. CPU(microsec):方法本身执行花费的CPU时间,不包括子方法的执行时间。(单位:微秒)
ECPU%:方法本身执行花费的CPU时间百分比。
Incl.MemUse(bytes):方法执行占用的内存,包括子方法执行占用的内存。(单位:字节)
IMemUse%:方法执行占用的内存百分比。
Excl.MemUse(bytes):方法本身执行占用的内存,不包括子方法执行占用的内存。(单位:字节)
EMemUse%:方法本身执行占用的内存百分比。
Incl.PeakMemUse(bytes):Incl.MemUse峰值。(单位:字节)
IPeakMemUse%:Incl.MemUse峰值百分比。
Excl.PeakMemUse(bytes):Excl.MemUse峰值。单位:(字节)
EPeakMemUse%:Excl.MemUse峰值百分比。
本文共计1313个文字,预计阅读时间需要6分钟。
本篇文章将简要介绍在PHP7环境下安装并使用xhprof性能分析工具的方法。xhprof是一个强大的性能分析工具,可以帮助开发者定位PHP应用的性能瓶颈。
安装xhprof
1. 首先,从GitHub获取xhprof的源代码:[https://github.com/longxinH/x](https://github.com/longxinH/x)
2.解压下载的源代码包。
3.在命令行中,进入源代码目录,执行以下命令安装依赖:
bash
sudo apt-get install php7.x-dev4. 编译并安装xhprof:
bash ./configure make sudo make install
配置xhprof
1. 在php.ini文件中启用xhprof:
ini extension=xhprof.so
2. 设置xhprof的存储路径:
ini xhprof.output_dir=/path/to/xhprof/profiles
3. 重启Apache服务器使配置生效。
使用xhprof
1. 在PHP代码中,添加以下代码来开始和结束性能分析:
php xhprof_start(my_trace); // ... 你的PHP代码 ... xhprof_stop(my_trace);
2. 访问存储路径下的文件,查看分析结果:
bash xhprof_view_profiler.php?file=/path/to/xhprof/profiles/my_trace.xhprof
参考价值
- xhprof可以帮助你快速定位性能瓶颈,优化代码。- 对于有性能优化需求的PHP开发者,xhprof是一个非常有价值的工具。
需要的朋友
- 如果你有关于xhprof的问题或需要帮助,欢迎随时提问。希望这篇文章对你有所帮助。
本篇文章给大家介绍一下PHP7下安装并使用xhprof性能分析工具的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。该 xhprof 版本是从 github.com/longxinH/xhprof 获取
git clone github.com/longxinH/xhprof
安装 xhprof
cd xhprof/extension/ phpize ./configure make make install
然后在/etc/php.ini中根据情况加入
extension=xhprof.so
执行
php -m | grep xhprof
可以看见输出,说明php扩展安装成功,然后重启Apache或者php-fpm
运行
可以直接运行从github上clone下来的文件里面example目录下的那个例子
输出如下
Array ( [main()] => Array ( [ct] => 1 [wt] => 9 ) ) --------------- Assuming you have set up the <xhprof-ui-address>/index.php?run=592567308784c&source=xhprof_foo ---------------
然后复制index.php后面的?run=592567308784c&source=xhprof_foo
访问
xhprof_html/index.php?run=592567308784c&source=xhprof_foo
可看见输出
点击中间的 View Full Callgraph 即可看见性能分析图片
报错
failed to execute cmd:" dot -Tpng". stderr:sh: dot:command not found。
//解决方案 yum install graphviz
随机应变
比如想测试自己的项目,例如一款框架的性能分析。
复制xhprof_lib/utils/下的两个文件
xhprof_lib.php和xhprof_runs.php到入口文件同级目录,然后在入口文件起始位置添加
// start profiling xhprof_enable();
结束位置添加
// stop profiler $xhprof_data = xhprof_disable(); // display raw xhprof data for the profiler run print_r($xhprof_data); include_once "xhprof_lib.php"; include_once "xhprof_runs.php"; // save raw data for this profiler run using default // implementation of iXHProfRuns. $xhprof_runs = new XHProfRuns_Default(); // save the run under a namespace "xhprof_foo" $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo"); echo "---------------\n". "Assuming you have set up the <xhprof-ui-address>/index.php?run=$run_id&source=xhprof_foo\n". "---------------\n";
即可得到如上所示的那个url,然后再次去访问
***/xhprof_html/index.php?run=*****&source=xhprof_foo
得到如下所示页面
查看图片
图中红色的部分为性能比较低,耗时比较长的部分,我们可以根据根据哪些函数被标记为红色对系统的代码进行优化
补充
Function Name:方法名称。
Calls:方法被调用的次数。
Calls%:方法调用次数在同级方法总数调用次数中所占的百分比。
Incl.Wall Time(microsec):方法执行花费的时间,包括子方法的执行时间。(单位:微秒)
IWall%:方法执行花费的时间百分比。
Excl. Wall Time(microsec):方法本身执行花费的时间,不包括子方法的执行时间。(单位:微秒)
EWall%:方法本身执行花费的时间百分比。
Incl. CPU(microsecs):方法执行花费的CPU时间,包括子方法的执行时间。(单位:微秒)
ICpu%:方法执行花费的CPU时间百分比。
Excl. CPU(microsec):方法本身执行花费的CPU时间,不包括子方法的执行时间。(单位:微秒)
ECPU%:方法本身执行花费的CPU时间百分比。
Incl.MemUse(bytes):方法执行占用的内存,包括子方法执行占用的内存。(单位:字节)
IMemUse%:方法执行占用的内存百分比。
Excl.MemUse(bytes):方法本身执行占用的内存,不包括子方法执行占用的内存。(单位:字节)
EMemUse%:方法本身执行占用的内存百分比。
Incl.PeakMemUse(bytes):Incl.MemUse峰值。(单位:字节)
IPeakMemUse%:Incl.MemUse峰值百分比。
Excl.PeakMemUse(bytes):Excl.MemUse峰值。单位:(字节)
EPeakMemUse%:Excl.MemUse峰值百分比。

