Jump to content

JavaScript Client Connection Speed Test


php_tom

Recommended Posts

Hey everyone...

 

I've been working on a connection speed test so I can give users a download time estimate for bigger files. It only works if you have JavaScript enabled in your browser, but I think it's pretty accurate (like, within ~50%). Please try it out, let me know what you think, or if you have any suggestions for tweaking it to get better performance.

 

<html>
<body>
<script type="text/javascript">
var start, end;
var c = 0;
function start() {
    var d = new Date();
    start = d.getTime();
    c++;
    // The image should probably be at least 50KB if you want the test to be accurate.
    document.getElementById('img1').src = "connTest.jpg?t="+start;
    // The ?t=1230478659256 at the end prevents browser caching.
}
function loaded(){
    var d = new Date();
    end = d.getTime();
    // I hard-coded the image size -- 396320 bits... you could use PHP's filesize() or something.
    var connSpeed = Math.round(396320/(end-start)); // connSpeed will be in kBITSps
    // Now I redirect to the destination page, which can access $_GET['speed'] dynamically
    window.location.href = "download.php?speed="+connSpeed;
}
</script>
<img id='img1' onload='if(c==1) loaded();' style='visibility:hidden;' />
<script type='text/javascript'>start();</script>
<noscript>
  <!-- No JavaScript? Redirect to destination page and test if($_GET['speed'] == "") -->
  <meta HTTP-EQUIV="REFRESH" content="0; url=download.php">
</noscript>
</body>
</html>

 

Attached is the image I made, connTest.jpg. Let me know if this works for you!

 

[attachment deleted by admin]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.