HP5.3或之前版本可以去pecl(http://pecl.php.net)下载xhprof扩展安装。

但pecl上的版本不支持PHP5.4

可以到github上的xhprof库中下载:https://github.com/facebook/xhprof

下载后进行解压安装

1 cd xhprof-master/extension/
2 phpize
3 ./configure --enable-xhprof
4 make
5 sudo make install

更改php.ini

1 [xhprof]
2 extension=xhprof.so
3 xhprof.output_dir="/document/gbyukg/www/test/xhprof"

重新启动apache。

简单使用:

1 // start profiling
2 xhprof_enable();
3
4 // run program
5 ......
6
7 // stop profiler
8 $xhprof_data = xhprof_disable();

xhprof_enable()允许接收一些参数,默认情况下,只会统计方法的调用次数和消耗的时间,通过传递参数(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY)打开统计CPU和内存使用率(在实际生产环境中会好关闭CPU统计信息,因为它会占用很多资源),同样也可以通过传递XHPROF_FLAGS_NO_BUILTINS参数来禁用对PHP内置函数的统计,甚至通过传递第二个参数来禁用对指定函数的统计,如:

1 // ignore builtin functions and call_user_func* during profiling
2 $ignore = array('call_user_func', 'call_user_func_array');
3 xhprof_enable(0, array('ignored_functions' => $ignore));

xhprof界面工具
页面工具存储在源代码目录中的xhprof_html文件夹下,同时需要xhprof_lib文件夹下的库文件。将这2个文件夹放到php.ini文件中指定的报告生成的目录中(应当是web服务器目录中)。
创建头和尾文件:

1 <?php
2 if (extension_loaded('xhprof')) {
3 include_once '/usr/local/lib/php/xhprof_lib/utils/xhprof_lib.php';
4 include_once '/usr/local/lib/php/xhprof_lib/utils/xhprof_runs.php';
5 xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
6 }
 1 if (extension_loaded('xhprof')) {
2 $profiler_namespace = 'myapp'; // namespace for your application
3 $xhprof_data = xhprof_disable();
4 $xhprof_runs = new XHProfRuns_Default();
5 $run_id = $xhprof_runs->save_run($xhprof_data, $profiler_namespace);
6
7 // url to the XHProf UI libraries (change the host name and path)
8 $profiler_url = sprintf('http://myhost.com/xhprof/xhprof_html/index.php?run=%s&source=%s', $run_id, $profiler_namespace);
9 echo '<a href="'. $profiler_url .'" target="_blank">Profiler output</a>';
10 }

最后配置.htaccess文件使之自动调用上述2个文件

1 php_value auto_prepend_file /document/gbyukg/www/xhprof/header.php
2 php_value auto_append_file /document/gbyukg/www/xhprof/footer.php

之后访问xhprof_html目录下的index.php文件

// start profiling
xhprof_enable();
//profile CPU time and/or memory usage
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); // run program
...... // stop profiler
$xhprof_data = xhprof_disable();
 1 // start profiling
2 xhprof_enable();
3
4 // run program
5 ....
6
7 // stop profiler
8 $xhprof_data = xhprof_disable();
9
10 //
11 // Saving the XHProf run
12 // using the default implementation of iXHProfRuns.
13 //
14 include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
15 include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
16
17 $xhprof_runs = new XHProfRuns_Default();
18
19 // Save the run under a namespace "xhprof_foo".
20 //
21 // **NOTE**:
22 // By default save_run() will automatically generate a unique
23 // run id for you. [You can override that behavior by passing
24 // a run id (optional arg) to the save_run() method instead.]
25 //
26 $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");

在生成的统计表格中,可以按照各个统计列进行排序,如:
Number of Calls:方法的调用次数
Memory Usage:内存占用率
Peak Memory Usage:内存峰值
CUP Time:CPU占用时间(包括内核使用时间和用户代码调用时间)
Wall Time:执行时间(如果执行一个远程调用,会包括CPU访问远程服务器、解析响应以及等待响应的时间和其它一些资源所占用的时间)

内存占用情况和CPU使用时间更进一步的分为Inclusive和Exclusive
Inclusive Time 包含该方法自己所占用的时间和该方法的所有子孙方法所占用的总资源或时间
Exclusive Time 仅仅包含该方法本身所占用的资源或时间


使用图形界面

可以通过点击页面上方的View Full Callgraph链接打开生成的图像模型,注意,在使用此功能前,请确保系统已经安装过GraphViz,Ubunt中可通过下面命令进行安装:

1 apt-get install graphviz

当需要对当前项目进行优化时,我们首先需要对CPU进行排序,找出CPU占用时间最多的方法进行优化,之后按照优化内存使用率进行排序,对内存使用率最高的方法进行优化,接着按照wall time进行排序优化。

差别与统计报告

XHProf允许你对两次统计结果进行对比,对比方式是:

1 http://%xhprof-ui-address%/index.php?run1=XXX&amp;run2=YYY&amp;source=myapp

XXX和YYY是run id,myapp是命名空间,即save_run的第二个参数

同样也可以经多次生成的统计结果合并到一起生成一个统计报告:

1 http://%xhprof-ui-address%/index.php?run=XXX,YYY,ZZZ&amp;source=myapp

提示:
在对项目进行优化时,首先需要知道哪些地方需要优化,例如,项目中使用Zend Framewor,首先需要知道运行ZF框架所需要的资源以及时间,如果ZF运行时需要占用2.5M内存,那么不可能要让整个项目的内存占用小于2.5M。
同时并不需要每次运行都生成统计报告,可修改头问文件使之运行1000次生成一次报告。

header.php

1 $xhprof_on = false;
2 if (mt_rand(1, 10000) === 1) {
3 $xhprof_on = true;
4 if (extension_loaded('xhprof')) {
5 include_once '/usr/local/lib/php/xhprof_lib/utils/xhprof_lib.php';
6 include_once '/usr/local/lib/php/xhprof_lib/utils/xhprof_runs.php';
7 xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
8 }
9 }

footer.php

 1 if ($xhprof_on && extension_loaded('xhprof')) {
2 $profiler_namespace = 'myapp'; // namespace for your application
3 $xhprof_data = xhprof_disable();
4 $xhprof_runs = new XHProfRuns_Default();
5 $run_id = $xhprof_runs->save_run($xhprof_data, $profiler_namespace);
6
7 // url to the XHProf UI libraries (change the host name and path)
8 $profiler_url = sprintf('http://myhost.com/xhprof/xhprof_html/index.php?run=%s&source=%s', $run_id, $profiler_namespace);
9 echo '<a href="'.$profiler_url.'" target="_blank">Profiler output</a>';
10 }

报告字段说明

Function Name:方法名称。
Calls:方法被调用的次数。
Calls%:方法调用次数在同级方法总数调用次数中所占的百分比。
Incl.Wall Time(microsec):方法执行花费的时间,包括子方法的执行时间。(单位:微秒)
IWall%:方法执行花费的时间百分比。
Excl. Wall Time(microsec):方法本身执行花费的时间,不包括子方法的执行时间。(单位:微秒)
EWall%:方法本身执行花费的时间百分比。
Incl. CPU(microsecs):方法执行花费的CPU时间,包括子方法的执行时间。(单位:微秒)
ICpu%:方法执行花费的CPU时间百分比。
Excl. CPU(microsec):方法本身执行花费的CPU时间,不包括子方法的执行时间。(单位:微秒)
ECPU%:方法本身执行花费的CPU时间百分比。
Incl.MemUse(bytes):方法执行占用的内存,包括子方法执行占用的内存。(单位:字节)
IMemUse%:方法执行占用的内存百分比。
Excl.MemUse(bytes):方法本身执行占用的内存,不包括子方法执行占用的内存。(单位:字节)
EMemUse%:方法本身执行占用的内存百分比。
Incl.PeakMemUse(bytes):Incl.MemUse峰值。(单位:字节)
IPeakMemUse%:Incl.MemUse峰值百分比。
Excl.PeakMemUse(bytes):Excl.MemUse峰值。单位:(字节)
EPeakMemUse%:Excl.MemUse峰值百分比。

  

PHP5.4安装xhprof扩展[不要去pecl下载]的更多相关文章

  1. 转载:【Linux+windows】PHP5.5安装PHPRedis扩展

    首先,你必须安装了 Redis服务器,然后才能安装php-redis扩展,就像先安装mysql,然后再将php-mysql扩展安装并引入(区别是:php-redis扩展插件php没有自带,php-my ...

  2. window下php5.5安装redis扩展

    redis是现在比较流行的noSQL,主流大型网站都用的比较多,很多同学不知道怎么安装,这里介绍在windows下面安装以及扩展,提供学习使用,实际使用环境多在Linux下. 1.phpinfo(), ...

  3. [转载]在Windows下为PHP5.6安装redis扩展和memcached扩展

    一.php安装redis扩展   1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本       2.根据PHP版本号,编译器版本号和CPU架构, 选择php_redis-2.2 ...

  4. Windows环境下为PHP5.6安装redis扩展和memcached扩展

    一.php安装redis扩展   1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本       2.根据PHP版本号,编译器版本号和CPU架构, 选择php_redis-2.2 ...

  5. 在Windows下为PHP5.6安装redis扩展和memcached扩展

    一.php安装redis扩展   1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本       2.根据PHP版本号,编译器版本号和CPU架构, 选择php_redis-2.2 ...

  6. windows php5.5安装redis扩展,并用redis存储session

    1.确定安装版本 先通过phpinfo()查看php的Compiler.Architecture.Thread Safety,其中Thread Safety如果是enabled,那么就是线程安全(ts ...

  7. php5.6 安装intl扩展

    PHP intl 是国际化扩展,是ICU 库的一个包装器.所以在安装PHP intl扩展前要先安装ICU库,安装ICU库的具体步骤见:http://www.linuxeye.com/Linux/237 ...

  8. php5.4安装fileinfo扩展

    Fileinfo 扩展是libmagic库的一个封装,可以用来获得文件的一些信息,如MIME类型 安装php_fileinfo扩展 1.windows 用phpinfo()查看php版本 下载 选择合 ...

  9. 在Windows下为PHP5.6安装redis扩展

    Redis 安装 Window 下安装 下载地址:https://github.com/MSOpenTech/redis/releases. Redis 支持 32 位和 64 位.这个需要根据你系统 ...

随机推荐

  1. Xamarin.Android开发实践(九)

    Xamarin.Android之ActionBar与菜单 一.选项卡 如今很多应用都会使用碎片以便在同一个活动中能够显示多个不同的视图.在 Android 3.0 以上的版本中,我们已经可以使用Act ...

  2. 64位ubuntu安装32位jdk

    转自:http://blog.csdn.net/anladeyatou/article/details/8213334 ubuntu-11.10-desktop-amd64 jdk-6u23-linu ...

  3. 常用的Linux操作

    1.运行.sh文件 第一种方法: 首先你要打开一个终端. 然后输入sudo su 随后输入密码.这样就取得了root用户权限. 然后找到那个文件 执行./sh文件名字 这样.sh就运行了. 第二种方法 ...

  4. Spotlight on MySQL监控MySQL服务器

    第一步: 下载并安装mysql-connector-3.5x Spotlight on MySQL 连接mysql必须使用mysql-connector-3.5x,5.3.2版本我试了下不行,有兴趣可 ...

  5. 使用java自带的定时任务ScheduledThreadPoolExecutor

    ScheduledThreadPoolExecutor是ThreadPoolExecutor的子类: JDK api里是这么说的: ThreadPoolExecutor,它可另行安排在给定的延迟后运行 ...

  6. 如何找到所有HTML Select 标签的选中项?

    $(‘[name=NameofSelectedTag]:selected’);

  7. HDU3729 I'm Telling the Truth(字典序最大的最大流)

    题目大概说n个学生,都各自有一个互不相同的成绩排名,他们各自说了他们成绩排名所在区间,问最多有几个学生没说谎以及字典序最大的没说谎的学生序列. 学生作为一个X部的点,排名作为Y部的点,学生与其成绩排名 ...

  8. ANSYS:Negative pivot encountered

    做柔性体仿真,从ANSYS导入模态中性文件时有这个报错. Negative pivot encountered. This is  likely caused by insufficient disp ...

  9. BZOJ3839 : [Pa2013]Działka

    对于每个询问,首先可以通过扫描线+线段树求出四个方向的第一个点,询问范围等价于框住这些点的最小矩形. 对于一个点$i$,预处理出: $A[i][j]$:$i$往左下角按凸壳走到$j$时,凸壳上相邻两点 ...

  10. POJ 3335 Rotating Scoreboard(多边形的核)

    题目链接 我看的这里:http://www.cnblogs.com/ka200812/archive/2012/01/20/2328316.html 然后整理一下当做模版.0换成eps,会wa,应该要 ...