这篇文章主要介绍了关于使用XHProf分析PHP性能瓶颈的方法二,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
上一篇文章里,我们介绍了如何基于xhprof扩展来分析PHP性能,并记录到日志里,最后使用xhprof扩展自带的UI在web里展示出来。本篇文章将讲述2个知识点:
使用xhgui代替xhprof的默认UI界面,更便于分析
使用tideways扩展替换xhprof扩展
使用更漂亮的UI: xhgui
xhgui支持 XHProf, Uprofiler或者Tideways 扩展,也就是说,只要安装了这几种扩展中的一种即可。
本次测试中,实际使用了tideways扩展(切换为XHProf扩展后web里看不到数据,原因未知。切换为Uprofiler也没有数据。)。
xhprof虽然来自facebook但已经很久不更新,官方源已经显示This package is abandoned and no longer maintained(此包已废弃,不再维护)。tideways恰好相反,一直有商业公司在维护,并且积极的支持了PHP7。两个扩展都是开源的,综上所述我建议大家选择tideways来分析代码。
安装tideways扩展:
wget https://github.com/tideways/php-xhprof-extension/archive/v4.1.5.tar.gz -O php-xhprof-extension-4.1.5.tar.gz tar xzf /php-xhprof-extension-4.1.5.tar.gz cd php-xhprof-extension-4.1.5 phpize ./configure make && make install
安装xhgui
cd /work/ git clone https://github.com/perftools/xhgui.git xhgui
如果需要安装中文界面的,可以:
git clone https://github.com/laynefyc/xhgui-branch.git xhgui
然后安装xhgui依赖:
cd xhgui php install.php
安装需要等待几分钟,请耐心等待。
设置缓存目录的权限,允许nginx创建文件:
chmod -R 777
xhgui已经把注入入口文件都写好了,位于external/header.php
,无需我们手动去写类似上一篇的xhprof.inc.php
注入文件。
安装MongoDB及客户端
xhgui 把日志写到了MongoDB,所以使用xhgui需要安装MongoDB服务端。此处省略安装、启动MongoDB服务端过程。
为提高 MongoDB 的性能,你可以运行以下指令以添加索引:
$ /usr/local/mongodb/bin/mongo > use xhprof db.results.ensureIndex( { 'meta.SERVER.REQUEST_TIME' : -1 } ) db.results.ensureIndex( { 'profile.main().wt' : -1 } ) db.results.ensureIndex( { 'profile.main().mu' : -1 } ) db.results.ensureIndex( { 'profile.main().cpu' : -1 } ) db.results.ensureIndex( { 'meta.url' : 1 } )
同理,由于xhgui是PHP写的,还需要读取MongoDB里的数据,需要安装MongoDB php 客户端:
pecl install mongodb
然后在php.ini文件添加配置:
[mongo] extension=mongo.so
查看扩展是否安装成功:
php -m | grep mongo
然后重启php-fpm服务。
配置xhgui
xhgui的config目录有一个config.default.php,复制为config.php
,如果mongodb地址不是默认的,修改:
'db.host' => 'mongodb://127.0.0.1:27017',
还有修改采样频率,默认是1/100,测试的话改为true:
'profiler.enable' => function() { //return rand(1, 100) === 42; return true; },
配置项目注入
上一篇文章中,我们介绍到,注入的入口文件可以写到php.ini或者nginx,我建议写在nginx配置,这样只会影响该nginx对应的项目,而不是所有使用该php环境的项目。入口文件使用xhgui自带的注入文件:
jifen.cc.conf
location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PHP_VALUE "auto_prepend_file=/work/xhgui/external/header.php"; include fastcgi_params; }
配置xhgui web
我们修改xhprof.test.com.conf为:
server { listen 80; server_name xhprof.test.com; #root /work/xhprof/xhprof_html; root /work/xhgui/webroot/; index index.php index.html; location / { if (!-e $request_filename) { rewrite . /index.php last; } } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
重启nginx服务。
我们请求几次应用的接口,打开浏览器输入http://xhprof.test.com/,可以看到:
点击某次请求进去看详情:
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
以上是使用XHProf分析PHP性能瓶颈的方法二的详细内容。更多信息请关注PHP中文网其他相关文章!

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Dreamweaver Mac版
视觉化网页开发工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

记事本++7.3.1
好用且免费的代码编辑器

WebStorm Mac版
好用的JavaScript开发工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。