0

What i am trying to achieve:

Testing the speed of download of a binary file using javascript.

I went through this link: How to detect internet speed in Javascript? and got the idea on how this can be done. Now this involves downloading the image only once and doing the calculation to get the result. Next to get better results i tried to send the repetitive ajax calls to my apache server till 10 seconds. Get the result, calculate the speed and save the result in an array. At last calculate the average of all the speeds which gives the result.

Problem is if i am using the async: false the browser becomes unresponsive while using async:true is causing the loop to go infinitely and also i am not able to figure out how calls should be made. This is the code i have written:

    var startTime = 0;
    var endTime = 0;
    var file = '';
    var allDownloadSpeeds=[];
    var timeElapsed;
    var t = new Date();
    var x=t.getTime();
    console.log(t.getHours() + "  " +  t.getMinutes() + "  " + t.getSeconds());
    t.setSeconds(t.getSeconds() + 10);
    var y=t.getTime();
    console.log(t.getHours() + "  " +  t.getMinutes() + "  " + t.getSeconds());
    var timeOfTest=y-x;

    function DownloadTest() {
        timeElapsed = 0;
        var speed;
        while(true){
            startTime = new Date().getTime();
            if(timeElapsed<timeOfTest){
              $.ajax({
                type: "GET",
                url: "file.bin?id=" + startTime,
                dataType: 'application/octet-stream',
                async:false,
                success: function(msg) {
                    binfile = msg;
                    endTime = new Date().getTime();
                    timeElapsed+=(end - startTime);
                    diff = (end - startTime) / 1000;
                    bytes = msg.length;
                    speed = (bytes / diff) / 1024 / 1024 * 8;
                    allDownloadSpeeds.push(speed);
                },            
              });
            }
            else{
                var averageSpeed=0;
                for(var i=0;i<allDownloadSpeeds.length;i++){
                   averageSpeed+=allDownloadSpeeds[i];
                }
                console.log("Speed   "  + averageSpeed/averageSpeed.length);
                               break;
            }
        }
    }

    $(document).ready(function(){
        TestDownload;
    });

I don't know how to handle asynchronous ajax calls. Thanks in advance for help.

Community
  • 1
  • 1
Prakash Kuma
  • 722
  • 1
  • 6
  • 11
  • @dm03514: Yes i tested it many times but making async:true is not giving any result...instead it causes the browser to crash. – Prakash Kuma Apr 23 '15 at 13:59
  • If you have async true, it's not waiting for one download to complete before starting the next. As a result, you might be doing thousands of ajax calls. In 10 seconds, you could call the $.ajax many many times. That's the problem. On top of that, most browsers won't allow you to make more than 4-5 ajax requests at the same time. – dmeglio Apr 23 '15 at 14:01
  • So what is the work around to this problem?? – Prakash Kuma Apr 23 '15 at 14:01
  • Well what are you trying to do - have multiple downloads at the same time, or one after the other? – dmeglio Apr 23 '15 at 14:02
  • One after another. I tried to do .then() but that also didn't worked.. I don't know how to handle ajax calls. – Prakash Kuma Apr 23 '15 at 14:03
  • The problem is going to be, you can't queue them up on the loop. You need to queue the next one, when the first succeeds, and only if you're in the timeframe. As a result, you want to call the $.ajax in your success handler, if timeElapsed – dmeglio Apr 23 '15 at 14:05
  • I am not able to get this. Can you please elaborate on this further.... – Prakash Kuma Apr 23 '15 at 14:08
  • Also how can i call the functions multiple times one after another using .then()....and also expect the browser not to become unresponsive – Prakash Kuma Apr 23 '15 at 14:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76072/discussion-between-prakash-kuma-and-dman2306). – Prakash Kuma Apr 23 '15 at 14:19

0 Answers0