How do I profile Guzzle 6 requests?

可紊 提交于 2019-12-10 13:37:22

问题


I'm trying to profile the requests made to an API server from a PHP client using Guzzle (v 6).

In the Guzzle 5.3 there is this complete and before event handling.

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();
    }
}

But how do I do this in v6?


回答1:


Just found it using Middleware. Here's the code.

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;
                });
            };
        };
    }
}

And then attach the profiler like this.

$stack = \GuzzleHttp\HandlerStack::create();
$stack->push(Profiler::profile());
$client = new \GuzzleHttp\Client([
   'handler' => $stack
]);


来源:https://stackoverflow.com/questions/31869708/how-do-i-profile-guzzle-6-requests

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!