One of the hardest script you can find in internet is the one that determines connection speed of a surfer visiting our site.
From today it’s not anymore a problem (even if nobody had nightmares before…) because we will make a php script in order to fill such lack.
Let’s start coding…
<?php
$kb=512;
echo "streaming $kb Kb...<!-";
flush();
$time = explode(" ",microtime());
$start = $time[0] + $time[1];
for($x=0;$x<$kb;$x++){
echo str_pad('', 1024, '.');
flush();
}
$time = explode(" ",microtime());
$finish = $time[0] + $time[1];
$deltat = $finish - $start;
echo "-> Test finished in $deltat seconds. Your speed is ". round($kb / $deltat, 3)."Kb/s";
?>
Done. Analysis and considerations:
2: Declaring how much Kb I want to transmit to accomplish the test. This value can also be passed in POST or GET. In my example it is declared.
3: I write I am downloading data, and open a comment tag. Everithing I am sending from now on will not be displayed. It’s not necessary, but prevents the browser to be fullfilled of chars.
4: Flush the chache
5-6: Saving actual timestamp
7-9: For $kb times I send 1024 chars (1Kb) and flush the cache
11-12: Saving actual timestamp
13: Calculating difference between start and finish timestamps
14: Closing comment tag and writing test result, calculated as Kb/time
Done… very easy isn’t it?
Considerations and exceptions
I wasn’t able to get reliable speed values with $kb under 512. I noticed the higher this number, the more reliable the result.
In some php configurations, those with output buffering to “On”, php executes before sending headers, cookies, and so on, and the time calculated in that way is the time of pure execution (excluding tranfers). Disabling output buffering, however, results are reliable.
You can try the script at this page.
pre {direction:ltr;}
</style>
<code>
<?php
$kb=512;
echo "streaming $kb Kb…<!-";
flush();
$time = explode(" ",microtime());
$start = $time[0] + $time[1];
for($x=0;$x<$kb;$x++){
echo str_pad(”, 1024, ‘.’);
flush();
}
$time = explode(" ",microtime());
$finish = $time[0] + $time[1];
$deltat = $finish – $start;
echo "-> Test finished in $deltat seconds. Your speed is ". round($kb / $deltat, 3)."Kb/s";
?>
</code>
You might also like
| AVM2 (AS3) to AVM1 (AS2/1) Communication via LocalConnection The ActionScript virtual machine that runs ActionScript 3 code (AVM2) is completely different from the ActionScript... | Tweener Class Tips! I've been using Tweener for almost an year now, and I thought I'd share a few quick and useful tips. Delayed... | Processing Revolution 3D in Flash Adobe MAX 2010 conference in the keynote for the new Flash Player capabilities introduced, the feature was long... | Edit the CSS styles of an element in Javascript I’ve actually started to learn the Internet beast JavaScript, and I think it’s about time I shared some knowledge... |








