Skip to content

Instantly share code, notes, and snippets.

@leshikus
Last active December 16, 2015 01:30
Show Gist options
  • Save leshikus/5355920 to your computer and use it in GitHub Desktop.
Save leshikus/5355920 to your computer and use it in GitHub Desktop.
Repetitive network bandwidth check
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
var relax = 5000;
var start = 0;
var end = 0;
var binfile = '';
function log(message) {
var d = document.getElementById('data');
d.innerHTML = d.innerHTML + message + '<br/>';
}
function TestDownload() {
start = new Date().getTime();
$.ajax({
type: "GET",
url: "bigfile.bin?id=" + start,
dataType: 'application/octet-stream',
success: function(msg) {
binfile = msg;
end = new Date().getTime();
diff = (end - start) / 1000;
bytes = msg.length;
speed = (bytes / diff) / 1024 / 1024 * 8;
speed = Math.round(speed*100)/100;
log('Download ' + speed + ' Mb/s');
},
complete: function(xhr, textStatus) {
setTimeout(TestUpload, relax);
}
});
}
function TestUpload() {
start = new Date().getTime();
$.ajax({
type: "POST",
url: "post.aspx?id=" + start,
data: binfile,
dataType: 'application/octet-stream',
success: function(msg) {
end = new Date().getTime();
diff = (end - start) / 1000;
bytes = binfile.length;
speed = (bytes / diff) / 1024 / 1024 * 8;
speed = Math.round(speed*100)/100;
log('Upload ' + speed + ' Mb/s');
},
complete: function(xhr, textStatus) {
setTimeout(TestDownload, relax);
}
});
}
$(document).ready(function() {
TestDownload();
});
</script>
</head>
<body>
<div id="data"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment