定制 aaron-dev/xhprof-webman 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

aaron-dev/xhprof-webman

Composer 安装命令:

composer require aaron-dev/xhprof-webman

包简介

aaron-dev/xhprof-webman is a code performance analysis plugin compatible with webman, Laravel, ThinkPHP and Hyperf. Uses xhprof extension for profiling, Redis for storage, provides browser-based performance analysis reports.

README 文档

README

兼容 webman / Laravel / ThinkPHP / Hyperf 的代码性能分析插件。

基于 xhprof 扩展采集数据并存入 Redis,开发者可通过浏览器快速访问性能分析报告,排查代码性能瓶颈。

环境要求

  • PHP >= 8.0
  • xhprof 扩展
  • redis 扩展
  • Redis 服务

安装

php.ini 中增加 xhprof 配置:

[xhprof]
extension=xhprof.so
xhprof.output_dir=/tmp/xhprof

Composer 安装:

composer require aaron-dev/xhprof-webman

框架配置

Webman

1. 注册全局中间件config/middleware.php

return [
    '' => [
        ErikWang2013\Xhprof\Webman\XhprofMiddleware::class,
    ],
];

2. 创建控制器

<?php

namespace app\controller;

use support\Request;
use ErikWang2013\Xhprof\Webman\Xhprof;

class XhprofController
{
    public function index(Request $request)
    {
        return Xhprof::index();
    }
}

3. 注册路由config/route.php

use Webman\Route;
use ErikWang2013\Xhprof\Webman\StaticController;

Route::get('/xhprof', [app\controller\XhprofController::class, 'index']);
Route::get('/xhprof-assets/{path:.+}', [StaticController::class, 'serve']);

// CallGraph 调用图路由(可选,需要服务器安装 graphviz `dot` 命令)
// Route::any('/xhprof/callgraph', [app\controller\XhprofController::class, 'callgraph']);

4. 配置 — 见 config/plugin/aaron-dev/xhprof/xhprof.php

Laravel

1. 注册中间件app/Http/Kernel.php

protected $middleware = [
    // ...
    \ErikWang2013\Xhprof\Laravel\Middleware::class,
];

2. 创建控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use ErikWang2013\Xhprof\Core\Xhprof;

class XhprofController extends Controller
{
    public function index(Request $request)
    {
        Xhprof::bootstrap();
        return Xhprof::index();
    }
}

3. 注册路由routes/web.php

use App\Http\Controllers\XhprofController;
use ErikWang2013\Xhprof\Core\StaticController;
use Illuminate\Support\Facades\Route;

Route::get('/xhprof', [XhprofController::class, 'index']);
Route::get('/xhprof-assets/{path}', function ($path) {
    $req = new \ErikWang2013\Xhprof\Laravel\Adapter\RequestAdapter(request());
    $res = new \ErikWang2013\Xhprof\Laravel\Adapter\ResponseAdapter(response(''));
    return StaticController::serve($req, $res)->send();
})->where('path', '.*');

// CallGraph 调用图路由(可选,需要 graphviz `dot` 命令)
// Route::any('/xhprof/callgraph', [XhprofController::class, 'callgraph']);

4. 发布配置

php artisan vendor:publish --tag=xhprof-config

配置文件在 config/xhprof.php。Laravel 支持自动发现 ServiceProvider。

ThinkPHP

1. 注册中间件app/middleware.php

return [
    \ErikWang2013\Xhprof\Thinkphp\Middleware::class,
];

2. 创建控制器

<?php

namespace app\controller;

use think\Request;
use ErikWang2013\Xhprof\Core\Xhprof;

class XhprofController
{
    public function index(Request $request)
    {
        Xhprof::bootstrap();
        return Xhprof::index();
    }
}

3. 注册路由route/app.php

use think\facade\Route;
use ErikWang2013\Xhprof\Core\StaticController;
use ErikWang2013\Xhprof\Thinkphp\Adapter\RequestAdapter;
use ErikWang2013\Xhprof\Thinkphp\Adapter\ResponseAdapter;

