xphrof 安装和使用
xhprof
安装
git clone https://github.com/longxinH/xhprof.git ./xhprof
cd xhprof/extension/
/path/to/php7/bin/phpize
./configure --with-php-config=/path/to/php7/bin/php-config
make && sudo make install
使用
xhprof_enable();
register_shutdown_function(function () {
include_once __DIR__ . "/xhprof_lib/utils/xhprof_lib.php";// 包含在扩展目录里
include_once __DIR__ . "/xhprof_lib/utils/xhprof_runs.php";
$xhprof_data = xhprof_disable();
$xhprof_runs = new XHProfRuns_Default();
$type = "project_type";
$run_id = $xhprof_runs->save_run($xhprof_data, $type);
});
效果
http://www.example.com/xhprof_html/index.php?run={$run_id}&source={$type}&all=1
xhprof 已经好久不维护了,改用 xhgui
xhgui
xhgui 包含两部分 GUI界面 和 profile 收集器,首先安装扩展 tideways_xhprof 收集 php 运行数据
安装扩展 tideways_xhprof
https://github.com/tideways/php-xhprof-extension#installation
线上项目使用 收集器 php-profiler
composer require perftools/php-profiler:v0.5.0 -- 装哪个版本就看哪个版本的README
GUI界面就是一个web项目,以下是 nginx 配置
server {
listen 80;
server_name xhgui.wsl;
# root directive should be global
root /mnt/e/xhgui2/webroot/;
index index.php;
access_log off;
error_log /data/wwwlogs/xhgui.wsl_nginx.log error;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
profile 收集的信息可以存放文件,也可以上传到 GUI 提供的接口,使用 mongodb 保存数据
// GUI 信息配置
/path/to/xgui-store/config/config.default.php
线上项目开始收集
try {
$config = [
'profiler.enable' => function () {
return true;
},
'save.handler' => \Xhgui\Profiler\Profiler::SAVER_UPLOAD,
'save.handler.upload' => [
'uri' => 'http://xhgui.wsl/run/import', // 上传到GUI提供的接口
'timeout' => 3,
'token' => 'zhorz',
],
];
$profiler = new \Xhgui\Profiler\Profiler($config);
$profiler->start();
$profiler->registerShutdownHandler();
} catch (Exception $e) {
echo $e->getMessage();
}
最后就是到 http://xhgui.wsl 分析,每个请求的函数栈执行时间、消耗内存等情况一目了然~