jeudi 20 octobre 2022

Laravel instantiating $response takes abnormally long

I have a Laravel 5.8 application, with very long loading times. Laravel’s speed is debugable within the public/index.php file. Which I have tried like this:

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

dd(microtime(true) - LARAVEL_START); 

Between LARAVEL_START being set, and the response being sent, it will take a staggering six to fourteen seconds to load. Switching my code to:

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

dd(microtime(true) - LARAVEL_START);    

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

Will yield an average loading time of about 0.6 to 2 seconds between Laravel booting and $kernel being set. This means that loading the kernel takes an average of 1.2 seconds, and processing the request takes about 11-13 seconds. Now, what I have tried to speed up the fetching of a page so far:

Dumping cache

Most Laravel problems are usually solved by clearing the cache, running php artisan optimize, dumping composer autoload, etc. This didn't yield any time difference.

Reinstalling the project

I tried to reinstall the project from it's GitHub repository, which also had no impact on the loading times. I rebuild the docker container from scratch as well, more about docker in the next paragraph

Docker

As I have been doing a lot of research into why my Laravel application could be slow, I stumbled upon many articles stating that docker could slow down Laravel substantially. To combat this, I’ve tried the following:

PHP artisan serve, instead of using docker, I’ve tried to use the built in webserver. This does not speed up the application.

  • I switched between a few PHP 7.X versions, to no avail.
  • My other projects work fine with the artisan serve command. Loading times are somewhere around 20-200 milliseconds.

Xampp, does not speed up the executable time.

  • My other projects work fine with xampp. Loading times are around 20-300 milliseconds.

Controller debugging

As I have eliminated the possibility of my webserver being the bottleneck, I timed the difference between the first and last line of my controller like this:

public function index() 
{
    $time_start = microtime(true)
    // controller code (about 40 lines)
    dd($time_end - $time_start)
}

This yields the following results:

0.31205701828003
0.24561214447021
0.23838305473328
0.12146878242493
0.25613188743591
0.30584192276001

While these seem like respectable times, my application didn’t take the average time of 0.22 seconds to boot, but rather took the expected eight to twenty seconds to boot. I tried it once again, now with a fully empty controller like this:

public function index() 
{
    $time_start = microtime(true)
    dd($time_end - $time_start)
}

Which yields an average controller execute time of 0.0 seconds. While remaining as slow as before.

Middleware

My next thought would be to debug the middlewares my project uses. As it is an enterprise application, there is a lot of validation going on under the hood, which is run before every request gets to a controller. I started off by completely removing all middlewares from the routes/web.php file. Rerunning my application does not yield any time difference. With no middleware being run, I can conclude that my slow loading times aren't due to a unoptimized middleware.

Service providers

Just like the middleware, service providers run on every request. Removing all non-essential service providers in config/app.php yields no loading time difference.

Where to go from here?

My routes/web.php now looks like:

<?php

use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
 */

Route::get('/greeting', function () {
    return 'Hello World';
});

It still takes 12 seconds to load. It seems as though something is slowing down:

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

Yet, removing practically the entire project has close to zero effect on the projects speed. How can I speed up this essential project?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire