Determine connection speed with php

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:
  1. <?php
  2. $kb=512;
  3. echo "streaming $kb Kb...<!-";
  4. $time = explode(" ",microtime());
  5. $start = $time[0] + $time[1];
  6. for($x=0;$x<$kb;$x++){
  7.     echo str_pad('', 1024, '.');
  8.     flush();
  9. }
  10. $time = explode(" ",microtime());
  11. $finish = $time[0] + $time[1];
  12. $deltat = $finish - $start;
  13. echo "-> Test finished in $deltat seconds. Your speed is ". round($kb / $deltat, 3)."Kb/s";
  14. ?>

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.

If you liked this post buy me a beer (or two)

12 Comment(s)

  1. jacob | Jun 9, 2006 | Reply

    it doesn’t reflect true values

  2. isoveli | Jun 23, 2006 | Reply

    this script is greatly affected by the server this script is in; http://www.triquitips.com/timetest.php claims my speed is ~231 Kb/s but the script on my own server says it is nearly 395 Kb/s

  3. colin | Jul 19, 2006 | Reply

    Provides an excellent and very useful way of giving visitors an approximate duration for subsequent downloads. Thank you.

  4. Michel Bozgounov | Oct 13, 2006 | Reply

    Interesting! :-)

  5. Wha...??!!? | Dec 17, 2006 | Reply

    uhhh… I have a 8.1Mb/sec connection, and it says that i have a speed of 303Kb/sec! I am now gonna test with a speed tester I downloaded…… (http://myspeed.com/)… That says 6.1 Mb/sec (that’s what can be expected, if you are british, you will know that BT is :(:(:(:(:(:( )

    But the upload speed is 376Kb/sec… any way this might link?

  6. Liam | May 17, 2007 | Reply

    a useful script!! ideal for my entertainment site to tell people how long it will take until the flash runs!! good job.

  7. Cody Ray | Jul 23, 2007 | Reply

    This does not reflect the actual connection speed. What this does tell you is how long the CPU on the server takes to execute this script, as all processing is done server-side in PHP. Try again.

  8. Tippi | Aug 31, 2007 | Reply

    Just one fault:

    If the server’s bandwidth is lower then the clients the servers bandwidth will be reported as the speed…

    This can be fixed, i have rewritten you code with some of mine, basiclly you should set the timestamps just before and after the echo.
    Becuse 1024 bytes for sure can be delivered at max speed with the any connection today, 512kb can’t :)

    Then when that loop is done, sum all up and make a avarage and do ur calculation.

    Send me a mail if you want the source for the modified version. Cheers.

  9. RR | Oct 21, 2007 | Reply

    Tippi: can you please post the code?

  10. deo | Dec 7, 2007 | Reply

    how about to determine my connection to a web server?? Tippi post your code, please.

  11. Determine connection speed | Apr 6, 2008 | Reply

    determine connection is easy to find with above written code

  12. getpr | Apr 16, 2008 | Reply

    This can be fixed, i have rewritten you code with some of mine, basiclly you should set the timestamps just before and after the echo.
    Becuse 1024 bytes for sure can be delivered at max speed with the any connection today, 512kb can’t :)

Post a Comment