Skip to content

Instantly share code, notes, and snippets.

@makotom
Last active December 12, 2015 06:39
Show Gist options
  • Save makotom/4731073 to your computer and use it in GitHub Desktop.
Save makotom/4731073 to your computer and use it in GitHub Desktop.
Useless network speed tester
<?php
define("TESTFILE", "zero");
if(isset($_GET["d"])){
readfile(TESTFILE);
exit();
}else if(isset($_GET["n"])){
exit();
}
?>
<!doctype html>
<meta charset="UTF-8">
<title>netspeed</title>
<script>
(function(){
var netSpeed = {rx : 0, tx : 0},
testdata = "",
xhrSpeed = function(url, data, nextStep){
var x = new XMLHttpRequest(),
time = 0;
x.open("POST", url, true);
x.onreadystatechange = function(){
if(x.readyState === 4){
time = new Date().getTime() - time;
nextStep(x, time);
}
};
time = new Date().getTime();
x.send(data);
},
getMbps = function(ms, byte){
return ((byte * 8) / (ms / 1000)) / 1000000;
},
showNetTime = function(){
alert("RX: " + netSpeed.rx.toString() + " Mbps\nTX: " + netSpeed.tx.toString() + " Mbps");
},
afterTx = function(x, time){
netSpeed.tx = getMbps(time, testdata.length);
showNetTime();
},
beforeTx = function(){
xhrSpeed("?n", testdata, afterTx);
},
afterRx = function(x, time){
testdata = x.responseText;
netSpeed.rx = getMbps(time, testdata.length);
beforeTx(x.responseText);
},
beforeRx = function(){
if(confirm("Press OK to start")){
xhrSpeed("?d", null, afterRx);
}
};
addEventListener("click", beforeRx, false);
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment