如何对 Guzzle 6 的请求进行性能分析?

7

我正在尝试使用Guzzle(v6)从PHP客户端对API服务器发出的请求进行分析。

在Guzzle 5.3中有completebefore事件处理功能。

class GuzzleProfiler implements SubscriberInterface
{
    public function getEvents()
    {
        return [
            'before'   => ['onBefore'],
            'complete' => ['onComplete']
        ];
    }

    public function onBefore(BeforeEvent $event, $name)
    {
         start_profiling();
    }

    public function onComplete(CompleteEvent $event, $name)
    {
         end_profiling();
    }
}

但是在v6中我该怎么做呢?

1
你可以安装 https://github.com/e-moe/guzzle6-bundle ,将Guzzle 6的调用集成在分析器中。 - Manolo
1个回答

3

我刚使用中间件找到了它。以下是代码。

class Profiler {

    /**
     * @return callable
     */
    public static function profile() {
        return function(callable $handler) {
            return function(\Psr\Http\Message\RequestInterface $request, array $options) use ($handler) {
                start_profiling();
                return $handler($request, $options)->then(function(\Psr\Http\Message\ResponseInterface $response) use ($token) {
                    end_profiling();
                    return $response;
                });
            };
        };
    }
}

然后像这样附加分析器。
$stack = \GuzzleHttp\HandlerStack::create();
$stack->push(Profiler::profile());
$client = new \GuzzleHttp\Client([
   'handler' => $stack
]);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接