note 86870 added to function.microtime

From: Date: Fri, 07 Nov 2008 11:10:12 +0000
Subject: note 86870 added to function.microtime
Groups: php.notes 
Request: Send a blank email to php-notes+get-146274@lists.php.net to get a copy of this message
Here is a simple way to measure the clients connection speed (assuming the server is able to supply fast enough). It measures how much data is transfered in one second and reports the result. I needed to measure this to offer sensible defaults for selecting live streams at different bitrates for a webcasting application. <?php function measure_kbps() { //Return the unix timestamp + microseconds function micro_time() { $timearray = explode(" ", microtime()); return ($timearray[1] + $timearray[0]); } //Prepare a 1kB chunk to send for ($i=0; $i<1023; $i++) $chunk.='A'; $chunk.='\n'; //Hide what happens next echo "<!-- "; //Keep sending 1 kB chunks for 1 second flush(); $count=0; $starttime = micro_time(); do { echo $chunk; $count++; flush(); $endtime = micro_time(); $totaltime = $endtime - $starttime; $totaltime = round($totaltime,5); } while ($totaltime < 1); echo " -->\n"; //Return how many kb were sent return ($count * 8); } $kbps=measure_kbps(); $mbps=$kbps / 1024; if ($mbps > 1 ) echo "Your speed is $mbps Mbps.<br />"; else echo "Your speed is $kbps Kbps.<br />"; ?> ---- Server IP: 92.48.74.199 Probable Submitter: 78.105.1.254 ---- Manual Page -- http://www.php.net/manual/en/function.microtime.php Edit -- https://master.php.net/note/edit/86870 Del: integrated -- https://master.php.net/note/delete/86870/integrated Del: useless -- https://master.php.net/note/delete/86870/useless Del: bad code -- https://master.php.net/note/delete/86870/bad+code Del: spam -- https://master.php.net/note/delete/86870/spam Del: non-english -- https://master.php.net/note/delete/86870/non-english Del: in docs -- https://master.php.net/note/delete/86870/in+docs Del: other reasons-- https://master.php.net/note/delete/86870 Reject -- https://master.php.net/note/reject/86870 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#146274) next »