123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="apple-mobile-web-app-capable" content="yes" />
- <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1" />
- <meta charset="UTF-8">
- <meta name="theme-color" content="#4b75ff">
- <link rel="stylesheet" href="../script/semantic/semantic.min.css">
- <script src="../script/jquery.min.js"></script>
- <script src="../script/ao_module.js"></script>
- <script src="../script/semantic/semantic.min.js"></script>
- <title>WebSocket Test</title>
- </head>
- <body>
- <br><br>
- <div class="ui container">
- <div class="ui three statistics">
- <div class="statistic">
- <div class="value" id="dl">
- -
- </div>
- <div class="label" id="dll">
- Download
- </div>
- </div>
- <div class="statistic">
- <div class="value" id="ul">
- -
- </div>
- <div class="label" id="ull">
- Upload
- </div>
- </div>
- <div class="statistic">
- <div class="value" id="ping">
- -
- </div>
- <div class="label" id="pingl">
- Ping
- </div>
- </div>
- </div>
- <br>
- <button class="ui fluid button" onclick="startBench();">Test</button>
- </div>
- </body>
- <script>
- ao_module_setFixedWindowSize();
- function startBench() {
- downloadBench();
- }
- function downloadBench() {
- $("#dl").text("⏱️");
- let socket = new WebSocket(getWSEndpoint() + "/system/ajgi/interface?script=Speedtest/special/wspeedtest.js");
- socket.onopen = function(e) {
- $("#dl").text("⌛");
- socket.send("DWL");
- };
- socket.onmessage = function(event) {
- if (event.data.indexOf("DATA:") == -1) {
- if (event.data.indexOf("TTL_BANDWIDTH") != -1) {
- var bandwidth = event.data.split("=")[1].split(" ");
- $("#dl").text(bandwidth[0]);
- $("#dll").text("Download (" + bandwidth[1] + ")");
- uploadBench();
- }
- }
- };
- socket.onerror = function(error) {
- $("#dl").text("❌");
- };
- }
- function uploadBench() {
- $("#ul").text("⏱️");
- let socket = new WebSocket(getWSEndpoint() + "/system/ajgi/interface?script=Speedtest/special/wspeedtest.js");
- socket.onopen = function(e) {
- $("#ul").text("⌛");
- socket.send("UPL");
- var randomStr = rnd(32768);
- var filesize = "DATA:".length + randomStr.length;
- setTimeout(executeULTest, 1000, socket, 0, 0, randomStr, filesize);
- };
- socket.onerror = function(error) {
- $("#ul").text("❌");
- };
- }
- function executeULTest(socket, CurrentPow, CurrentDif, randomStr, filesize) {
- var CurrentMB = Math.pow(2, CurrentPow);
- var start = new Date();
- for (var i = 0; i < CurrentMB; i++) {
- socket.send("DATA:" + randomStr);
- }
- var end = new Date();
- CurrentDif = (end.getTime() - start.getTime()) / 1000;
- CurrentPow++;
- if (CurrentDif < 5) {
- setTimeout(executeULTest, 1000, socket, CurrentPow, CurrentDif, randomStr, filesize);
- } else {
- socket.send("stop");
- var bandwidth = bytesToSpeed(CurrentMB * filesize / CurrentDif).split(" ");
- $("#ul").text(bandwidth[0]);
- $("#ull").text("Upload (" + bandwidth[1] + ")");
- pingTest();
- }
- }
- function pingTest() {
- var clientSendTimeStamp = [];
- var serverResponseTimeStamp = [];
- var clientReceiveTimeStamp = [];
- $("#ping").text("⏱️");
- let socket = new WebSocket(getWSEndpoint() + "/system/ajgi/interface?script=Speedtest/special/wspeedtest.js");
- socket.onopen = function(e) {
- $("#ping").text("⌛");
- socket.send("PING");
- setTimeout(executePingTest, 500, socket, clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp, 0);
- };
- socket.onmessage = function(event) {
- clientReceiveTimeStamp.push(new Date().getTime());
- serverResponseTimeStamp.push(event.data);
- };
- socket.onerror = function(error) {
- $("#ping").text("❌");
- };
- }
- function executePingTest(socket, clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp, count) {
- var clientTime = new Date();
- socket.send(clientTime);
- clientSendTimeStamp.push(clientTime.getTime());
- count++;
- console.log(count);
- if (count < 3) {
- setTimeout(executePingTest, 1000, socket, clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp, count);
- } else {
- setTimeout(showPingResult, 2000, clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp);
- }
- }
- function showPingResult(clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp) {
- console.log(clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp);
- serverResponseTimeStamp.shift();
- serverResponseTimeStamp.shift();
- clientReceiveTimeStamp.shift();
- clientReceiveTimeStamp.shift();
- var diff = 0;
- for (var i = 0; i < clientSendTimeStamp.length; i++) {
- diff += (clientReceiveTimeStamp[i] - clientSendTimeStamp[i]) - (serverResponseTimeStamp[i].split(",")[1] - serverResponseTimeStamp[i].split(",")[0]);
- }
- $("#ping").text(Math.round((diff / clientSendTimeStamp.length) * 100) / 100);
- $("#pingl").text("Ping (ms)");
- }
- function getWSEndpoint() {
- //Open opeartion in websocket
- let protocol = "wss://";
- if (location.protocol !== 'https:') {
- protocol = "ws://";
- }
- wsControlEndpoint = (protocol + window.location.hostname + ":" + window.location.port);
- return wsControlEndpoint;
- }
- //https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript
- function rnd(length) {
- var result = '';
- var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
- var charactersLength = characters.length;
- for (var i = 0; i < length; i++) {
- result += characters.charAt(Math.floor(Math.random() *
- charactersLength));
- }
- return result;
- }
- //https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
- function bytesToSize(bytes) {
- var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
- if (bytes == 0) return '0 Byte';
- var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
- return Math.round((bytes / Math.pow(1024, i)) * 100, 3) / 100 + ' ' + sizes[i];
- }
- function bytesToSpeed(bytes) {
- bytes = bytes * 8;
- var sizes = ['bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps'];
- if (bytes == 0) return '0 Byte';
- var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1000)));
- return Math.round((bytes / Math.pow(1000, i)) * 100, 3) / 100 + ' ' + sizes[i];
- }
- </script>
- </html>
|