xhprof 安装使用(windows linux混版)

来源:互联网 发布:爱情不是毒药网络歌手 编辑:程序博客网 时间:2024/05/16 06:35

1、安装扩展 

  windows下把 xhprof.dll 放到extensions目录下

修改配置文件

复制代码
[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
复制代码

 

linux下安装

复制代码
wget http://pecl.php.net/get/xhprof-0.9.2.tgztar zxf xhprof-0.9.2.tgzcd xhprof-0.9.2/extension/sudo phpize./configure --with-php-config=/usr/local/php/bin/php-configsudo makesudo make install
复制代码

把生成的 xhprof.so 放到扩展的目录下,并配置记录存放的路径

 

php中增加调试代码 sample.php 文件

复制代码
function bar($x) {  if ($x > 0) {    bar($x - 1);  }}function foo() {  for ($idx = 0; $idx < 5; $idx++) {    bar($idx);    $x = strlen("abc");  }}//开启调试xhprof_enable();

// cpu:XHPROF_FLAGS_CPU 内存:XHPROF_FLAGS_MEMORY
// 如果两个一起:XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY 
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

//要测试的php代码foo();//停止监测$xhprof_data = xhprof_disable();// display raw xhprof data for the profiler runprint_r($xhprof_data);//包含工具类,在下载的 tgz 包中可以找到$XHPROF_ROOT = realpath(dirname(__FILE__) .'/..');include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";// save raw data for this profiler run using default// implementation of iXHProfRuns. $xhprof_runs = new XHProfRuns_Default();// xhprof_foo 指命名空间,可以为任意字符串$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");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_foo\n".     "---------------\n";
复制代码

 

以表格方式查看

  访问地址:http://test.cm/xhprof/xhprof_html/index.php?run=539d612de570e&source=xhprof_foo 

run后的参数指生成的文件名, 目录再php.ini中的 xhprof.output_dir 指定

以图表方式查看

  1、安装 Graphviz  软件(windows,linux版都有)

  2、修改配置文件 config.php

 

  3、 然后点击 view full callgraph 链接即可

 

红色节点是整个php程序执行过程中的瓶颈,黄色路径为整个过程耗时最长的路径 

 

 输出结果的含义:

ct 函数调用次数,wt 花费的时间,cpu 花费的 CPU 时间(微秒即百万分之一秒),mu 使用的内存(bytes),pmu 使用的内存峰值(bytes)。

 

web 分析结果页面含义

复制代码
Calls:函数的调用次数Incl. Wall Time (microsec) :包含内部函数花费的时间,单位微秒Excl. Wall Time (microsec):不包含内部函数花费的时间,单位微秒及所占百分比(%)注:Incl.:为 Including 包含的简写Excl.:为 Excluding 不包含的简写Wall Time:意为挂钟时间即任务花费的时间
复制代码
main():一个虚构的函数,程序根节点bar@2:递归调用 2 次
复制代码
Incl. CPU (microsecs):包含内部函数 CPU 花费的时间,单位微秒Excl. CPU (microsec):不包含内部函数 CPU 花费的时间,单位微秒Incl. MemUse (bytes):包含内部函数所占内存,单位字节Excl. MemUse (bytes):不包含内部函数所占内存,单位字节Incl. PeakMemUse (bytes):包含内部函数所占内存峰值,单位字节Excl. PeakMemUse (bytes):不包含内部函数所占内存峰值,单位字节及所占百分比(%)可以认为共三种情况:1. 包括内部函数2. 不包括内部函数或者说函数本身3. 所占总数(时间或内存使用)的百分比
复制代码

 

 

参考文章:http://blog.aboutc.net/profiling/17/php-profiler-xhprof

     http://www.cnblogs.com/bluefrog/archive/2012/03/01/2374922.html

     软件下载(并非官方源文件)http://dev.freshsite.pl/php-extensions/xhprof.html

来源:http://www.cnblogs.com/siqi/p/3790186.html

0 0
原创粉丝点击