You possibly also want to end your benchmark after the output is flushed.
<?php
your_benchmark_start_function();
ob_start ();
for ($i = 0; $i < 5000; $i++)
echo str_repeat ("your string blablabla bla bla", (rand() % 4) + 1)."<br>\n";
<----------
echo your_benchmark_end_function(); |
ob_end_flush (); ------------------------
?>
CXIII. Funktionen zur Ausgabesteuerung
Einführung
Mit den Funktionen zur Ausgabesteuerung können Sie die Ausgabe eines Skripts steuern. Dies kann in verschiedenen Situationen ganz nützlich sein, besonders wenn Sie header an den Browser schicken müssen nachdem Ihr Skript bereits etwas ausgegeben hat. Die Ausgabesteuerungsfunktionen gelten nicht für Header, die mit header() oder setcookie() geschickt wurden, sondern nur für Daten ,die mit echo() gesendet wurden oder für Daten zwischen PHP-Codeblöcken.
Anforderungen
Diese Erweiterung benötigt keine externen Bibliotheken.
Installation
Für diese Funktionen ist keine Installation erforderlich, diese gehören zum Grundbestand von PHP.
Laufzeit Konfiguration
Das Verhalten dieser Funktionen wird durch Einstellungen in der php.ini beeinflusst.
Tabelle 195. Output Control Konfigurationsoptionen
| Name | Default | Änderbar | Kommentar |
|---|---|---|---|
| output_buffering | "0" | PHP_INI_PERDIR | |
| output_handler | NULL | PHP_INI_PERDIR | Verfügbar 4.0.4. |
| implicit_flush | "0" | PHP_INI_ALL | PHP_INI_PERDIR in PHP <= 4.2.3. |
Weitere Details und die Definitionen der PHP_INI_*-Konstanten finden Sie im Anhang G, php.ini Einstellungen.
Hier eine kurze Erklärung der Konfigurationsoptionen:
- output_buffering boolean/integer
Sie können Output Buffering für alle Scripte aktivieren indem Sie diesen Wert auf 'On' setzen. Wenn Sie die Größe des Buffers auf limitieren wollen so können Sie die maximale Größe in Bytes an Stelle von 'On' angeben (z.B. output_buffering=4096). Ab PHP 4.3.5 ist dieser Wert für den PHP CLI Kommandozeileninterpreter grundsätzlich 'Off'.
- output_handler string
Sie können die Ausgaben ihres Scriptes an eine Funktion weiterleiten. Wenn sie z.B. mb_output_handler() als output_handler setzen so wird das Character Encoding transparent in das spezifizierte Encoding umgewandelt. Das Setzen einer output_handler Funktion aktiviert das Output Buffering automatisch.
Anmerkung: Sie können mb_output_handler() nicht gleichzeitig mit ob_iconv_handler() und ob_gzhandler() nicht gleichzeitig mit zlib.output_compression. benutzen.
Anmerkung: Es können nur eingebaute PHP-Funktionen angegeben werden, benutzerdefinierte Funktionen können mit Hilfe der ob_start() Funktion genutzt werden.
- implicit_flush boolean
Diese Option ist standardmäßig deaktiviert. Wenn Sie implicit_flush aktivieren so wird PHP angewiesen nach jedem Output-Block automatisch den Output Layer zu leeren. Dies ist äquivalent zu flush() Aufrufen nach jedem echo() oder print() Aufruf und nach jedem HTML-Block.
In Webanwendungen hat diese Option massive Performanceauswirkungen und sollte wenn überhaupt dann nur zu Debuggingzwecken aktiviert werden. Im PHP CLI Kommandozeileninterpreter dagegen ist diese Option standardmäßig aktiviert.
Siehe auch ob_implicit_flush().
Resource Typen
Diese Erweiterung definiert keine Resource-Typen.
Vordefinierte Konstanten
Diese Erweiterung definiert keine Konstanten.
Beispiele
Beispiel 1398. Beispiel Ausgabesteuerung
<?php |
Im obigen Beispiel wird die Ausgabe der echo() solange im Ausgabepuffer zwischengespeichert, bis die Funktion ob_end_flush() aufgeufen wird. In der Zwischenzeit speichert der Aufruf von setcookie() erfolgreich einen Cookie, ohne einen Fehler zu erzeugen. (Normalerweise können Sie keine header an den Browser mehr schicken, wenn schon Daten ausgegeben wurden.)
Siehe auch
See also header() and setcookie().
Inhaltsverzeichnis
- flush — Leert (sendet) den Ausgabepuffer
- ob_clean — Löscht den Ausgabepuffer
- ob_end_clean — Löscht den Ausgabe-Puffer und deaktiviert die Ausgabe-Pufferung
- ob_end_flush — Leert (schickt/sendet) den Ausgabe-Puffer und deaktiviert die Ausgabe-Pufferung
- ob_flush — Leert (sendet) den Ausgabepuffer
- ob_get_clean — Get current buffer contents and delete current output buffer
- ob_get_contents — Gibt den Inhalt des Ausgabe-Puffers zurück
- ob_get_flush — Flush the output buffer, return it as a string and turn off output buffering
- ob_get_length — Return the length of the output buffer
- ob_get_level — Anzahl der aktiven Ausgabepuffer
- ob_get_status — Get status of output buffers
- ob_gzhandler — ob_start callback function to gzip output buffer
- ob_implicit_flush — Schaltet die implizite Ausgabe ein bzw. aus
- ob_list_handlers — List all output handlers in use
- ob_start — Einschalten der Pufferung von Ausgaben
- output_add_rewrite_var — Setzt URL Revriter Variablen
- output_reset_rewrite_vars — Reset URL rewriter values
Sometimes users are blaming about slow pages ... not being aware that mostly this is due to network issues.
So I've decided to add some statistics at the end of my pages:
At beginning I start the counters:
<?php
function microtime_float() {
if (version_compare(PHP_VERSION, '5.0.0', '>')) return microtime(true);
list($u,$s)=explode(' ',microtime()); return ((float)$u+(float)$s);
}
$initime=microtime_float();
ob_start();
ob_implicit_flush();
?>
And at the end I show the statistics:
<?php
echo "PHP Time: ".round((microtime_float()-$initime)*1000)." msecs. ";
echo "Size: ".round_byte(strlen(ob_get_contents()));
ob_end_flush();
?>
(round_byte is my function to print byte sizes)
It seems that while using output buffering, an included file which calls die() before the output buffer is closed is flushed rather than cleaned. That is, ob_end_flush() is called by default.
<?php
// a.php (this file should never display anything)
ob_start();
include('b.php');
ob_end_clean();
?>
<?php
// b.php
print "b";
die();
?>
This ends up printing "b" rather than nothing as ob_end_flush() is called instead of ob_end_clean(). That is, die() flushes the buffer rather than cleans it. This took me a while to determine what was causing the flush, so I thought I'd share.
Please note that most browsers don't display a table unless they passed its end-tag "</table>".
Thats why using this feature in HTML-tables might not result in what you expected...
best regards
BasicArtsStudios
Sometimes you might not want to include a php-file under the specifications defined in the functions include() or require(), but you might want to have in return the string that the script in the file "echoes".
Include() and require() both directly put out the evaluated code.
For avoiding this, try output-buffering:
<?php
ob_start();
eval(file_get_contents($file));
$result = ob_get_contents();
ob_end_clean();
?>
or
<?php
ob_start();
include($file);
$result = ob_get_contents();
ob_end_clean();
?>
which i consider the same, correct me if I'm wrong.
Best regards, BasicArtsStudios
Unfortunately, the PHP guys didn't build support into any of the image output functions to return the image instead of outputting it.
Fortunately, we have output buffering to fix that.
<?php
$im = imagecreatetruecolor(200, 200);
// Other image functions here...
ob_start();
imagepng($im);
$imageData = ob_get_contents();
ob_clean();
?>
You can now use the $imageData variable to either create another GD image, save it, put it in a database, make modifications to the binary, or output it to the user. You can easily check the size of it as well without having to access the disk...just use strlen();
Now this just blew my mind. I had a problem with MySQL being incredibly slow on Windows 2003 running IIS... on ASP/VBScript pages. PHP is also installed on the server and so is Microsoft SQL 2005 Express. (Yes, we're running ASP, PHP, MySQL and MS SQL on the same Windows 2003 Server using IIS.)
I was browsing the internet for a solution and saw a suggestion that I change output_buffering to on if MySQL was slow for PHP pages. Since we also served PHP pages with MySQL from the same server, it caught my eye. For the hell of it, I went into php.ini and changed output_buffering to on and suddenly MySQL and ASP was faster... MySQL and PHP was faster... Microsoft SQL Server 2005 Express and ASP was faster.... everything was faster... even stuff that had no PHP!
And I didn't even have to restart IIS. As soon as I saved the php.ini file with the change, everything got faster.
Apparently PHP and MySQL and IIS are so intertwined somehow that changing the buffering setting really effects the performance of the entire server.
So, if you are having performance problems on Windows 2003 & IIS, you might try setting output_buffering = On in php.ini if you happen to have PHP installed. Having it set to off apparently effects the performance of Windows 2003 and IIS severely... even for webpages that do not use PHP or MySQL.
Output buffering is set to '4096' instead of 'Off' or '0' by default in the php-5.0.4-10.5 RPM for Fedora Core release 4 (Stentz). This has cost me much time!
I ran out of memory, while output buffering and drawing text on imported images. Only the top portion of the 5MP image was displayed by the browser. Try increasing the memory limit in either the php.ini file( memory_limit = 16M; ) or in the .htaccess file( php_value memory_limit "16M" ). Also see function memory_get_usage() .
For those who are looking for optimization, try using buffered output.
I noticed that an output function call (i.e echo()) is somehow time expensive. When using buffered output, only one output function call is made and it seems to be much faster.
Try this :
<?php
your_benchmark_start_function();
for ($i = 0; $i < 5000; $i++)
echo str_repeat ("your string blablabla bla bla", (rand() % 4) + 1)."<br>\n";
echo your_benchmark_end_function();
?>
And then :
<?php
your_benchmark_start_function();
ob_start ();
for ($i = 0; $i < 5000; $i++)
echo str_repeat ("your string blablabla bla bla", (rand() % 4) + 1)."<br>\n";
echo your_benchmark_end_function();
ob_end_flush ();
?>
Trying to benchmark your server when using output_buffering ?
Don't forget that the value 4096 in the php.ini will give you complete different loadtimes compares to the value of 1.
In the first case the output will be sent after buffering 4096 and the loadtime timed at the end of the page will contain the loadtime needed to download the complete page in the clientbrowser while the second value will contain the loadtime needed to place the complete page in the buffer. The time needed for sending is not clocked.
This can be very frustrating if you don't see the differance between server and the 1st is using 4096 instead of 1.
Although technically much faster than the second server the second server was providing much better loadtime results.
This result will grow when using large amounts of output.
But this becomes interesting if you want to measure the time needed for the page to be loaded for the client.
