Profiling with XDebug

If you're a software developer chances are you've heard of profiling. Profiling allows you to measure the performance of various sections of your application. Unfortunately, not too many developers bother learning how to get, or once gotten, look at profile information.

Profiling can be a great benefit to developers, especially if your customer starts complaining about how slow the application is running in a particular area. It can help you identify areas of your application that are running slow, and particularly the one or two methods that are killing your application. In the end, knowing how to profile is another tool in your bag of tricks that you can pull out and use to dazzle everyone. There are several excellent tools out there that help in profiling.

Let's get started. Profiling an application, or a web page, is a two step process. The first step is to generate the profile data in a data file, and then, after you generate that data, the second step is too analyze the data with other applications that use the profile data file you created in the first step. In this article, I'll cover creating the profile data file, and in the next two articles, I'll go over two of the better analysis tools that use the profile data file.


The universal PHP program used to gather the profile data file is XDebug. The XDebug profiler is very powerful and generates a ton of data.

The first step in the process is to make sure that XDebug is installed in your PHP environment. You can tell this by bringing up a phpinfo() screen and looking for XDEBUG right after the first block of data in phpinfo.  If it's there, then you have XDEBUG installed properly.


If your PHP installation does not have XDebug, you can download it from the XDebug site.  Make sure your download matches the PHP version you have installed on your system.  You can also set it up by using Github.   Once you have XDebug installed, it is turned on, and controlled from your php.ini file.  If your using Wampserver, XDebug should already be installed on your system, but the profiler will not be turned on.

Let's take a look at the XDebug settings at the bottom of the php.ini file.

Remember when you change a setting, and save it, in the php.ini file, you'll have to restart Apache to reload the php.ini file with your new changes, or your changes will not take effect.

If not installed, XDebug is turned-on by un-commenting, removing the semicolon from, the beginning of the Zend_extension line, and making sure the path is correct to where you have XDebug installed.  By the way, XDebug links to all your IDE editors, and its other function, as its named, is to debug your PHP scripts, so it's good practice to always have XDebug turned on.

The "remote" settings you see in the picture are for debugging a remote site, which I won't cover here.

Now, to the profiler settings, normally the profiler is turned off.  This is because the profiler generates a data file every time you click a button in your application, and if you run it all the time, you'll end up cluttering up your output folder with a confusing amount of continually accumulating data files.  You don't need to run the profiler all the time, and slow down your application with information gathering.  Turn the profiler on when you want to take a snapshot of your application's performance for a particular event, or page load, and then turn it off when you're done.

How do we turn the profiler on and off? To turn on the profiler, you set the profiler_enable line to 1, and it will start generating a data file for every web page you go to, or every request made on a page.  A request, an event, is any button you click, like save the page.

If your thinking, I'll save myself the hassle of reloading Apache every time by using "ini-set(profiler_enable, 1);" to turn it on and off in my code, this does not work with the profiler.

However, you can trigger the profiler on only when you want it by turning off the profiler_enable, to stop continuous profiler data generation, and then setting the profiler_enable_trigger to 1, or on.  This will create a data file just when you type "&XDEBUG_PROFILE" at the end of your current url.  You can also put the XDEBUG_PROFILE" message in a cookie for that page, if you want files generated every time you go to that particular page.

The profiler_output_name is where you tell the profiler how you want to name your files.  I recommend you call the files "cachegrind.out.%R.%t" as shown here, because the cachegrind application that analyzes the file, specifically looks for that file name, and no others, which I'll cover in my upcoming posts.

The "profiler_output_dir" is where you tell XDebug the folder you want to use for the output files.  Make a separate folder just for the output files, that will give you one place to look for files when running your analysis programs.

Here's what your directory will look like after generating some profiler files.

profilerdir629Each file is about 100 KB.   The file names will be long, and you want them that way, because usually there are so many files you want a way to find the particular data you're looking for and the long file names do that.  After the cachegrind.out name the rest of the file tag gives you enough information to know what data your looking at.  The number right after the cachegrind name is a time stamp.  You'll notice the name even identifies the record id in the file name.

Once you have your files generated, you want to look at the data. The second part of the process is using an analysis program to look at the data.  I'll cover that in my next post.

Comments are closed.