Performance always matters. Your Laravel application might be beautifully designed and written, but there's always the possibility of hidden bottlenecks. Is it unnecessarily slow? Can it be optimized further? Sometimes, a single function can consume most of your CPU time. In this tutorial, you'll learn how to profile your Laravel application in Visual Studio Code to uncover and address these issues.
If you're looking for general PHP profiling guidance, check out these resources:
Simply put, profiling tracks all function calls in your application, recording their start and end times. This information allows you to:
With this data, you can pinpoint performance bottlenecks and optimize your application effectively.
Before starting, ensure you have the following:
Xdebug
extension installed.zlib
extension enabled (recommended).To confirm PHP and Xdebug are correctly configured, open any PHP file in Visual Studio Code and check the OUTPUT panel for a confirmation message:
Open Laravel Application
Open folder with the Laravel application. You should see the directory structure like this:
Create a launch.json
Profile
If you don’t already have a launch.json
file, follow these steps:
Run and Debug
panel.create a launch.json file
and choose the PHP (Xdebug)
option.Add the Profiling Configuration
Ensure your launch.json
file includes the following profile:
"configurations": [
{
"name": "Launch built-in server and Profile",
"type": "php",
"request": "launch",
"noDebug": true,
"runtimeArgs": [
"-S",
"localhost:8000",
"-t",
".",
"../vendor/laravel/framework/src/Illuminate/Foundation/resources/server.php"
],
"cwd": "${workspaceRoot}/public",
"serverReadyAction": {
"action": "openExternally"
},
"envFile": "../.env",
"profile": true,
"openProfile": true
}
]
Select the Profiling Configuration
In the Run and Debug
panel, choose the "Launch built-in server and Profile" option:
Start Debugging
Press F5 to start the profiling session. This will:
You should see a smilar output in VS Code's DEBUG CONSOLE
:
Make Web Requests
Navigate through the pages you wish to profile. Each request generates a separate profiling file, which will open automatically when the profiling ends.
Stop the Profiling Session
Return to Visual Studio Code and click the Stop button to end the session. Note, it may take a few seconds.
View Profiling Results
The profiling output will appear in new document tabs:
Each profiling file corresponds to a single request. Here's how to interpret the results:
Aggregated Time
Sort by Time (ms) to identify the "heaviest" functions.
Call Counts
Sort by Calls to find the most frequently called functions.
Highlight Hot Paths
Use the Highlight in Source Code option to visualize performance hotspots directly in your code: