Profiling your Application - A quick introduction
Profiling your application
Profiling your application can be difficult to do. You can either do it by guesswork (not a very efficient way of doing it), or by using some tools to help you. Sometimes you'll be surprised at where a bottleneck occurs, which is why guessing doesn't work - you could be looking in the completely wrong spot!
One of the best tools when working with a PHP application is XDebug. I'm assuming you have root access to your server and you are working on a development machine - don't try this on in a production environment ;)
Installing it is quite easy, the instructions on their website are pretty straight forward and can be foundhere.
Once it's installed, you need to enable it in your php.ini file. To do that, open it up and jump to the bottom. Add the following line:
zend_extension=/path/to/xdebug.so
Where /path/to/xdebug.so is where it was installed.
If you are doing this for web applications, you'll need to restart your webserver. You'll also need to put the following in a .htaccess file so profiling is enabled for your app:
xdebug.profiler_enable=1
xdebug.profiler_output_dir=/tmp
xdebug.profiler_output_name=cachegrind.out.%t.%p
For command line scripts, place those directives in the php.ini file (after loading the zend_extension), they can't be done on the fly.
Once you have that going (check your web server logs for errors if necessary), you can fire up your application.
After you view a page, you'll see a file in the /tmp folder called 'cachegrind.out.timestamp.pid', eg 'cachegrind.out.1211853650.6921'. The timestamp & pid will change per page view, so if you're on a busy or shared development server, change the profile_output_dir to another writable folder (eg your application has a writable cache/ folder).
Now you have a profile script, how do you use it?
The best tool to do that is KCacheGrind, however that is for Linux based systems only. If you're on a Windows desktop, you can use WinCacheGrind - however it hasn't been updated in a while and doesn't show as much information as KCacheGrind.
Here's a screenshot of part of the profile of running an Email Marketer cron job (which sent an email to 50,000 subscribers while the app was in "test mode"). The graph tells us that most of the time is spent in the SS_Email_API->Send method. Inside that, most of the time is spent in _ReplaceCustomFields (which in turn shows most of the time is spent doing the replacements).
Using this method of profiling, we were able to find a problem with sending an email campaign with lots of trackable links. We significantly improved the way that link tracking occurred to enhance sending speed and still provide the same functionality.

