I have a script that builds my webpage in one string ($content) and then echo it to the user.
My script looks like this:
$time1= microtime(true);
$content = create_content();
$content_time=(microtime(true)-$time1)
$time = microtime(true);
echo $content;
$echo_time = (microtime(true)-$time);
Now $content_time is always well under 0.5s so thats no problem. However a few times a day the $echo_time is well above one second and can even go up to 15 seconds. The content isn’t really big, about 10-20kb and the times at which this happens are completly random, so it’s not on the busy times and even happen in the middle of the night.
Anybody have any idea what that can be?
EDIT
The site is hosted on a (remote) dedicated server and only host this site. There is a database involved but like I say the $content_time is well under 1 second, so what this function does can not be the delay.
When the time of my site is above a certain value (lets say 5s) I log this. Even Googlebots seems to have these issues sometimes so I don’t think they use a dial-up connection 🙂
Let’s narrow the issue down and factor out some things…
In the question you indicate you’re echoing out 10-15kb. That’s a significant amount no matter how it’s buffered to output–remember php is single thread, once you flush your buffer you got to wait for all the output to happen via the shell or HTTP before the script continues. It will eventually have to flush the internal buffer before continuing the echo. To get good time without the flushing overhead of echo
Try replacing the
With
This will echo to a buffer, but not actually spit it out via HTTP or whatever. That should give you the ‘real’ time of the echo command without any concern sending out what’s in the buffer.
If echo_time shrinks down, you have a transport issue to address as best you can with buffering.
If echo_time is still to large, you’ll need to start digging into the PHP C code.
Either way you’re a lot closer to finding your issue and a solution