billtian
billtian

Reputation: 6851

about measuring bandwidth with javascript

I am trying to inject a java script snippet into the webpage. In the java script, I try to measure the total amount of data that is downloaded for this webpage, then the data size will be divided by page load time to calculate the bandwidth.

Does anyone know how to measure how much data is downloaded in the complete event in java script?

The most suggested way to measure bandwidth is to download a file purposely to measure which I don't want to use.

Thanks a lot

Upvotes: 0

Views: 1427

Answers (2)

Xotic750
Xotic750

Reputation: 23502

You could possibly take a look at the Navigation Timing API to get page load timings and count the characters in the page to give you the size in bytes, this of course will not give you the true bandwith used as it does not inclue the overhead of the tcp traffic headers. And of course you will have to take the sizes of other objects into account, such as images etc. Just some thoughts off the top of my head.

A similar question was asked previously on stackexchange:

How to get current page size in KB using just javascript?

Upvotes: 0

Joseph
Joseph

Reputation: 119887

The most suggested way to get the bandwidth (both upload and download) is to download/upload a file of known size, take the difference of the start time and end time, then divide the size by the time it took to download/upload it.

The most suggested way is also the easiest way since:

  1. you know the size of the file
  2. you can get the time it takes to download
  3. and you have control of the time when it starts and finishes

Normally you can. But the problem with gauging the speed using the resources loaded on the current page is that you need to hook into every resource, which includes (but not limited to):

  • images
  • scripts
  • stylesheets
  • frames
  • XHR (AJAX)
  • sockets
  • the page itself
  • and other stuff I missed.

Pages load resources in a combination of sequential and parallel requests which start and end at varying points in time. Some may even delay loading using loader scripts, which at the time you try to check, might not have responded yet.

Also, if you use this script, this script must load way before everything to hook into everything on the page. Another problem here is that not all resources have a reliable onload event, especially the link element.

I suggest you stick to the easy method.

Upvotes: 1

Related Questions