Site view internet speed

Asked

Viewed 391 times

0

What languages are used to create these Internet speed sites? Could you give examples of how to calculate this? Or is it something complex?

  • 2

    To the question "Which languages are used" makes the question very broad, because it would be practically impossible to quote all that are possible, so some people can give the -1. If you remove that from the question, you would receive more +1, because it would be interesting to know how it works, regardless of language.

  • I get it, it’s because I don’t know which ones. That’s why I asked. Sometimes it’s just like with PHP, sometimes with Javascript, sometimes with some more, but thanks!

1 answer

7


The account is simple, and any language serves to implement it on the server (on the browser side, it obviously has to be on javascript). Essentially you have to have one or several files of known size, and have the client’s browser download these files, storing in a variable the server time immediately before starting the download and immediately after it is finished.

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 elapsed time 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 the 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 don’t guarantee that this example works, because I don’t have access to a server to try to run the CGI script, but it won’t differ much from that. In this case, I created a CGI to generate files on the fly with an arbitrary size, but you could have a set of static files on your server to download. Any type of error checking is also missing in this script (for example, if you put a non-numeric or zero value in the size box, the script is dead). But as a general idea, it should serve.

  • Thank you, I will search how to do this "Essentially you have to have one or several files of known size, and have the client browser download these files".

  • I don’t understand the down vote of the people, really.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.