PHP 7.1安装xhprof进行性能分析的介绍

不言
发布: 2023-04-02 12:10:02
原创
1670 人浏览过

这篇文章主要介绍了关于PHP 7.1安装xhprof进行性能分析的介绍,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

安装扩展
该 xhprof扩展版本是从 https://github.com/longxinH/xhprof 获取的(第三方的一个库,官方版本不支持php7)

下载并编译xhprof扩展
在web的html目录下操作:
git clone https://github.com/longxinH/xhprof

编译扩展

cd xhprof/extension/phpize
./configure 
makemake install
登录后复制

修改php.ini配置

[xhprof]
extension=xhprof.so;
xhprof.output_dir=/tmp/xhprof
登录后复制

其中 xhprof.output_dir 是 xhprof 的输出目录,每次执行 xhprof 的 save_run 方法时都会生成一个 run_id.project_name.xhprof 文件。这个目录在哪里并不重要。注意此路径的权限要可读写!!否则文件无法生成成功

重启 php-fpm
sudo service php7.1-fpm restart

添加测试代码

<?php
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);// 要检查性能的代码
$xhprof_data = xhprof_disable();
include_once &#39;/var/www/html/xhprof/xhprof_lib/utils/xhprof_lib.php&#39;;
include_once &#39;/var/www/html/xhprof/xhprof_lib/utils/xhprof_runs.php&#39;;
$xhprof_runs = new \XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, &#39;your_project&#39;);
登录后复制

测试代码中要引入xhprof_lib.php和xhprof_runs.php两个文件

查看生成报告
需要访问:xhprof/xhprof_html/index.php文件查看:
http://localhost/xhprof/xhprof_html/index.php?run=5b35d3dfa8c29&source=your_project
run后的参数为$run_id,source参数为your_project配置的名字

如果图表生成错误,需要安装插件:
sudo apt-get install graphviz

实际演示代码

<?php
function test1(){
for($i=0;$i<10;$i++){
echo &#39;aaa&#39;.$i.&#39;<br>&#39;;
}
}// start profilingxhprof_enable();

test1();
// stop profiler
$xhprof_data = xhprof_disable();
// display raw xhprof data for the profiler runprint_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_test"
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_test");echo "---------------\n".
"Assuming you have set up the http based UI for \n".
"XHProf at some address, you can view run at \n".
"http://<xhprof-ui-address>/index.php?run=$run_id&source=xhprof_test\n".
"---------------\n";
登录后复制

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

LAMP、LNMP和LNAMP的区别和安装

使用Wamp搭建Php本地开发环境以及HBuilder调试的方法

以上是PHP 7.1安装xhprof进行性能分析的介绍的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!