[xhprof xhgui] 对 hyperf作调用栈分析 | laravel china 社区-金年会app官方网

起步

安装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 协议》,转载必须注明作者和本文链接
从零开发一个电商项目,功能包括电商后台、商品 & sku 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
以构建论坛项目 larabbs 为线索,展开对 laravel 框架的全面学习。应用程序架构思路贴近 laravel 框架的设计哲学。
讨论数量: 1

可以贴一下profiler的配置吗

3周前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
47
粉丝
7
喜欢
34
收藏
63
排名:465
访问:1.9 万
博客标签
社区赞助商
网站地图