分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

使用XHProf分析PHP性能瓶颈

发布时间:2023-09-06 01:54责任编辑:胡小海关键词:PHP

安装xhprof

wget http://pecl.php.net/get/xhprof-0.9.4.tgztar zxf xhprof-0.9.4.tgzcd xhprof-0.9.4/extension/sudo phpize./configuresudo makesudo make installcd ../

配置php.ini

[xhprof]extension=xhprof.soxhprof.output_dir=/tmp

注:xhprof不支持php7。需要在github上下载 https://github.com/phacility/xhprof.git。

配置xhprof环境

需要把xhprof压缩包里的两个目录复制到指定目录(假设定义到 /work/xhprof/):

mkdir /work/xhprof/cp -a xhprof_html/ /work/xhprof/cp -a xhprof_lib/ /work/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(); ???} ???include_once "/work/xhprof/xhprof_lib/utils/xhprof_lib.php"; ???include_once "/work/xhprof/xhprof_lib/utils/xhprof_runs.php"; ???$xhprof_runs = new XHProfRuns_Default(); ???$run_id = $xhprof_runs->save_run($xhprof_data, 'xhprof');});

xhprof_enablexhprof_disable是成对出现的,一个是代码运行最前面,一个是最后面。中间是要分析的代码。

经过上面的配置后,我们后续请求项目的接口,xhprof就会分析请求过程中的CPU、内存、耗时等内容。日志保存在xhprof.output_dir目录。

配置web

配置好了,怎么查看日志呢?我们可以搭建一个简单的server:
xhprof.test.com.conf

server { ???listen ??????80; ???server_name ?xhprof.test.com; ???root /work/xhprof/xhprof_html; ???index ?index.html index.php; ???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; ???}}

然后配置虚拟主机xhprof.test.com。重启nginx,打开 xhprof.test.com就可以看到效果了:


非侵入式引入xhprof

前面我们是通过在入口文件添加代码实现了分析的功能。更优雅的方式是新建一个额外的文件 xhprof.inc.php,保存在/work/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(); ???} ???include_once "/work/xhprof/xhprof_lib/utils/xhprof_lib.php"; ???include_once "/work/xhprof/xhprof_lib/utils/xhprof_runs.php"; ???$xhprof_runs = new XHProfRuns_Default(); ???$run_id = $xhprof_runs->save_run($xhprof_data, 'xhprof');});

利用PHP的自动加载功能,在执行代码前注入此文件,编辑php.ini:

auto_prepend_file = /work/xhprof/xhprof.inc.php

然后重启PHP服务。这样所有使用该php环境的都会生效。

或者写到指定项目的nginx配置里也行:

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/xhprof/xhprof.inc.php"; ???????include ???????fastcgi_params; ???}

然后重启nginx服务。这样仅该项目生效。

通过 auto_prepend_file 和 auto_append_file包含的文件在此模式下会被解析,但有些限制,例如函数必须在被调用之前定义。

修改采样频率

默认情况下,xhprof每次都会运行,线上环境如果这么设置,会对性能有影响。

xhprof.inc.php

<?php$profiling = !(mt_rand()%9); if($profiling) xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);register_shutdown_function(function() use($profiling) { ???if($profiling){ ???????$xhprof_data = xhprof_disable(); ???????if (function_exists('fastcgi_finish_request')){ ???????????fastcgi_finish_request(); ???????} ???????include_once "/work/xhprof/xhprof_lib/utils/xhprof_lib.php"; ???????include_once "/work/xhprof/xhprof_lib/utils/xhprof_runs.php"; ???????$xhprof_runs = new XHProfRuns_Default(); ???????$xhprof_runs->save_run($xhprof_data, 'xhprof'); ????}});

参考

1、Xhprof 的配置和使用方法 - 简书
https://www.jianshu.com/p/38e3ae81970c
2、使用XHProf查找PHP性能瓶颈 - 程序猿成长计划 - SegmentFault 思否
https://segmentfault.com/a/1190000003509917
3、PHP性能追踪及分析工具xhprof的安装与使用 - 马新才的技术博客 - SegmentFault 思否
https://segmentfault.com/a/1190000007288664
4、Tideways和xhgui打造PHP非侵入式监控平台 | 我是大熊
http://blog.it2048.cn/article-tideways-xhgui/

使用XHProf分析PHP性能瓶颈

原文地址:https://www.cnblogs.com/52fhy/p/9031529.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved