再現できる奇妙な動作を投稿します(少なくともapache2 + php5では)。私が間違っているかどうかはわかりませんが、私が達成しようとしていることを説明させてください。
バイナリデータのチャンク(たとえば30)を送信し、最後に平均Kbit / sを分析する必要があります:
各チャンク出力時間、各チャンクサイズを合計し、最後にKbit/s計算を実行します。
<?php
// build my binary chunk
$var= '';
$o=10000;
while($o--)
{
$var.= pack('N', 85985);
}
// get the size, prepare the memory.
$size = strlen($var);
$tt_sent = 0;
$tt_time = 0;
// I send my chunk 30 times
for ($i = 0; $i < 30; $i++)
{
// start time
$t = microtime(true);
echo $var."\n";
ob_flush();
flush();
$e = microtime(true);
// end time
// the difference should reprenent what it takes to the server to
// transmit chunk to client right ?
// add this chuck bench to the total
$tt_time += round($e-$t,4);
$tt_sent += $size;
}
// total result
echo "\n total: ".(($tt_sent*8)/($tt_time)/1024)."\n";
?>
上記のこの例では、これまでのところ機能しています(ローカルホストでは、さまざまなテストを通じて7000から10000 Kbit / sまで振動します)。
ここで、送信を整形したいとします。これは、クライアントが1秒間処理するのに十分なデータのチャンクを持っていることがわかっているためです。
チャンク送信間の一時停止をマークするために、usleep(1000000)を使用することにしました。
<?php
// build my binary chunk
$var= '';
$o=10000;
while($o--)
{
$var.= pack('N', 85985);
}
// get the size, prepare the memory.
$size = strlen($var);
$tt_sent = 0;
$tt_time = 0;
// I send my chunk 30 times
for ($i = 0; $i < 30; $i++)
{
// start time
$t = microtime(true);
echo $var."\n";
ob_flush();
flush();
$e = microtime(true);
// end time
// the difference should reprenent what it takes to the server to
// transmit chunk to client right ?
// add this chuck bench to the total
$tt_time += round($e-$t,4);
$tt_sent += $size;
usleep(1000000);
}
// total result
echo "\n total: ".(($tt_sent*8)/($tt_time)/1024)."\n";
?>
この最後の例では、理由がわかりません。計算された帯域幅が72000 Kbit / sから1,200,000に跳ね上がる可能性があり、まったく不正確/無関係です。問題の一部は、チャンクが送信されるたびに(最初のusleepの後)、チャンクを出力するために測定された時間が途方もなく短いことです。
私は何か間違ったことをしていますか?バッファ出力は同期していませんか?