Home > Backend Development > PHP Tutorial > Use xhprof for online PHP performance tracking and analysis

Use xhprof for online PHP performance tracking and analysis

WBOY
Release: 2016-08-08 09:29:53
Original
1031 people have browsed it
I have been using Xdebug for PHP performance analysis before, which is sufficient for local development environments. However, if it is an online environment, xdebug consumes a lot of money and the configuration is not flexible enough. Therefore, it is recommended to use xhprof for PHP in online environments. Performance tracking and analysis. xhprof installation and easy usagexhprof is Facebook’s open source lightweight PHP performance analysis tool. It can be installed directly through pecl in Linux environment. For example, under Ubuntu, it only requires 3 lines of instructions pecl install xhprof-beta echo "extension=xhprof.so" > /etc/php5/fpm/conf.d/xhprof.ini service php5-fpm restart and then it can be installed through phpinfo() Check if the extension has been loaded. How to use it specifically? The xhprof project has provided examples and simple UI. Download the xhprof project to the web server. If it can be accessed through http://localhost/xhprof/, then visit http://localhost/ xhprof/examples/sample.phpYou can see some output, and you are prompted to view the results by visiting http:///index.php?run=XXX&source=xhprof_foo. Next, visit http://localhost/xhprof/xhprof_html/ to see the saved results, listing all function calls and the time spent. Analyze the sample code sample.php. The key part is only 2 lines: //开启xhprof并开始记录 xhprof_enable(); //运行一些函数 foo(); //停止记录并取到结果$xhprof_data = xhprof_disable(); $xhprof_data records all the function call time and CPU memory consumption during the single-step running of the program. What indicators can be recorded specifically? Through the entry parameter control of xhprof_enable, the subsequent processing has nothing to do with the xhprof extension. Basically, a storage class XHProfRuns_Default is written to serialize and save $xhprof_data to a certain directory, which can be done through XHProfRuns_Default(__DIR__) Output the results to the current directory. If not specified, xhprof.output_dir in the php.ini configuration file will be read. If still not specified, it will be output to /tmp. xhprof_html/index.php Organize and visualize the recorded results. The default UI lists:
  • funciton name : 函数名
  • calls: 调用次数
  • Incl. Wall Time (microsec): 函数运行时间(包括子函数)
  • IWall%:函数运行时间(包括子函数)占比
  • Excl. Wall Time(microsec):函数运行时间(不包括子函数)
  • EWall%:函数运行时间(不包括子函数)
  • 每一项应该不难理解,以项目自带的sample.php为例,示例中编写了一个main()函数,main()函数中调用foo()、bar()等一些子函数进行了一点字符处理。整个程序运行过程中,main()函数只运行了一次,并且由于main()函数中包括了所有的逻辑,所以main()函数的IWall%占比为100%,但是由于main()函数的功能都是由子函数实现的,因此main()函数的EWall%只有0.3%,而foo()函数完成了主要的工作,EWall%有98.1%。因此在分析更大型的程序时,往往需要根据这几项指标分别排序,从不同的角度审视性能消耗。xhprof_html/index.php中还可以看到[View Full Callgraph]链接,点击后可以绘制出一张可视化的性能分析图,如果点击后报错的话,可能是缺少依赖graphviz,ubuntu可以通过apt安装apt-get install graphviz 更好的注入方式了解了上面这些,其实就已经可以将xhprof整合到任何我们已有的项目中去了。目前大部分MVC框架都有唯一的入口文件,只需要在入口文件的开始处注入xhprof的逻辑//开启xhprof xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU); //在程序结束后收集数据 register_shutdown_function(function() { $xhprof_data = xhprof_disable(); //让数据收集程序在后台运行if (function_exists('fastcgi_finish_request')) { fastcgi_finish_request(); } //保存xhprof数据 ... }); 但是这样免不了要修改项目的源代码,其实php本身就提供了更好的注入方式,比如将上述逻辑保存为/opt/inject.php,然后修改php fpm配置文件vi /etc/php5/fpm/php.ini 修改auto_prepend_file配置auto_prepend_file = /opt/inject.php这样所有的php-fpm请求的php文件前都会自动注入/opt/inject.php文件如果使用Nginx的话,还可以通过Nginx的配置文件设置,这样侵入性更小,并且可以实现基于站点的注入。fastcgi_param PHP_VALUE "auto_prepend_file=/opt/inject.php"; 更好的分析工具:xhprof.io还是xhpgui注入代码后我们还需要实现保存xhprof数据以及展示数据的UI,听起来似乎又是一大堆工作,有现成的轮子可以用吗?经过搜索和比较,貌似比较好的选择有xhprof.io以及xhpgui。两个项目做得事情差不多,都提供了xhprof数据保存功能以及一套索引展示数据的UI,下面是一些比较xhprof.io
  • ? 年久失修
  • ? 保存xhprof数据到MySQL
  • ? 支持域名、URI等多个维度的数据索引
  • ? 函数调用记录完整,内核级别函数都能显示
  • ? 无法针对个别URI开启
  • ? 注入被分割成两个文件,如果程序被强制中断时xhprof数据将无法收集
  • xhgui
  • ? 保存xhprof数据到MongoDB
  • ? 不支持域名索引
  • ? 函数调用记录不完整,部分内核级别函数(如扩展内)无法显示
  • ? 有配置文件可以控制开启条件
  • ? 注入只有一个文件
  • ? 狂拽酷炫的基于D3.js的调用关系动态图
  • 可以看到其实两个项目都不够完善,相对而言xhgui不支持域名索引对于线上调试来说是无法忍受的,因此我最后的选择是使用xhprof.io,但是自己进行了微量的调整,修改后的xhprof.io修正版支持:
  • ? 增加开启开关配置,可以针对个别URI开启
  • ? 注入文件合并为一个
  • xhprof.io修正版安装与使用安装及配置方法如下,假设web服务器根目录为/opt/htdocscd /opt/htdocs git clone https://github.com/EvaEngine/xhprof.io.git cd xhprof.io/ composer install cp xhprof/includes/config.inc.sample.php xhprof/includes/config.inc.php vi xhprof/includes/config.inc.php 在MySQL中建立xhprof.io数据库,假设数据库名为xhprof,然后导入xhprof/setup/database.sql配置文件config.inc.php中需要调整
  • 'url_base' => 'http://localhost/xhprof.io/', 这是xhprof.io界面所在路径
  • 'pdo' => new PDO('mysql:dbname=xhprof;host=localhost;charset=utf8', 'root', 'password'), 根据MySQL实际情况调整配置
  • enable 这是一个匿名函数,当匿名函数返回true时启用xhprof数据收集
  • 通过配置enable项,就可以实现线上调试的需求,比如始终开启xhprof'enable' => function() { returntrue; } 1/100概率随机开启xhprof'enable' => function() { return rand(0, 100) === 1; } 网页携带参数debug=1时开启xhprof'enable' => function() { return !empty($_GET['debug']); } 网页URL为特定路径时开启'enable' => function() { return strpos($_SERVER['REQUEST_URI'], '/testurl') === 0; } 最后按上文所述,在要配置的项目中包含xhprof.io/inc/inject.php即可。线上环境操作时务必要胆大心细,如果没有结果尤其注意需要检查xhprof扩展是否安装。附录:xhpgui的安装方法apt-get install mongodb php5-mongo php5-mcrypt cp /etc/php5/mods-available/mcrypt.ini /etc/php5/fpm/conf.d/ cp /etc/php5/mods-available/mcrypt.ini /etc/php5/cli/conf.d/ cd /opt/htdocs git clone https://github.com/perftools/xhgui.git cd xhgui composer install cp config/config.default.php config/config.php chown www-data.www-data -R cache 编辑Nginx配置文件加入fastcgi_param PHP_VALUE "auto_prepend_file=/opt/htdocs/xhgui/external/header.php"; 收集数据过多时可以清空mongodbmongo use xhprof; db.dropDatabase();

    以上就介绍了使用xhprof进行线上PHP性能追踪及分析,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

    Related labels:
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template