Know URL read time

Asked

Viewed 46 times

0

I’m reading the content that has a url

Using the following code block:

String urlNormal = "http://minhaurl";
URLConnection conn = urlNormal.openConnection();
BufferedReader input = new BufferedReader(new InputStreamReader(conn.getInputStream()));

I wonder if there’s any way I know how much time it cost him to read the data or how much time he spent trying to read

2 answers

2


It could be so:

long inicio = System.currentTimeMillis();

String urlNormal = "http://minhaurl";
URLConnection conn = urlNormal.openConnection();
BufferedReader input = new BufferedReader(new InputStreamReader(conn.getInputStream()));

long fim = System.currentTimeMillis();
System.out.println("Tempo: " + (fim - inicio));

Gives you the time in ms.

2

Follow an example below:

    //Registrar tempo inicial:
    long time_begin = System.currentTimeMillis();

    //Declarar URL, e criar objeto do tipo URL:
    String example_your_URL = "https://jsonplaceholder.typicode.com/posts/1";
    URL url = new URL(example_your_URL);

    //Abrir conexão:
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));

    //Registrar tempo final:
    long time_end = System.currentTimeMillis();

    //Tempo gasto:
    double milliseconds = (time_end - time_begin);

    //Imprimir tempo gasto:
    System.out.println("Tempo em milissegundos: " + milliseconds+"\n");

    //Ler conteúdo:
    String content = null;
    while( (content = br.readLine()) != null) {
        System.out.println(content);
    }

Browser other questions tagged

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