Using PhpStorm, we can analyze performance of our PHP code by profiling it. Profiling allows us to gather program execution statistics such as the names of functions executed, the number of times a function has been executed, how long a function took to execute, which other functions have been called into, and so on. This information can give us a hint on where our code can be improved.
Let's see how this works.
Requirements
PhpStorm makes use of Zend Debugger to collect profiler information. This debugging engines must be installed and configured on our system. See Zend Debugger Installation Guide for more information.
1. Enabling the Zend Debugger profiler
The Zend Debugger profiler can only be enabled by specific GET/POST or COOKIE parameters. This means it is only able to profile web applications.
In order to specify the GET/POST or COOKIE parameters required on the fly (to enable or disable profiling when needed), use the PhpStorm bookmarklets (or one of the Browser Debugging Extensions) and add the Start profiler and Stop profiler bookmarklets to the web browser.
2. Capturing a profiler snapshot for web applications
To be able to analyze information captured by the profiler, we first have to collect that information. To do this, follow these steps:
- In PhpStorm, listen for incoming debugger connections. Use the Start Listen for PHP Debug Connections button on the toolbar or the Run | Start Listen for PHP Debug Connections menu.
- Open the application in a browser and make use of the Start profiler bookmarklet.
- Refresh the application or navigate to a different script within the application.
- PhpStorm will notify of an Incoming Connection from Zend Debugger. Accept the connection to capture profiler information for the current request.
3. Analyzing a profiler snapshot
When profiling using Zend Debugger, PhpStorm will automatically open the profiler snapshot for the current HTTP request. Let's go over the different tabs.
3.1. Execution Statistics tab
In the Execution Statistics view, we can examine the summary information about execution metrics of every called function. We can see all files, function calls, the number of times they have been called, and the time (absolute and relative) they took to execute.
The top grid shows us different metrics:
- Callable - the file that has been executed
- Time - the total execution time
- Own Time - the amount of time the function spent executing its own code (excluding calls to other functions)
- Calls - the number of calls
The bottom grid has two tabs, Callees (which functions the script calls into) and Callers (from where the script is called). We can see various metrics here as well:
- Callable - the function that has been executed
- Time - the total execution time
- Calls - the number of calls
Functions with high own times and/or large number of calls are definitely worth inspecting.
3.2. Call tree tab
The Call Tree view shows us the execution paths of our code. We can see more details about function execution times and so on.
The top grid shows us call trees (which function calls into which function) and different metrics:
- Callable - the file that has been executed
- Time - the total execution time
- Calls - the number of calls
The bottom grid has two tabs, Callees (which functions are being called) and Callers (from where the function is called). We can see various metrics here as well:
- Callable - the function that has been executed
- Time - the total execution time
- Calls - the number of calls