搜索
首页后端开发php教程非侵入式监控PHP应用性能监控分析

前言

所谓非侵入式监控PHP应用性能,就是不修改现有系统代码,而对系统进行监控。这样的系统才能更容易的应用到PHP应用中。这里抛砖引玉,欢迎大家交流。

方案一

如果只是监控每次请求的访问时间。直接检测nginx的日志即可。在nginx的日志中有两个选项。$request_time 和 $upstream_response_time 。 这两个选项记录了响应时间。

1、$request_time 指的就是从接受用户请求的第一个字节到发送完响应数据的时间,即包括接收请求数据时间、程序响应时间、输出响应数据时间。

2、$upstream_response_time 是指从Nginx向后端(php-cgi)建立连接开始到接受完数据然后关闭连接为止的时间。

如果只是监控后端PHP服务的性能,只要更多的关注 $upstream_response_time 这个选项即可。

方案二

如果还想进一步一个PHP请求处理过程中,具体那部分占用时间比较多,就需要借助xhprof了。xhprof可以生成调用关系图,一目了然的可以看出那部分占用时间比较多。如下图(来源于网络):

下面是完整的搭建步骤:

下载安装xhprof

1、下载编译安装的命令如下:

$wget https://github.com/phacility/xhprof/archive/master.zip$unzip ./xhprof_master.zip$cd ./xhprof_master/extension$/usr/local/php/bin/phpize$./configure --with-php-config=/usr/local/php/bin/php-config$make$make install

注意,我的php是安装在/usr/local/php目录。根据您的情况,适当修改上面的路径。

2、修改配置文件php.ini

$vim /etc/php.ini

底部增加如下内容:

[xhprof]

extension=xhprof.so

xhprof.output_dir=/tmp/xhprof

3、通过下面的命令检测xhprof是否安装成功

$/usr/local/php/bin/php -m

如果以上命令输出内容中有xhprof字样,说明xhprof扩展安装成功。

4、拷贝xhprof相关程序到指定目录

$mkdir -p /www/sites/xhprof$cp -r ./xhprof_master/xhprof_html /www/sites/xhprof$cp -r ./xhprof_master/xhprof_lib /www/sites/xhprof

5、修改nginx配置,以便通过url访问性能数据:在nginx中增加如下代码:

server {        listen  8999;        root    /opt/sites/xhprof/;        index  index.php index.html;        location ~ .*\.php$ {        add_header  Cache-Control "no-cache, no-store, max-age=0, must-revalidate";        add_header  Pragma  no-cache;        add_header Access-Control-Allow-Origin *;        add_header      Via "1.0 xgs-150";        fastcgi_pass   127.0.0.1:9000;        fastcgi_index  index.php;        include fastcgi.conf;        fastcgi_connect_timeout 300;         fastcgi_send_timeout 300;        fastcgi_read_timeout 300;        }   }

6、部署性能数据采集程序把xhprof.php文件部署到 /www/sites/xhprof.php 。xhprof.php文件内容如下:

<?phpdefine("XH_LOG_PATH", "/tmp/xhprof.log");$xh_force_disable = false; //设置性能分析是否启用,设置为true表示关闭。$max_time = 100; //millisecond$xh_enable = false;$start_time = microtime(true);//这里可以设置需要进行性能分析的url和设置的超时时间。如果指定的url,响应时间超过了设置的超时时间,性能分析数据就会被记录下来。超时时间的单位为毫秒。$xh_conf["urls"] = array(    //url => max_time    "/i/content/getdetail.json" => 100,);function xh_save_data(){    global $start_time, $xh_force_disable, $xh_enable, $max_time;    $end_time = microtime(true);    $cost_time = $end_time - $start_time;    $cost_time *= 1000;     if( $cost_time > $max_time && !$xh_force_disable && $xh_enable ){        include_once "/www/sites/xhprof/xhprof_lib/utils/xhprof_lib.php";    	include_once "/www/sites/xhprof/xhprof_lib/utils/xhprof_runs.php";        $xhprof_data = xhprof_disable();    	$objXhprofRun = new XHProfRuns_Default();    	$run_id = $objXhprofRun->save_run($xhprof_data, "xhprof");        $log_data = "cost_time||$cost_time||run_id||$run_id||request_uri||".$_SERVER["REQUEST_URI"]."\n";        //高并发下 可能会出现错乱情况。建议把 const_time run_id request_uri 写入到数据库        file_put_contents(XH_LOG_PATH, $log_data, FILE_APPEND);    }}$xh_request_uri = isset($_SERVER["REQUEST_URI"]) ? $_SERVER["REQUEST_URI"] : "";$arr_xh_cur_url = explode("?", $xh_request_uri);$xh_cur_url = $arr_xh_cur_url[0];if( !$xh_force_disable && isset($xh_conf["urls"][$xh_cur_url]) ){    $xh_enable = true;    $max_time = $xh_conf["urls"][$xh_cur_url];    xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);    register_shutdown_function("xh_save_data"); }else{    $xh_enable = false;}?>

把这个代码文件部署到/www/sites/xhprof.php目录后,就可以启用这个文件了。因为我们想对PHP应用代码不进行代码侵入,那么我们就只能通过如下几种方式启用:

* Nginx/PHP-FPM 方式:

fastcgi_param PHP_VALUE "auto_prepend_file=/www/sites/xhprof.php";

* Apache 方式:

php_value auto_prepend_file "/www/sites/xhprof.php"

* php.ini 方式:

auto_prepend_file="/www/sites/xhprof.php"

注意:如果使用了 opcode 缓存,记得要重启你的 php 进程。

7、查看性能分析日志

$tail /tmp/xhprof.log

$cost_time||200||run_id||adadfdsadad||request_uri||/i/content/getcontent.json

上面输出内容中:

cost_time 耗时 200毫秒

run_id 为 adadfdsadad

request_uri 为 /i/content/getcontent.json

8、根据run_id 查看性能分析数据

http://127.0.0.1:8999/xhprof_html/index.php?run=adadfdsadad

查看方式请参考 http://www.cnblogs.com/siqi/p/3790186.html

注意:

1、在正式启用前,一定要确认不会影响正常的数据输出。确认输出内容无异后,再上线。

2、每个url的max_time不要设置的过小。

3、xhprof会影响线上服务的性能,因此最好只在一台机器上进行监控,或者 修改xhprof.php代码,对请求进行随机监控。

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
高流量网站的PHP性能调整高流量网站的PHP性能调整May 14, 2025 am 12:13 AM

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

PHP中的依赖注入:初学者的代码示例PHP中的依赖注入:初学者的代码示例May 14, 2025 am 12:08 AM

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

PHP性能:是否可以优化应用程序?PHP性能:是否可以优化应用程序?May 14, 2025 am 12:04 AM

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

PHP性能优化:最终指南PHP性能优化:最终指南May 14, 2025 am 12:02 AM

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

PHP依赖注入容器:快速启动PHP依赖注入容器:快速启动May 13, 2025 am 12:11 AM

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

PHP中的依赖注入与服务定位器PHP中的依赖注入与服务定位器May 13, 2025 am 12:10 AM

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

PHP性能优化策略。PHP性能优化策略。May 13, 2025 am 12:06 AM

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

PHP电子邮件验证:确保正确发送电子邮件PHP电子邮件验证:确保正确发送电子邮件May 13, 2025 am 12:06 AM

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

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

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

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。