[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());
        }
    }
}

运行

请求列表

[xhprof + xhgui] 对 hyperf作调用栈分析

详细参数

[xhprof + xhgui] 对 hyperf作调用栈分析

调用图

[xhprof + xhgui] 对 hyperf作调用栈分析

火焰图

[xhprof + xhgui] 对 hyperf作调用栈分析

运行对比

[xhprof + xhgui] 对 hyperf作调用栈分析

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
讨论数量: 1

可以贴一下profiler的配置吗

2个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!