Xhprof安装笔记(PHP性能监控)

由facebook开源出来的一个PHP性能监控工具,占用资源很少,甚至能够在生产环境中进行部署。
它可以结合graphviz使用,能够以图片的形式很直观的展示代码执行耗时
wget http://pecl.php.net/get/xhprof-0.9.4.tgz
tar zxvf xhprof-0.9.4.tgz
cd xhprof-0.9.4/extension/
/usr/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
mkdir /tmp/xhprof
chown -R www:www /usr/local/src/xhprof-0.9.4
# 编辑php.ini: [xhprof] extension = xhprof.so xhprof.output_dir=/tmp/xhprof 重启服务 service php-fpm restart # 最后返回数组,就表示安装好了。具体哪些值是什么意思先别管,因为下面有UI的配置。会很直观! yum -y install libjpeg freetype freetype-devel libjpeg-devel liberation-sans-fonts.noarch 自动安装 yum -y install graphviz 为Xhprof配置一个访问站点(虚拟主机) 比如做一个虚拟域名 dev.xhprof.com 绑定到xhprof的站点根目录 /usr/local/xhprof-0.9.4/xhprof_html # 找到你要分析的代码,在代码开始处添加,start profiling,将会统计内存占用情况 xhprof_enable(XHPROF_FLAGS_MEMORY); # 在代码结束位置添加 $xhprof_data = xhprof_disable(); // stop profiler, display raw xhprof data for the profiler run include_once ("/usr/local/src/xhprof-0.9.4/xhprof_lib/utils/xhprof_lib.php"); # 请注意设置站点 include_path 权限 include_once ("/usr/local/src/xhprof-0.9.4/xhprof_lib/utils/xhprof_runs.php"); $xhprof_runs = new \XHProfRuns_Default(); // Save the run under a namespace "xhprof_foo". // **NOTE**: // By default save_run() will automatically generate a unique // run id for you. [You can override that behavior by passing // a run id (optional arg) to the save_run() method instead.] $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo"); $str = "\r\n------------------\r\n". "Assuming you have set up the http based UI for \r\n". "XHProf at some address, you can view run at \r\n". "http://dev.xhprof.com/index.php?run=$run_id&source=xhprof_foo". "\r\n------------------\r\n"; echo nl2br($str); 然后进入程序输出的网址(比如 http://dev.xhprof.com/index.php?run=52c0ea0bef834&source=xhprof_foo ),进行查看 下面是一些参数说明 Inclusive Time 包括子函数所有执行时间。 Exclusive Time/Self Time 函数执行本身花费的时间,不包括子树执行时间。 Wall Time 花去了的时间或挂钟时间。 CPU Time 用户耗的时间+内核耗的时间 Inclusive CPU 包括子函数一起所占用的CPU Exclusive CPU 函数自身所占用的CPU 点击 [View Full Callgraph] 能够以图文的形式查看,很方便 注意: 需要使用ctype这个扩展,Callgraph图片生成依赖一些PHP系统级的函数,所以,最好去掉 php.ini 中的函数禁用
posted @ 2016-08-04 20:33  Funsion Wu  Views(506)  Comments(0Edit  收藏  举报