By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,694 Members | 1,622 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,694 IT Pros & Developers. It's quick & easy.

Download speed test

P: 11
Hi I am trying to measure download speed from server with ajax call and xmlwebrequest, I download a 1 mb file and check the total time. However it shows just response time from server and doesn't wait until all file loaded for 2 methods I showed below. What am I doing wrong? Any other ways are welcome. Thanks...

First method I am tring is

Expand|Select|Wrap|Line Numbers
  1. var start = new Date();
  2. $.ajax ({
  3. url: '',
  4. cache: false,  
  5. success : function()
  6. {
  7.  var total=(new Date() - start)
  8.  alert(total)    
  9. },
  10.  error : function(jqxhr, status, ex) {
  11. })
Second method is

Expand|Select|Wrap|Line Numbers
  1. var start = new Date();
  2.  (function rec() { var start = new Date().getTime();
  3.  var xmlHttp = new XMLHttpRequest();
  4.'GET', "",true);   
  5.  xmlHttp.setRequestHeader("Cache-Control", "no-cache");
  6.  xmlHttp.onreadystatechange = function () {
  7.  if (xmlHttp.readyState == 4) {
  8.    var total=(new Date() - start)            
  9.   alert(total)          
  10. };
  11.  }; xmlHttp.send(null);
  12. })();
Jan 14 '13 #1
Share this Question
Share on Google+
8 Replies

P: 185
You may try using an image. An image has a property onload that should help you on this. For example:
Expand|Select|Wrap|Line Numbers
  1. var img = document.getElementById('myimg');
  3.     img.onload = function () {
  4.         alert ("The image has been loaded!");        
  5.     };
Please let us know the results you got, hope it can help.
Jan 14 '13 #2

P: 11
I want to measure download speed not for one server. There will be 3 regions that client can measure download speed from these 3 servers located in these regions. How can I use this image onload property for different servers?
Jan 14 '13 #3

P: 185
I was trying to answer the question about knowing how much does it take to load the object. If your object is a files, the above method is perfect for you. If you have another method, I don;t know this can be implemented.
Jan 14 '13 #4

P: 11
Sorry I am not good for web development and my question with your method how can I load pictures from different servers. for example your code loads 'myimg' which is located at same place with the webpage.
Expand|Select|Wrap|Line Numbers
  1. var img = document.getElementById('myimg');
I use at my methods different URL's to call the files in different servers. Could you give advice how to do that with images. Thanks for your answer...
Jan 14 '13 #5

P: 185
For example you'll create a div with id container to hold the images. From JavaScript you can add images ass follows:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function xLoad() {
  3.     alert('Image was loaded ...');
  4. }
  6. document.getElementById('container').innerHTML = '<img src="" onload="xLoad()" />'
  7. </script>
You may style the container div to be hidden if you don't want it to be visible for the user.
Jan 14 '13 #6

P: 11
That is perfect. I will give a try and let you know about result. Thank you very much...
Jan 14 '13 #7

P: 185
I appreciate if you can share with us the solution you adopted.
Jan 19 '13 #8

P: 11
Hi Anas, I tried your solution to measure bandwidth test, It works perfect. Thank you.
Moreover I found something with my code I mentioned above, at the ajax call when I set async:false, anymore it waits to download whole file what was my problem, so I am able to measure download speed with this method as well. However internet explorer and firefox is still the same. chrome, safari works well now. Do you have any idea why it is happening?
Thanks again...

Edit: I have asked this at another question.
Jan 21 '13 #9

Post your reply

Sign in to post your reply or Sign up for a free account.