How do I profile Guzzle 6 requests?
Asked Answered
H

1

7

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?

Humming answered 7/8, 2015 at 4:28 Comment(1)
You can install github.com/e-moe/guzzle6-bundle which integrates Guzzle 6 calls in profiler.Bipolar
H
3

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
]);
Humming answered 7/8, 2015 at 5:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.