Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ob_flush takes long time to be executed

In my website(running with drupal) the ob_flush function takes a long time(between 10 - 100 secs) to be executed. How do I find out why? What can cause this so long time? enter image description here

like image 556
user16948 Avatar asked Dec 16 '12 17:12

user16948


2 Answers

Try this:

ob_start();
//Your code to generate the output
$result = ob_get_contents(); //save the contents of output buffer to a string
ob_end_clean();
echo $result;

It is run quick for me.

like image 167
Rubén Carrasco Avatar answered Dec 05 '22 18:12

Rubén Carrasco


[You may want to tag your question with Drupal, since this feels like it might be a Drupal issue. Specifically, I suspect that when you flush the buffer, you're writing to an outer buffer, which triggers a ton of hooks to be called to filter the data you've just written.]

I suspect that your problem is nested buffers. Drupal really likes buffers and buffers everything all over the place. Check the result of:

echo "<pre>\nBuffering level: ";
    . ob_get_level() .
    . "\nBuffer status:\n"
    . var_dump(ob_get_status(TRUE))
    . "\n</pre>";

If you've got nested buffers, then I suspect ob_flush() will do nothing for you: it just appends the contents of your inner buffer into the next outermost layer of buffering.

Nested buffers can come from Drupal itself (which the above will show), or from the settings for zlib-output-compression and output_buffering (try twiddling those, see if it changes anything).

If your buffers are not nested, and the above settings do not help, then you may also want to split the operation into pieces, and run the profiler there, to see which part is taking the time:

$data = ob_get_contents(); // Return the contents of the output buffer.
ob_clean(); // Clean (erase) the output buffer.
ob_end(); // Close the buffer.
echo($data); // Output our data - only works if there's no outer buffer!
ob_start(); // Start our buffer again.

The question then becomes, though, "what are you trying to accomplish?" What do you think ob_flush() is doing here? Because if the answer is "I want to push everything I've done so far to the browser"... then I'm afraid that ob_flush() just isn't the right way.

like image 20
Dewi Morgan Avatar answered Dec 05 '22 19:12

Dewi Morgan