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
https://forums.phpfreaks.com/topic/63465-javascript-client-connection-speed-test/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.