Tricks to Speed Up Laravel With xDebug
I was curios if I would be able to speed up my Laravel application. What better to do than to fire up xDebug and take a peak behind the curtain. After I share my findings I will explain how to setup xDebug if you are using Mac (with Ubuntu VM, in my case Docker).
There are several things I found out.
Watch out for Laravel routes
If you have more than 300 routes you will start to see a difference if your /home route (the one you are testing against right now) is not among the first ones because it will need to iterate through. I have around 400 routes and when I removed all of them and only left /home in there, I saw a dramatic increase in speed.
How to fix it
I am running L5.5 and it is crucial to run php artisan route:cache
in order to have it speed up.
Watch out for Composer autoloader
I also noticed that composer is taking more time to find the library since internally it has to check if the file_exists() in order to load it.
How to fix it
Make sure to run composer dump-autoload -a
in order to have the composer autoloader optimized.
How to install xDebug
In case your server doesn’t have xDebug installed, you can run:
sudo apt-get install -y php-xdebug
Then all you need to do is to add a few lines to your php.ini
file which you can locate with your phpinfo()
function.
Since I am running my application on nginx
my php.ini
file is in /etc/php/7.2/fpm/php.ini
.
In there, make sure to add
xdebug.profiler_output_dir = /var/www/html/cachegrind
xdebug.profiler_enable = 1
xdebug.profiler_enable_trigger = 1
The first line is where your application is located. You need to create this cachegrind
folder so xDebug can store the files.
Install profiler
We will use qcachegrind
with graphviz
support so you get cool GUI with it.
brew install qcachegrind --with-graphviz
Once you start your server with these new php.ini
additions, you can use the following commands to enable or disable xDebug on the server:
sudo phpdismod xdebug # Disable xDebugsudo phpenmod xdebug # Enable xDebug
Now you can run your application myapp.dev?XDEBUG_SESSION_START=1
.
You will notice that it takes more time than usual to process your app. This is due to xDebug working it way to log all the calls. Once it is done, check your cachegrind
folder where you will see a file.
Now, open your local Mac terminal and type qcachegrind
. It will open a new tool where you can import the new file so that would look something like this:
Reminder
Don’t forget to disable xDebug when you are not debugging since it will be slower than usual.
Happy coding!
Follow me on Twitter
Add me on LinkedIn
Personal adnansabanovic.com