upscale/swoole-blackfire

Swoole 网络服务器的 Blackfire 性能分析器集成

4.2.0 2023-06-12 20:04 UTC

This package is auto-updated.

Last update: 2024-09-12 22:50:24 UTC


README

此库允许通过 Swoole / Open Swoole 网络服务器使用 Blackfire 对运行的 PHP 应用程序进行性能分析。

功能

  • 透明的请求分析
  • 选择性子系统分析
  • 自定义启动/停止分析调用
  • Blackfire 伴侣 集成

安装

此库应通过 Composer 作为开发依赖项安装

composer require upscale/swoole-blackfire --dev

用法

请求分析

开始分析的最简单方法是激活从开始到结束的所有请求的分析器。这种设计方法是完全透明的,对服务器上运行的应用程序没有任何影响。只需在服务器入口点添加几行代码即可。

为所有请求安装分析工具

$server->on('request', function ($request, $response) {
    $response->header('Content-Type', 'text/plain');
    $response->end(
        'CRC32: ' . hash_file('crc32b', __FILE__) . "\n" .
        'MD5:   ' . md5_file(__FILE__) . "\n" .
        'SHA1:  ' . sha1_file(__FILE__) . "\n"
    );
});

$profiler = new \Upscale\Swoole\Blackfire\Profiler();
$profiler->instrument($server);

选择性分析

可以通过将感兴趣代码包装在分析器调用中来限制分析范围。

将需要分析的代码包装在分析器调用中

$profiler = new \Upscale\Swoole\Blackfire\Profiler();

$server->on('request', function ($request, $response) use ($profiler) {
    $response->header('Content-Type', 'text/plain');

    $profiler->inspect($request, $response, function ($request, $response) {
        $response->write('CRC32: ' . hash_file('crc32b', __FILE__) . "\n");    
    });
    
    $response->write('MD5:   ' . md5_file(__FILE__) . "\n");
    $response->write('SHA1:  ' . sha1_file(__FILE__) . "\n");
});

当前,每个请求只允许一个分析器检查调用。

手动分析

根据应用程序设计和复杂性,可能很难精确地将所需的代码包装在分析器调用中。可以在不同的调用栈级别手动放置分析器开始/停止调用,以进一步缩小检查范围。开发者负责考虑响应填充工作流,确保开始/停止调用的对称性。必须在发送响应体之前停止分析,以便能够在响应头中发送结果。

将需要分析的代码用分析器开始/停止调用包围

$profiler = new \Upscale\Swoole\Blackfire\Profiler();

$server->on('request', function ($request, $response) use ($profiler) {
    $response->header('Content-Type', 'text/plain');
    
    $output = 'CRC32: ' . hash_file('crc32b', __FILE__) . "\n";
    
    $profiler->start($request);
    $output .= 'MD5:   ' . md5_file(__FILE__) . "\n";
    $profiler->stop($request, $response);
    
    $output .= 'SHA1:  ' . sha1_file(__FILE__) . "\n";
    
    $response->end($output);
});

当前,每个请求只允许一对开始/停止调用。

限制

分析在发送响应体之前隐式停止,并将结果添加到响应头中。当前,每个请求只支持一个由 inspect()start/stop() 调用启动的分析会话。

贡献

欢迎提供修复和改进的拉取请求!

许可证

版权所有 © Upscale Software。保留所有权利。

根据 Apache 许可证 2.0 版 许可。