Php – How to diagnose PHP server execution times to reduce them

amazon ec2network-speedPHP

I am running an API server on Amazon EC2 powered by PHP, and am trying to finetune the performance with a goal of reducing response times. Currently I am testing the performance of the PHP code internally by getting the millisecond time at the start of a request, and then getting the milliseconds at the end of the request just before ending the script. After implementing memcache and a few other tricks I've gotten most requests reporting sub 10-20ms execution time within my PHP code. However, when I look at the network requests in Chrome's Developer Tools I still see requests that hit my PHP scripts showing up as:

Sending 0ms
Waiting 145ms
Receiving 1ms

This compares with requests for static files which are usually more along the lines of:

Sending 0ms
Waiting 15ms
Receiving 1ms

So all I know is that when I run my PHP script and test execution time within the script it only takes 10-20ms to execute my script. At this point I've taken care of the low hanging fruit of making my PHP code run very fast, but I'm trying to figure out how to make the rest of the flow outside the execution of my script faster.

So my question is how to I determine where the extra 120ms comes from since it is outside the flow of my own code? Is it time spent by Apache setting up a PHP process? Is it time spent by the PHP interpreter parsing the PHP files? Is it time spent by the PHP process delivering the response from my script back to Apache? How do I figure out how that 120ms of extra time is distributed with an aim to reducing it?

Best Answer

Well, you have the hard, not-quite-as-hard, and the easy way to do this.

The Hard Way

Use microtime() to manually profile your code to see what is taking the most amount of time. Easy to implement for a smaller project but really gets unwieldy.

The Not-Quite-As-Hard Way

Set up a PHP Debugger to do this for you. Xdebug is probably the best one, but there are many options out there.

It is set up as a PHP extension so that it will automatically profile your code for you and let you know what is taking a large amount of time.

This will most likely still take you a decent amount of time to set up, and it is anything but straight-forward.

The Easy Way

Check out New Relic. You install a PHP extension and set up an account on their server, and it will automatically profile your code for you.

Their pro version, which being fairly expensive, gives you a ton of data to figure out what is taking up time on a PHP script. The free version gives you a basic idea as to what is going on.

I was skeptical at first, but I am really happy with the level of data they provide.

Also, they have a 15-day free Pro trial.

* I am not affiliated with New Relic.

Related Topic