Optimizing your PHP Scripts with Xdebug’s Profiler

Often times, website owners will find that their site running slow or their host will inform them they are using too many resources, tracking down the root cause can be a tough task.  While many factors could be in play, chances are it is a poorly written script. This can be a difficult thing to track down and too often people start looking at other factors such as the hosting, their ISP or other related technologies.

Creating a great PHP application requires consideration of a lot of factors and one of such factors is speed. A good application you needs to be fast, because no one likes to wait for slow sites and if your application doesn’t load fast enough you will certainly lose a number of visitors.

When this happens the best place to start your troubleshooting is first to look at the PHP scripts and the speed of the database queries.  This is where Xdebug’s Profiler tool comes in handy. Using it you can precisely determine the load times of all the elements in your script and generate an easy to interpret report.

The following article has been written by corePHP.com a professional web development firm and in it you’ll be introduced to the basics of a very powerful tool designed to find slow PHP scripts and resolve the issues.

Getting things up and running

After you have downloaded and installed the Xdebug extension for PHP, you can turn on the profiler to collect information about your scripts and store the log files to your desired folder by the following steps:
• Open up your php.ini file on your server.
• Set the value of xdebug.profiler_enable field to 1.
• Set your desired folder for storing the reports by setting the output folder in the field xdebug.profiler_output_dir.
After you have set these fields, you are all good to go and the extension should start collecting information for your scripts.

Analyzing the data

Once you have collected some data and the logs are ready in your specified output directory, you can open them and analyze the data using KCacheGrind (for Linux users) or WinCacheGrind (for Windows users).

 

KCacheGrind

 

 

 

 

 

 

 

 

 

 

Once you open up any log file you can easily view all the functions contained in your script, how many times they are called, and how much time they are taking for execution.

KC UI

Image Source: xdebug.org

The Flat Profile shows up all the functions present in the script and the time taken by each of the function to complete its task.

The “Self” column shows how much time a single function needs and the “Called” column shows how many times a function has been called in the process, while the “Function” column shows the details of the function.

This data will help you to study your scripts, make changes accordingly and speed up your script to resolve the issues that are causing your site to run slowly or consume too many resources.

Summary

In this article you were introduced to the basics of Xdebug and how it can help you with identifying scripts that are poorly coded or are taking too much resource.

###

‘corePHP’ is a developer of Joomla! websites and high quality extensions. They are available to assist you in troubleshooting PHP script issues that may be plaguing your site.

728x90_gif