Route::get('/xhprof', 'app\controller\XhprofController@index');
Route::get('/xhprof-assets/[:path]', function ($path = '') {
    $req = new RequestAdapter(app('request'));
    $res = new ResponseAdapter(response(''));
    return StaticController::serve($req, $res)->send();
})->pattern(['path' => '.*']);

// CallGraph 调用图路由(可选,需要 graphviz `dot` 命令)
// Route::any('/xhprof/callgraph', 'app\controller\XhprofController@callgraph');

4. 配置 — 复制 vendor/aaron-dev/xhprof-webman/src/Thinkphp/config/xhprof.php 到项目 config/xhprof.php

Hyperf

1. 中间件自动注册 — ConfigProvider 自动将中间件加入 HTTP 中间件队列。

2. 创建控制器

<?php

namespace App\Controller;

use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use ErikWang2013\Xhprof\Core\Xhprof;

#[Controller(prefix: '/xhprof')]
class XhprofController
{
    #[RequestMapping(path: '')]
    public function index()
    {
        Xhprof::bootstrap();
        return Xhprof::index();
    }
}

3. 静态资源路由config/routes.php

use Hyperf\HttpServer\Router\Router;
use ErikWang2013\Xhprof\Core\StaticController;
use ErikWang2013\Xhprof\Hyperf\Adapter\RequestAdapter;
use ErikWang2013\Xhprof\Hyperf\Adapter\ResponseAdapter;
use Hyperf\Context\ApplicationContext;

Router::get('/xhprof-assets/{path:.+}', function ($path) {
    $container = ApplicationContext::getContainer();
    $req = new RequestAdapter($container->get(\Hyperf\HttpServer\Contract\RequestInterface::class));
    $res = new ResponseAdapter($container->get(\Hyperf\HttpServer\Contract\ResponseInterface::class));
    return StaticController::serve($req, $res)->send();
});

// CallGraph 调用图路由(可选,需要 graphviz `dot` 命令)
// Router::addRoute(['GET', 'POST'], '/xhprof/callgraph', 'App\Controller\XhprofController@callgraph');

4. 发布配置

php bin/hyperf.php vendor:publish aaron-dev/xhprof-webman

配置输出在 config/autoload/xhprof.php

配置项说明

所有框架共用以下配置项:

配置 类型 默认值 说明
enable bool true 是否启用性能分析
time_limit int 0 仅记录响应超过 n 秒的请求,0 表示全部
log_num int 1000 最大记录条数
view_wtred int 3 列表耗时超过 n 秒标红
ignore_url_arr array ["/test"] 忽略的 URL 路径
assets_url string /xhprof-assets 静态资源 URL 前缀

CallGraph 调用图

报告页面中的「CallGraph」功能依赖服务器安装 Graphviz:

# Debian/Ubuntu
apt install graphviz

# CentOS
yum install graphviz

安装后在各框架中注册 CallGraph 路由(见上方各框架路由配置中的注释部分)即可使用。

手动初始化

如果自动检测框架失败,可以手动注入适配器:

use ErikWang2013\Xhprof\Core\Xhprof;

Xhprof::bootstrap(
    new MyRequestAdapter($request),
    new MyResponseAdapter($response),
    new MyConfigAdapter(),
    new MyCacheAdapter(),
    new MyLoggerAdapter()
);

作者

艾瑞可 erik

开源不易,欢迎支持

微信 支付宝
微信 支付宝

本插件参考 phacility/xhprofphpxxb/xhprof

aaron-dev/xhprof-webman 适用场景与选型建议

aaron-dev/xhprof-webman 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 502 次下载、GitHub Stars 达 5, 最近一次更新时间为 2023 年 10 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「performance」 「profiling」 「xhprof」 「laravel」 「thinkphp」 「hyperf」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 aaron-dev/xhprof-webman 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 aaron-dev/xhprof-webman 我们能提供哪些服务?
定制开发 / 二次开发

基于 aaron-dev/xhprof-webman 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 502
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 32
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 0
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-27