[xhprof + xhgui] 对 hyperf作调用栈分析
起步
安装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的配置吗