5

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.

rap-2-h
  • 30,204
  • 37
  • 167
  • 263
  • That's maybe because the first call to a Guzzle method loads and initializes many of Guzzle's classes, where as the second one is fast because everything has already been loaded. What if you measure only the second async call directly without calling the synchronous methods first ? –  Jan 16 '15 at 21:10
  • Are you using Guzzle to call an API on your server, or from your server? Also, have you tried setting `"debug" => false` in app.config and then running `php artisan optimize`? When having debug mode set, a lot of unnecessary files are loaded. – Marcus Olsson Feb 24 '15 at 18:40

1 Answers1

0

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.

Danny Kopping
  • 4,862
  • 2
  • 29
  • 38