Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Since you have executed the request from the local machine - it suggests that neither network nor the script itself is the problem, which leaves the interpreter process. If the PHP interpreter must be loaded for each request, the time per request will be quite high. Determine your PHP handler (e.g. mod_php, fastcgi, etc) and the settings used (and update your question). Also determine your MPM (worker/prefork) and its settings. Running a benchmark (e.g. ApacheBench) might narrow the problem. A setup such as Nginx/PHP-FPM may be more efficient for your purposes. –  cyberx86 Nov 14 '12 at 0:06
add comment

1 Answer 1

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.

share|improve this answer
    
+1 for New Relic suggestion –  HTTP500 Nov 13 '12 at 20:23
    
I'm already using microtime at the very start and very end of my script, and the microtime reports 16ms. However, in the browser it reports 200ms of waiting time. –  Nathan Peck Nov 13 '12 at 22:35
    
To be clear I'm not counting DNS resolving. I just run curl -s -w "%{time_total}\n" -o /dev/null -H 'Host: myvirtualhost' 127.0.0.1 and the response usually shows 150ms while microtime within my script shows 0-1ms. –  Nathan Peck Nov 13 '12 at 22:38
    
As others have stated, if you have eliminated the PHP processing time, then likely it is the setup time. Spawning the php process, reading the file, etc. Personally I like php-fpm running all the time, plus APC or Xcache for file caching. Try that setup and see how it impacts your PHP load times. –  Dave Drager Nov 14 '12 at 14:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.