Have you ever had a script that ran for a long time and then got stuck somewhere, but you don't know where? Recently a client came to us with a synchronizing script that was taking hours to run. In order to identify the cause of the problem, we put live output within the script to see which parts were getting stuck. We did this by filling up the output buffer; we ended up creating a new function that reported the line number and execution time of the code, so we could determine where the slow parts of the script were and which sections were causing the problem.
Here is the code:
function debug($line) {
echo 'Line: '.$line.'
';
echo 'Time: '.date('h:i:s').'
';
echo str_repeat(' ', ini_get('output_buffering'));
flush();
}
We populated the script file at regular intervals with debug(__LINE__)
to find out why it was taking hours to run. We determined that it was part of a cache rebuilding process that took place every time a loop executed. When attempting to sync thousands of items at a time, the rebuilding of cache can take its toll. So by disabling the caching process when the sync script ran, we were able to substantially reduce the associated time burden and resolve our client's problem.