[xhprof xhgui] 对 hyperf作调用栈分析 | laravel china 社区-380玩彩网官网入口
起步
安装xhprof
xhprof 使用第三方扩展 是在官方扩展上进行删减优化的
git clone "https://github.com/longxinh/xhprof.git"
cd xhprof/extension
phpize
./configure
make
sudo make install
/www # php -m
[php modules]
...
xhprof
...
安装xhgui
git clone https://github.com/perftools/xhgui
cd xhgui
docker compose up -d
项目与xhgui互通
把项目容器和 xhgui 容器放在同一网络进行通信并实现文件共享
version: "3.7"
services:
xhgui:
# https://hub.docker.com/r/xhgui/xhgui/tags
image: xhgui/xhgui:latest
restart: always
volumes:
- ./config:/var/www/xhgui/config
- ./nginx.conf:/etc/nginx/http.d/default.conf:ro
environment:
- xhgui_mongo_hostname=mongo
- xhgui_mongo_database=xhprof
ports:
- "8142:80"
mongo:
image: percona/percona-server-mongodb:3.6
# (case sensitive) engine: mmapv1, rocksdb, wiredtiger, inmemory
command: --storageengine=wiredtiger
restart: always
environment:
- mongo_initdb_database=xhprof
volumes:
- ./mongo.init.d:/docker-entrypoint-initdb.d
- mongodb:/data/db
ports:
- "27017:27017"
volumes:
webroot-share:
mongodb:
安装xhprof收集请求信息
composer require perftools/php-profiler
中间件写入
<?php
namespace app\middleware;
use exception;
use hyperf\contract\configinterface;
use hyperf\di\annotation\inject;
use psr\http\message\responseinterface;
use psr\http\message\serverrequestinterface;
use psr\http\server\middlewareinterface;
use psr\http\server\requesthandlerinterface;
use throwable;
use xhgui\profiler\profiler;
class memorytrackermiddleware implements middlewareinterface
{
#[inject]
protected configinterface $config;
/**
* @throws exception
*/ public function process(serverrequestinterface $request, requesthandlerinterface $handler): responseinterface
{
try {
$config = $this->config->get('profiler');
$profiler = new profiler($config);
$profiler->start();
$response = $handler->handle($request);
$data = $profiler->disable();
$data['meta']['url'] = $request->geturi()->getpath();
$data['meta']['server'] = array_change_key_case($request->getserverparams(), case_upper);;
$profiler->save($data);
return $response;
} catch (throwable $e) {
throw new exception($e->getmessage(), $e->getcode());
}
}
}
运行
请求列表
详细参数
调用图
火焰图
运行对比
本作品采用《cc 协议》,转载必须注明作者和本文链接
可以贴一下profiler的配置吗