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 (); ------------------------
?>
CXI. 出力制御関数(output control)
導入
出力制御関数により、スクリプトから送信される出力を制御することが可 能になります。この機能は、複数の異なった場面、特にスクリプトがデー タ出力を開始した後にヘッダをブラウザに送信する必要がある場合に有用 です。出力制御関数は、header() または setcookie()を使用して送信されたヘッダには作用せ ず、echo() のような関数とPHPコードのブロック間 のデータにのみ作用します。
要件
外部ライブラリを必要としません。
インストール手順
PHP コアに含まれるため、 追加のインストール無しで使用できます。
実行時設定
php.ini の設定により動作が変化します。
表 1. 出力制御設定オプション
| 名前 | デフォルト | 変更の可否 | 変更履歴 |
|---|---|---|---|
| output_buffering | "0" | PHP_INI_PERDIR | |
| output_handler | NULL | PHP_INI_PERDIR | PHP 4.0.4 以降で使用可能です。 |
| implicit_flush | "0" | PHP_INI_ALL | PHP <= 4.2.3 では PHP_INI_PERDIR です。 |
以下に設定ディレクティブに関する 簡単な説明を示します。
- output_buffering boolean/integer
このディレクティブを 'On' と設定することにより、全てのファイルに 関して出力バッファリングを有効にすることができます。 特定の大きさにバッファの大きさを制限したい場合、このディレクティブの 値として 'On' の代わりに最大バイト数(例:output_buffering=4096) を使用することができます。 PHP 4.3.5 以降、PHP-CLI ではこのディレクティブが常に Off となります。
- output_handler string
スクリプトの全ての出力を関数にリダイレクトすることができます。 例えば、output_handler に mb_output_handler() を指定した場合、文字エンコーディングは透過的に指定したエンコーディングに 変換されます。出力ハンドラを指定することにより自動的に出力 バッファリングを on にします。
注意: ob_iconv_handler() と mb_output_handler() の両方で使用することは できません。また、 ob_gzhandler() と zlib.output_compression の両方を使用することはできません。
注意: このディレクティブには、組み込み関数のみが使用可能です。ユーザ定義の 関数については、ob_start() を使用してください。
- implicit_flush boolean
デフォルトは FALSE です。これを TRUE に変更すると、PHP が 各出力ブロックの後で自動的に出力レイヤをフラッシュするよう 指定します。これは、各 print() および HTML ブロックの後で flush() 関数をコールすることと等価です。
Web 環境の中で PHP を使用している時に このオプションを on に変更すると、著しい性能低下が生じるため、 通常はデバッグ目的のみにすることが推奨されます。CLI SAPI のもとで実行される時、この値はデフォルトで TRUE になっています。
ob_implicit_flush() も参照ください。
リソース型
リソース型は定義されていません。
定義済み定数
定数は定義されていません。
例
上記の例では、echo()からの出力は、 ob_end_flush() がコールされるまで出力バッファに 保存されます。この際、 setcookie()をコールするとエラーを発生することな くクッキーが保存されます。(通常、データの送信後はブラウザにヘッダ を送信することはできません。)
注意: PHP 4.1 (および4.2)から4.3に更新する際、前のバージョンのバグのせ いで、php.iniのimplict_flushを OFFにする必要があります。さもないと、 ob_start()を使用する全ての出力は、出力を抑制 することができなくなります。
参考
header()およびsetcookie() も参照ください。
- 目次
- flush -- 出力バッファをフラッシュする
- ob_clean -- 出力バッファをクリア(消去)する
- ob_end_clean -- 出力用バッファをクリア(消去)し、出力のバッファリングをオフにする
- ob_end_flush -- 出力用バッファをフラッシュ(送信)し、出力のバッファリングをオフに する
- ob_flush -- 出力バッファをフラッシュ(送信)する
- ob_get_clean -- 現在のバッファの内容を取得し、出力バッファを削除する
- ob_get_contents -- 出力用バッファの内容を返す
- ob_get_flush -- 出力バッファをフラッシュし、 その内容を文字列として返した後で出力バッファリングを終了する
- ob_get_length -- 出力バッファの長さを返す
- ob_get_level -- 出力バッファリング機構のネストレベルを返す
- ob_get_status -- 出力バッファのステータスを取得する
- ob_gzhandler -- 出力バッファを gzip 圧縮するための ob_start コールバック関数
- ob_implicit_flush -- 自動フラッシュをオンまたはオフにする
- ob_list_handlers -- 使用中の出力ハンドラの一覧を取得する
- ob_start -- 出力のバッファリングを有効にする
- output_add_rewrite_var -- URL リライタの値を追加する
- output_reset_rewrite_vars -- URL リライタの値をリセットする
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.
