PhalApi-Xhprof -- Facebook开源的轻量级PHP性能分析工具

时间:2022-04-29
本文章向大家介绍PhalApi-Xhprof -- Facebook开源的轻量级PHP性能分析工具,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

#PhalApi-Xhprof -- Facebook开源的轻量级PHP性能分析工具

##前言

Facebook开源的轻量级PHP性能分析工具,非常爽希望大家喜欢

附上:

官网地址:http://www.phalapi.net/

开源中国Git地址:http://git.oschina.net/dogstar/PhalApi/tree/release

开源中国拓展Git地址:http://git.oschina.net/dogstar/PhalApi-Library

##1. 安装

首先需要安装配置Xhprof

wget http://pecl.php.net/get/xhprof-0.9.2.tgz

tar zxf xhprof-0.9.2.tgz

cd xhprof-0.9.2/extension/

sudo phpize
./configure --with-php-config=/usr/local/php/bin/php-config
sudo make
sudo make install

需要在php.ini中配置好

[xhprof] extension=xhprof.so; ; directory used by default implementation of the iXHProfRuns ; interface (namely, the XHProfRuns_Default class) for storing ; XHProf runs. ; ;xhprof.output_dir=<directory_for_storing_xhprof_runs> xhprof.output_dir=/tmp/xhprof

通过phpinfo()看到xhprof扩展则为安装成功

注意:xhprof.output_dir=/tmp/xhprof,设置必须统一不然需要自行替换编译出来html的问题到拓展项目中

然后对我们的index.php文件做如此的改造

在头部加上:

if (!empty($_GET['__debug__'])) {
    xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
}

在尾部加上: if (!empty($_GET['debug'])) {

    $data = xhprof_disable();

    include API_ROOT . "/Library/Xhprof/utils/xhprof_lib.php";
    include API_ROOT . "/Library/Xhprof/utils/xhprof_runs.php";
    $objXhprofRun = new XHProfRuns_Default();//数据会保存在php.ini中xhprof.output_dir设置的目录去中
    echo $objXhprofRun->save_run($data, "developers");
}

这个时候我们访问的时候带入请求参数__debug__可以获得如下返回

然后我们访问http://xxxx/Library/Xhprof/index.php可以的到如下界面

我们可以看到有一个key和上面生成的一样的我们点击进去:

##2. 总结

希望此拓展能够给大家带来方便以及实用,此扩展可以分析出在整个运行途中的消耗用时可以针对进行优化,在压力情况下可以非常好的辨别出慢代码出现在哪里!

注:笔者能力有限有说的不对的地方希望大家能够指出,也希望多多交流!

官网QQ交流群:421032344 欢迎大家的加入!