Search code examples
phpcurllaravelguzzlelaravel-forge

Guzzle slow on laravel forge and homestead


I don't understand why guzzle requests are really slow on laravel forge and laravel homestead. I did not change default server configuration on forge and homestead.

Every simple request like this one ...

$client = new GuzzleHttp\Client();
$response = $client->get('path-to-my-api');

... takes about 150ms (on homestead and forge). This appends on every request (same network or internet). I read some posts about guzzle and it seems to be very fast for every users, but not for me.

Versions :

  • curl 7.35.0 (x86_64-pc-linux-gnu) libcurl/7.35.0 OpenSSL/1.0.1f zlib/1.2.8 libidn/1.28 librtmp/2.3
  • PHP Version 5.6.0
  • Guzzle 5.1.0

Something really weird is that when I do this (asynchronous) ...

$req = $client->createRequest('GET', 'path-to-my-api', ['future' => true]);

$client->send($req)->then(function ($response) {
});

... it takes about 10ms. It's great but I dont understand why. And I don't want to perform asynchronous requests.

Maybe my time measure is buggy, but I think it's Ok : I use PHP debug bar like this :

// .....

// synch
Debugbar::startMeasure('synch','SYNCH Request');
$response = $client->get('path-to-my-api');
Debugbar::stopMeasure('synch');

// asynch
Debugbar::startMeasure('asynch','ASYNCH Request');
$req = $client->createRequest('GET', 'path-to-my-api', ['future' => true]);

$client->send($req)->then(function ($response) {
    Debugbar::stopMeasure('asynch');
});

I know it's not easy to answer this question (because it's vague), but I have no clue for now :(. I can edit it if you want. Thanks a lot.


Solution

  • Guzzle cannot be slow - it's just a library. Your synchronous requests are probably taking longer because your API is taking long to respond, and your asynchronous requests seem to be faster because it's not blocking the network until it receives a response.

    Try calling the API directly in your browser or using cURL in your terminal - you'll probably find the latency is there.