Search code examples
javatimejsouppageloadmeasure

Measure the time it takes to download page/images/... using JSoup


I'm making a basic 'webmonitoring tool' with java, and i'm using the library jsoup. Everything works fine, but the problem is that I can't figure out when I have actually (down)loaded something. Take this for example;

Long start = System.nanoTime();
Document doc = Jsoup.connect("http://hln.be").get();
Long end = System.nanoTime();
long elapsedTime = end - start;
double seconds = (double) elapsedTime / 1000000000.0;

Would this be an accurate way of knowing how long it took to completely (down)load the page? The definition of the get()-method is; Execute the request as a GET, and parse the result.

So I guess this would be what I'm looking for, or is there a better way?

Same question but for images only; how do know I have downloaded all the images without actually saving them on my pc? I'm currently doing something like this; I have a list of all the image url's, and then in a for loop I go

url = new URL(element.absUrl("src"));
in = url.openStream();
reader = new BufferedReader(new InputStreamReader(is));

After I opened the stream and read it with the BufferedReader, does it mean I 'downloaded' them? If so I can just measure the time it takes to excecute those 3 lines to know how long it takes to download an image?

I hope I'm making myself clear.


Solution

  • You can just use the execute() method, which just fetches the page and does not parse the response.

    E.g.

    Document doc = Jsoup.connect("http://hln.be").execute();