There are several programs that can be used to analyze the output of the XDebug profiler. Some of these are commercial. The ones I will review are open source. The difference between these programs is mostly the way they graphically present the data in the interface, and what part of the profiling data they emphasize. I will cover the two most popular programs in the next two articles, webGrind, and KCacheGrind. These two programs have an entirely different looking interface, and present a different look at data in the profiler data file.
In my last article, I reviewed creating the profile data file with XDebug. Once we have our XDebug cachegrind.out profiler files, we are ready to analyze the data.
I first ran across webGrind when I installed Wampserver as my local server. It comes with Wampserver. At first, I couldn't figure it out, because if you run webGrind in Wamp without any data you get a blank screen. If it is not installed, you can install it from Github.
WebGrind is automatically configured to look for cachegrind files where you told XDebug to put them in the php.ini file.
Here's what webGrind with data looks like.
First, let's get some data on the screen. In the upper grey bar, you'll see a drop down labeled "of," if you click on the drop down arrow, it will show you all your cachegrind files created with XDebug. Pick one. Nothing happened? You have to click the "update" button for the file to load.
Continuing in the grey bar area, the "Show," is a percentage to cut down the amount of functions shown in the window, and the "percent" is actually a drop down to allow you to show the amount of time each function takes to run in either: percent, milliseconds, microseconds. There's also a small check box which allows you to take out of the display the PHP functions, since you can't change the performance of the the PHP functions themselves. Every time you change a drop down, you'll have to click on the "Update" button for that choice to take effect.
Next the white header area, shows the event you are analyzing, and the detailed cachegrind file name, and moving to the right we see a colored graph. Below the graph shows the totals for the entire data file. In this case, "99 different functions called in 36 milliseconds."
The bar graph colors represent the distribution of built-in PHP functions (blue), includes/requires (grey), class methods (green) and procedural functions (orange). The length of the bars is a relative display of the time for each type of process in the file. If you see a large grey area, chances are this particular event may be slow to load, since it has to load a bunch of other files along with the page.
Now, to the meat of the display, the column labeled "Function," shows the different functions, methods, and included/required files that were run with this event.
The Invocation Count is the number of times a particular function has been called.
The Total Self Cost is the total time it took to execute the raw PHP in this function. The time taken to execute your other custom functions is excluded.
The Total Inclusive Cost is the total time, including any other functions called both PHP internal, and or your functions. These three columns can be set to percent, milliseconds, or microseconds depending on the drop down in the upper grey area.
Each of the columns, including the first color coded column can be sorted by clicking the sort buttons in the menu bar, which will bring the functions that take the longest to run to the top of the page.
There's one more column I haven't talked about yet and that is the column to the right of the function name with little horizontal lines. If you click on one of these icons, you'll open a new browser window that will show you the source code for that particular method, along with the file name, so you can locate it in your code. Pretty neat.
As to the actual function listing, if you click on the function name, you'll find sub windows will open and you can drill down into the code to find the performance hog. The stats for each of the sub window functions, along with the link to the source code are displayed in the sub windows. You can keep drilling down on any function that has a little black arrow next to it. Here's a look at the sub windows.
How do you go about finding the bottleneck in your applications? I would take advantage of the sorting capabilities in webGrind. If you click on the first colored column, you'll find that the orange procedural functions come to the top, followed by the grey includes and requires, which you can drill down into, and then the green class methods. The blue PHP functions are at the bottom. Both your custom code, and where the performance problems are, will tend to rise to the top of the display.
I would also sort on the "Invocation Count" column, or the number of times a function is called. Finally the "Total Inclusive Cost" will tell what areas of your code take the most time to run.
There's a wealth of information in webGrind from the profile data files created in XDebug. The way it is set up in Wampserver makes it really easy to get to the information just by clicking on webGrind in the Wampserver menu.
WebGrind is a real nice application, but it is only a subset of the features offered in KCachegrind, which I'll cover in my next post.