What's happening in our world

Blog Post
Benchmarking Your PHP Code Performance
When it comes to getting the most out of your PHP application, there's only so far that raw talent can take you. Most developers can benefit from a little external help when it comes to code optimisation, and even the best of the best can use a bit of help from time to time. The best way to ensure your code is executing as efficiently as possible in terms of resource usage is, naturally, by measuring the efficacy of each individual element and determining where any bottlenecks may exist.
 
The benchmarking of individual elements is also known as profiling, and there are a number of tools around that can help you with the process, but today we're going to look at one of the most popular tools, called XHGui. XHGui is a fork of the XHProf tool that was originally created by Facebook, but it comes along with a much-improved GUI (as the name would suggest) as well as a number of other improvements. One of the most important improvements on XHProf are the changes in the way the final output is presented, making it handy for creating pretty deliverables for managers who are not - and have never been - developers.
 
Since you're doubtless comfortable working with Apache, we'll skip the details of how to install XHGui and focus on what it can do for you once you've got it up and running. Once you've populated it with some data (try comparing heavy site loads with more typical usage patterns), you'll see the breakdown of data for each request: the URL, timestamp, 'wall time' which represents the user's wait time, CPU time used, total memory used, and peak memory usage. You can then drill down even deeper into each request, which gives you more data than you are likely to ever need about each step in your program.
 
To start your benchmarking, put your application through its paces: run each of the functions you've coded, including all error trapping. Once you've built your initial dataset, you can begin to analyse the problem areas and begin to consider optimisation strategies. Often, the culprits behind slow execution times will be elements of the PHP framework you've chosen to develop in, but any particularly extreme offenders can often be replaced by custom code that does the specific job more effectively - this is just one of the inevitable trade-offs that come with developing using frameworks, and each will have their own specific quirks that need to be considered and corrected for.
 
If nothing immediately stands out as being the main bottleneck, look for related code elements that interact to create slowdowns and consider recoding them as a more cohesive unit. You can also reconsider any elements that may be better off handled asynchronously, or even reconsider their inclusion at all if they're non-essential. Ultimately, however, the main consideration has to be a balance between user experience, functionality, and execution time, and XHGui is your best friend when it comes to determining how to align that balance.
Sign up to receive updates from SourceGuardian
Try our free php source code demo
TRY SOURCEGUARDIAN FREE FOR 14 DAYS
Account Login:

login Forgotten Password?
Connect with us
Bookmark
facebook linkedin twitter rss
© Copyright 2002 - 2024 SourceGuardian Limited
Privacy Policy l Terms & Conditions l Company Info l Contact us l Sitemap l PHP Weekly News