PHP Performance Profiling
PHP has undergone incredible growth in the last couple of years and is now being used for a huge range of tasks ranging from tiny scripts to large-scale web applications. Some web applications have hundreds of thousands of lines of PHP code, and the fact that PHP can scale to these sorts of levels is a great testament to its design and the efficient Zend Engine that actually manages PHP code execution. Of course, bigger and more complex projects result in more load on your servers, and when you throw a database into the mix you have even more potential performance bottlenecks to keep track of. A typical scenario is that you've added a few new features to a web application, and now you're seeing more server load and memory usage, and pages seem to load slower. What can you do? Maybe you can afford to throw bigger hardware at the problem, but even if that's a viable option you should also find the parts of your code that are causing slowdowns and optimize them. In this paper I outline the use of performance profiling to run PHP code in a controlled environment, and show how to generate a report giving statistics such as time spent within each function, how long each database query takes, and how much memory has been used. Examples and demonstrations are based on APD, the Advanced PHP Debugger, for which I am the Debian package maintainer. I demonstrate installation of APD, show how to integrate a profiling trace into a live site without compromising performance for normal users or being overwhelmed by debugging output, process the trace output using the pprofp tool, and explain how to interpret the pprofp output. If time permits, hints are then given on approaches to streamlining code.
Jonathan Oxer is Founder and Technical Director of Internet Vision Technologies, an Australian web application development company with clients around the world. He is also a Debian developer, and organiser of Debian Miniconf2 in Perth in January 2003 and Debian Miniconf3 in Adelaide in January 2004. He has presented papers at a number of conferences including the last 2 Debian miniconfs and at Linux Conf Australia 2004.
Jonathan has written for a number of magazines and newspapers including Linux Journal, The Age and Sydney Morning Herald. He is also a columnist for Australian Linux magazine Linmagau.
His first book, 'How To Hire A Web Developer And Stay Sane', is soon to be published, while his second book, 'The Debian Universe' is being written live online at www.debianuniverse.com, and his third and fourth books ('Web Applications And Business Intelligence' and 'Disaster Proofing For Small Networks') are already underway. He can be reached at jon@oxer.com.au.