What languages are used to create these internet speed sites? Could you give examples of how to calculate this? Or is it something complex?
What languages are used to create these internet speed sites? Could you give examples of how to calculate this? Or is it something complex?
The account is simple, and any language is meant to be implemented on the server (on the browser side, obviously it has to be javascript ). Essentially you have to have one or several files of known size, and have the client's browser download these files, storing the server's time in a variable immediately before starting the download and immediately after it finishes.
Subtracting the two dates, you have how long it took for the client to download the file, and as the file size is known, you divide this size by the time elapsed and find the speed.
For example, we can write a file index.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Teste de velocidade</title>
<script type="text/ecmascript">
document.addEventListener("DOMContentLoaded", function (evt) {
document.getElementById("executar").addEventListener (evt) {
var tam = parseInt(document.getElementById("tam"), 10);
var inicio = new Date();
fetch("dados.cgi?tam=" + tam, { method: "get" }).then(function (dados) {
var fim = new Date();
var segundos = (fim.getTime() - inicio.getTime()) / 1000;
document.getElementById("resultado").innerText = (tam / segundos) + " B/s";
});
});
});
</script>
</head>
<body>
<h1>Teste de velocidade caseiro</h1>
<h2>Sua velocidade ao baixar um arquivo de <input type="text" id="tam" value="1048576"> é: <span id="resultado"></span></h2>
<button id="executar">Testar</button>
</body>
</html>
And dados.cgi
, written in C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(int argc, char ** argv) {
char * qstring = getenv("QUERY_STRING");
size_t tamanho = 0;
if (!strncmp(qstring, "tam=", 4)) {
tamanho = strtoul(qstring + 4, &qstring, 10);
printf("200 Ok\r\n"
"Content-Type: application/octet-stream\r\n"
"Content-Size: %d\r\n"
"\r\n",
tamanho
);
fwrite("U", 1, tamanho, stdout);
}
return 0;
}
Note that I can not guarantee that this example works because I do not have access to a server to try to run the CGI script, but it will not differ much from that. In this case, I created a CGI to generate files in time with an arbitrary size, but you could have a set of static files on your server to download. Also missing any type of error checking in this script (for example, if you put a non-numeric value or zero in the size box, the script gives dick). But as a general idea, it should serve.