美文网首页程序猿的进阶屋
使用xhprof对php7程序进行性能分析

使用xhprof对php7程序进行性能分析

作者: dreamer_lk | 来源:发表于2020-10-19 11:35 被阅读0次

Xhprof是facebook开源出来的一个php轻量级的性能分析工具,跟Xdebug类似,但性能开销更低,还可以用在生产环境中,也可以由程序开关来控制是否进行profile。

对于还在使用php5的朋友们,可以安装pecl的xhprof扩展

http:``//pecl.php.net/package/xhprof

但是因为长时间不更新,针对php7已无法正常安装,可以使用下的地址

https:``//github.com/longxinH/xhprof/releases

一、安装xhprof

下载xhprof源码

wget https:``//github.com/longxinH/xhprof/archive/v2.1.0.tar.gz

解压源码包

tar xf v2.1.0.tar.gz

进入目录

cd xhprof-2.1.0/extension

运行phpize,请自行修改你们的phpize路径

/data/nmp/php7/bin/phpize

运行configure,请自行修改你们的php-config路径

./configure --with-php-config=/data/nmp/php7/bin/php-config

编译安装
`make && make install

修改php.ini配置,如果extension_dir配置了就不用再配置了,如果没配置,则把该目录指向,扩展编译安装后显示的路径。

extension_dir = ``"/data/nmp/php7/lib/php/extensions/no-debug-zts-20170718"

[xhprof]

extension = xhprof.so

xhprof.output_dir = /tmp/xhprof

二、配置xhprof

我们把扩展目录下的xhprof_html和xhprof_lib复制出来,单独存放到一个目录中。

cp -a xhprof_html /data/wwwroot/xhprof

cp -a xhprof_lib /data/wwwroot/xhprof

针对要分析的项目,可以把如下代码添加到入口文件中:

`//开启性能分析`

`xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);`

`//php中止时运行的函数`

`register_shutdown_function(``function` `() {`

`//停止性能分析`

`$xhprof_data` `= xhprof_disable();`

`if` `(function_exists(``'fastcgi_finish_request'``)) {`

`fastcgi_finish_request();`

`}`

`//引入xhprof库文件,路径请自行修改`

`$XHPROF_ROOT` `= ``realpath``(dirname(``__FILE__``) . ``'/xhprof'``);`

`include_once` `$XHPROF_ROOT` `. ``"/xhprof_lib/utils/xhprof_lib.php"``;`

`include_once` `$XHPROF_ROOT` `. ``"/xhprof_lib/utils/xhprof_runs.php"``;`

`$xhprof_runs` `= ``new` `XHProfRuns_Default();`

`//导出性能分析数据,默认xhprof.output_dir指定的目录`

`$run_id` `= ``$xhprof_runs``->save_run(``$xhprof_data``, ``'xhprof'``);`

`});`

数据默认会导出到xhprof.output_dir指定目录,所以需创建该目录,并给予相关权限

mkdir /tmp/xhprof

chmod -R 777 /tmp/xhprof

这里我们写一个简单程序进行一下演示

`<?php`

`error_reporting``(E_ALL);`

`ini_set``(``'display_errors'``, ``'on'``);`

`//开启性能分析`

`xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);`

`//php中止时运行的函数`

`register_shutdown_function(``function` `() {`

`//停止性能分析`

`$xhprof_data` `= xhprof_disable();`

`if` `(function_exists(``'fastcgi_finish_request'``)) {`

`fastcgi_finish_request();`

`}`

`//引入xhprof库文件,路径请自行修改`

`$XHPROF_ROOT` `= ``realpath``(dirname(``__FILE__``) .``'/xhprof'``);`

`include_once` `$XHPROF_ROOT` `. ``"/xhprof_lib/utils/xhprof_lib.php"``;`

`include_once` `$XHPROF_ROOT` `. ``"/xhprof_lib/utils/xhprof_runs.php"``;`

`$xhprof_runs` `= ``new` `XHProfRuns_Default();`

`//导出性能分析数据,默认xhprof.output_dir指定的目录`

`$run_id` `= ``$xhprof_runs``->save_run(``$xhprof_data``, ``'xhprof'``);`

`});`

`function` `a()`

`{`

`echo` `'aaa'``;`

`sleep(1);`

`b();`

`}`

`function` `b()`

`{`

`echo` `'bbb'``;`

`sleep(3);`

`c();`

`}`

`function` `c()`

`{`

`echo` `'ccc'``;`

`sleep(5);`

`}`

`a();`

三、配置虚拟主机,用来查看分析日志

`server {`

`listen       80;`

`server_name  xhprof.xxx.com;`

`charset utf-8;`

`root   /data/wwwroot/xhprof/xhprof_html;`

`index  index.html index.htm index.php;`

`location ~ \.php$ {`

`fastcgi_pass   127.0.0.1:9000;`

`fastcgi_index  index.php;`

`fastcgi_param  SCRIPT_FILENAME  ``$document_root``$fastcgi_script_name``;`

`include`        `fastcgi_params;`

`}`

`}`

我们可以通过访问xhprof.xxx.com来查看分析日志了。

image

如果点击 [View Full Callgraph] 无法查看,则需装如下工具:
yum install -y libpng

yum install -y graphviz

如果上面安装完后,还是无法显示,并报如下错误:

failed to execute cmd: ``" dot -Tpng"``. stderr:Format: ``"png"not recognized.`

Use one of: canon cmap cmapx cmapx_np dot eps fig gv imap imap_np ismap pic plain plain-ext pov ps ps2 svg svgz tk vml vmlz xdot '

我们需要手动安装 graphviz

https:``//graphviz.gitlab.io/_pages/Download/Download_source.html

下载源码包,并编译安装

`tar xf graphviz.tar.gz`

`cd graphviz-2.40.1`

`./configure`

`make`

`make install`

相关文章

网友评论

    本文标题:使用xhprof对php7程序进行性能分析

    本文链接:https://www.haomeiwen.com/subject/qivhnhtx.html