AY 4 лет назад
Родитель
Сommit
a4f6b11680
4 измененных файлов с 36 добавлено и 54 удалено
  1. 32 46
      web/Speedtest/dl.html
  2. 1 1
      web/Speedtest/init.agi
  3. 3 7
      web/Speedtest/special/wspeedtest.js
  4. 0 0
      web/Speedtest/ul.html

+ 32 - 46
web/Speedtest/dl.html

@@ -66,10 +66,11 @@
         socket.onmessage = function(event) {
             if (event.data.indexOf("DATA:") == -1) {
                 if (event.data.indexOf("TTL_BANDWIDTH") != -1) {
-                    var bandwidth = event.data.split("=")[1].split(" ");
+                    let bandwidth = event.data.split("=")[1].split(" ");
                     $("#dl").text(bandwidth[0]);
                     $("#dll").text("Download (" + bandwidth[1] + ")");
                     uploadBench();
+					//pingTest();
                 }
             }
         };
@@ -86,9 +87,7 @@
         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);
+            setTimeout(executeULTest, 1000, socket);
         };
 
         socket.onerror = function(error) {
@@ -96,30 +95,31 @@
         };
     }
 
-    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 executeULTest(socket) {
+		let CurrentMB = 0;
+        let start = new Date();
+		while(socket.bufferedAmount < 10485760 * 10) {
+			socket.send(new ArrayBuffer(32768));
+			CurrentMB += 32768;
+		}
+		let checker = setInterval(function(){ 
+			if(socket.bufferedAmount == 0) {
+				let end = new Date();
+				CurrentDif = (end.getTime() - start.getTime()) / 1000;
+				socket.send("stop");
+				let bandwidth = bytesToSpeed(CurrentMB / CurrentDif).split(" ");
+				$("#ul").text(bandwidth[0]);
+				$("#ull").text("Upload (" + bandwidth[1] + ")");
+				clearInterval(checker);
+				pingTest();
+			}
+		}, 100);
     }
 
     function pingTest() {
-        var clientSendTimeStamp = [];
-        var serverResponseTimeStamp = [];
-        var clientReceiveTimeStamp = [];
+        let clientSendTimeStamp = [];
+        let serverResponseTimeStamp = [];
+        let clientReceiveTimeStamp = [];
         $("#ping").text("⏱️");
         let socket = new WebSocket(getWSEndpoint() + "/system/ajgi/interface?script=Speedtest/special/wspeedtest.js");
 
@@ -140,11 +140,10 @@
     }
 
     function executePingTest(socket, clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp, count) {
-        var clientTime = new Date();
+        let 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 {
@@ -153,13 +152,12 @@
     }
 
     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++) {
+        let diff = 0;
+        for (let 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);
@@ -176,31 +174,19 @@
         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'];
+        let sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
         if (bytes == 0) return '0 Byte';
-        var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
+        let 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'];
+        let sizes = ['bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps'];
         if (bytes == 0) return '0 Byte';
-        var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1000)));
+        let i = parseInt(Math.floor(Math.log(bytes) / Math.log(1000)));
         return Math.round((bytes / Math.pow(1000, i)) * 100, 3) / 100 + ' ' + sizes[i];
     }
 </script>

+ 1 - 1
web/Speedtest/init.agi

@@ -14,7 +14,7 @@ var moduleLaunchInfo = {
 	SupportFW: true,
 	LaunchFWDir: "Speedtest/index.html",
 	SupportEmb: false,
-	InitFWSize: [450, 210]
+	InitFWSize: [600, 210]
 }
 
 //Register the module

+ 3 - 7
web/Speedtest/special/wspeedtest.js

@@ -87,29 +87,25 @@ if (setup()) {
 function downloadTest() {
     var CurrentPow = 0;
     var CurrentDif = 0;
-    //websocket.send("Waiting for the string to be generated...");
-    //randomStr = rnd(10000);
     randomStr = appdata.readFile("Speedtest/special/random1MB.txt");
     var filesize = "DATA:".length + randomStr.length;
-    //websocket.send("Generated");
     waitForStart();
 
     while (CurrentDif < 5) {
         var CurrentMB = Math.pow(2, CurrentPow);
-        //websocket.send("Current File Size:" + bytesToSize(CurrentMB * 1024 * 1024));
         var start = new Date();
-        //websocket.send("Start: " + start.toString());
+		//websocket.send(start);
         for (var i = 0; i < CurrentMB; i++) {
             websocket.send("DATA:" + randomStr);
         }
         var end = new Date();
-        //websocket.send("End: " + end.toString());
         CurrentDif = (end.getTime() - start.getTime()) / 1000;
+		//websocket.send(end);
+		//websocket.send(CurrentDif);
         CurrentPow++;
     }
     websocket.send("TTL_SIZE=" + bytesToSize(CurrentMB * filesize));
     websocket.send("TTL_TIME=" + CurrentDif + "s");
-    //websocket.send("TTL_BANDWIDTH=" + bytesToSize(CurrentMB * filesize / CurrentDif) + "/s OR " + bytesToSpeed(CurrentMB * filesize / CurrentDif));
     websocket.send("TTL_BANDWIDTH=" + bytesToSpeed(CurrentMB * filesize / CurrentDif));
 }
 

+ 0 - 0
web/Speedtest/ul.html