2

i really didnt know how to word the title so i did the best i could.

we have an issue where we calculate final script (web based) execution time.

we do a simple

if(time()-$_SERVER['REQUEST_TIME']>X){
    logMe();
}

we find that sometimes the script is shown to take longer then X seconds to execute.

we are 100% sure this is not an issue with mysql, memcached, sphinx or any of the other usual culprits. please... just assume that its not 'something on our end' holding it up.

we even added this simple exec time check way way up in the script, before any heavy processing is done and there are still some hits. mainly far far away overseas clients.

so i am thinking this must be related to the output buffer somehow.

so the question is as follows:

in php + apache, how does the output buffer work? lets say you have the following:

[ 10KB HTML Body Head ]

[ Mysql query #1 ]

[ 50KB HTML Body ]

[ Mysql query #2 ]

[ 20KB HTML Body Footer ]

in this example, imagine you have a client connection that maxes out at 2KB/s.

so in the most ideal situation, it would take him 5 seconds to recieve the "HTML Body Head".

does that mean it will take 5 seconds before "Mysql query #1" is executed?

i think you get the idea. does a slow client connection affect how long it takes for a script to be processed.

further more: do any php.ini settings affect this, and does flush()/ob_flush()?

thanks.

hakre
  • 193,403
  • 52
  • 435
  • 836
anonymous-one
  • 14,454
  • 18
  • 60
  • 84
  • PHP will always run as fast as it can and it will not wait for the client to receive the response. – hakre Jul 07 '11 at 19:28
  • Why use $_SERVER. Using a $time= time() call at the start of the script is way more reflective of the true start of execution time. Rather than the request time since the request time is not necessarily the time when the script starts execution. – frostymarvelous Jul 07 '11 at 19:42
  • did not know that the $_SERVER['REQUEST_TIME'] variable was set by apache :/ all of the sudden this is making a lot of sense ;) was always under the assumption that REQUEST_TIME was populated the moment apache handed over the request to php. – anonymous-one Jul 07 '11 at 19:53

2 Answers2

3

PHP will execute at maximum possible speed, and if output has to be buffered, the script will NOT pause until the buffer empties. It'll just keep filling up the buffer until the script ends.

How big is your 'X' value? $_SERVER['REQUEST_TIME'] comes from Apache itself, and is when the hit came in. There may be a pause in Apache to wait for a child process to become available to service the request. There can be pauses in PHP while waiting for a resource to open up (e.g. no more free database handles, so wait for one to free up). Then there can be delays for the database itself to execute and retrieve data off disk, etc...

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • X is between 5-15 seconds..... the site is fairly busy tho. so the issue u mention is quite possible. we will log the start time in a separate var rather then relying to $_SERVER['REQUEST_TIME'] to get a better idea. – anonymous-one Jul 07 '11 at 19:36
  • thank you kindly btw. this has been bugging me for about... oh... 1.5 years ;) – anonymous-one Jul 07 '11 at 19:56
0

PHP is serverside so the script execution time doesn't depend on the clients connection.

PHP usually ouputs once the script(s) has finished.

Flush can be used for long running script to output while this script hasn't finished yet.

Still the client have to download the page.

Don´t think there is a lot you can do when you have clients with 2KB/s other than upgrading their connection :P

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • the 2kb/s was just an example to make the question fairly easy to understand. so what you are saying is that regardless of how fast (or slow) the client connection is, a time()-$SERVER['REQUEST_TIME'] at the end of the script will give a true script execution time? thx. – anonymous-one Jul 07 '11 at 19:33
  • 1
    @anon1: yup that's what I was saying. The webserver gets the request and it just processes the request (without the need of waiting on the client) and outputs when ready to the client – PeeHaa Jul 07 '11 at 19:36