螺旋 / 分析器
Spiral Xhprof 分析器
v3.2.0
2023-09-26 05:16 UTC
Requires
- php: >=8.1
- psr/http-server-middleware: ^1.0
- spiral-packages/profiler: ^1.0
- spiral/boot: ^3.0
- spiral/core: ^3.0
- spiral/debug: ^3.0
- spiral/hmvc: ^3.0
- spiral/logger: ^3.0
Requires (Dev)
- nyholm/psr7: ^1.8
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^5.1
README
要求
请确保您的服务器已配置以下PHP版本和扩展
- PHP 8.1+
- Spiral 框架 3.0+
安装
要安装此包
composer require spiral/profiler
安装包后,您需要将包中的引导程序添加到您的应用程序中。
use Spiral\RoadRunnerBridge\Bootloader as RoadRunnerBridge; protected const LOAD = [ // ... Spiral\Profiler\ProfilerBootloader::class, // ... ];
定义环境变量
PROFILER_ENABLE=true PROFILER_ENDPOINT=http://127.0.0.1:8080/api/profiler/store PROFILER_APP_NAME="My super app" PROFILER_MIDDLEWARE_DEFAULT_ENABLED=true
使用方法
使用分析器有两种方式
- 作为中间件使用分析器
- 作为拦截器使用分析器
作为拦截器使用分析器
如果您想分析应用程序的特定部分,并且支持使用拦截器,拦截器将很有用。
- 控制器,
- GRPC,
- 队列任务,
- TC,
- 事件。
namespace App\Bootloader; use App\Interceptor\CustomInterceptor; use Spiral\Bootloader\DomainBootloader; use Spiral\Core\CoreInterface; class AppBootloader extends DomainBootloader { protected const SINGLETONS = [ CoreInterface::class => [self::class, 'domainCore'] ]; protected const INTERCEPTORS = [ \Spiral\Profiler\ProfilerInterceptor::class ]; }
有关拦截器的更多信息,请参阅此处。
作为中间件使用分析器
要将分析器作为中间件使用,您需要将其添加到您的路由器中。
全局中间件
namespace App\Bootloader; use Spiral\Bootloader\Http\RoutesBootloader as BaseRoutesBootloader; use Spiral\Profiler\ProfilerMiddleware; final class RoutesBootloader extends BaseRoutesBootloader { protected function globalMiddleware(): array { return [ ProfilerMiddleware::class, // <================ LocaleSelector::class, ErrorHandlerMiddleware::class, JsonPayloadMiddleware::class, HttpCollector::class, ]; } // ... }
路由组中间件
namespace App\Bootloader; use Spiral\Bootloader\Http\RoutesBootloader as BaseRoutesBootloader; use Spiral\Profiler\ProfilerMiddleware; final class RoutesBootloader extends BaseRoutesBootloader { // ... protected function middlewareGroups(): array { return [ 'web' => [ CookiesMiddleware::class, SessionMiddleware::class, CsrfMiddleware::class, ], 'profiler' => [ // <================ ProfilerMiddleware::class, 'middleware:web', ], ]; } // ... }
路由中间件
class HomeController implements SingletonInterface { #[Route(route: '/', name: 'index.page', methods: ['GET'], middleware: 'profiler')] public function index(...): void { // ... } #[Route(route: '/', name: 'index.page', methods: ['GET'], middleware: \Spiral\Profiler\ProfilerMiddleware::class)] public function index(...): void { // ... } }
分析策略。
默认情况下,中间件将在每个请求上启动分析。您可以将分析配置为仅针对某些请求启用。
- 将环境变量PROFILER_MIDDLEWARE_DEFAULT_ENABLED设置为false。
PROFILER_MIDDLEWARE_DEFAULT_ENABLED=false
- 传递Http头
X-Spiral-Profiler-Enable=1
以分析您想要的请求。