How do we track request time with Guzzle and Guzzle Bundle?

Vlad Gregurco
2 min readAug 8, 2018

--

With confidence I can say that requests are not the fastest part of any application and for “unknown” reasons, one day you will want to check the time it takes and it will spoil your day.

Let’s create a guzzle client, that will be used, to perform request:

use GuzzleHttp\Client;
use
GuzzleHttp\TransferStats;

$client = new Client();

And just add on_stats option to the request:

$client->request('GET', 'http://httpbin.org', [
'on_stats' => function (TransferStats $stats) {
// Estimated time the request was being transferred
// $stats->getTransferTime();
}
]);

Your callback will be called by guzzle and useful information will be passed through TransferStats object.

Things become even easier if you’re using Symfony:

Step 1: Install GuzzleBundle

composer require eightpoints/guzzle-bundle

Step 2: Configure client

eight_points_guzzle:
clients:
httpbin:
base_url: "http://httpbin.org"

Step 3: Do a request

$container->get('eight_points_guzzle.client.httpbin')->get('/204');

Step 4: Track request time in Profiler

Guzzle Bundle tracks the time of all requests and shows all the information in a convenient form.

Read more here: https://github.com/8p/EightPointsGuzzleBundle

--

--