DEV Community

Paulund
Paulund

Posted on • Originally published at paulund.co.uk

Performance Metrics Using Guzzle

In this article we're going to investigate how you can track performance metrics using Guzzle. Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services.

Introduction

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. It provides a simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc.

Installation

To install Guzzle, you can use Composer. Run the following command to install Guzzle:

composer require guzzlehttp/guzzle
Enter fullscreen mode Exit fullscreen mode

Usage

To send a request using Guzzle, you need to create a new Client instance and then use the request method to send
the request. Here's an example:

use GuzzleHttp\Client;

$client = new Client();
$response = $client->request('GET', 'https://example.com/posts/1');

echo $response->getBody();
Enter fullscreen mode Exit fullscreen mode

In this example, we're sending a GET request to https://example.com/posts/1 and then printing the response body.

Performance Metrics

Guzzle provides a way to track performance metrics for each request. You can use the on_stats options to get detailed
information about the request. Here's an example:

use GuzzleHttp\Client;

$client = new Client([
    'on_stats' => function (\GuzzleHttp\TransferStats $stats) {
        echo $stats->getEffectiveUri() . "\n";
        echo $stats->getTransferTime() . "\n";
        echo $stats->getHandlerStats()['total_time'] . "\n";
    },
]);

$response = $client->request('GET', 'https://example.com/posts/1');

echo $response->getBody();
Enter fullscreen mode Exit fullscreen mode

In this example, we're using the on_stats option to get detailed information about the request. We're printing the
effective URI, transfer time, and total time for the request.

Inside the on_stats callback, you can access the following methods to get insight into the request timings.

  • getEffectiveUri(): Returns the effective URI of the request.
  • getTransferTime(): Returns the total time in seconds it took to complete the transfer.
  • getHandlerStats(): Returns an array of statistics from the handler.

Inside the getHandlerStats() method, you can access the following

  • total_time: The total time in seconds it took to complete the transfer.
  • namelookup_time: The time in seconds it took from the start until the name resolving was completed.
  • connect_time: The time in seconds it took from the start until the TCP connect to the remote host was completed.
  • appconnect_time: The time in seconds it took from the start until the SSL handshake was completed.
  • starttransfer_time: The time in seconds it took from the start until the first byte was just about to be transferred.

This is how the app UpChecker tracks performance metrics for each request using Guzzle. You'll get a detailed insight into the request timings and can use this information to optimize your application.

Top comments (0)