How many concurrent AJAX (XmlHttpRequest) requests are allowed in popular browsers?

后端 未结 8 1346
无人共我
无人共我 2020-11-21 11:37

In Firefox 3, the answer is 6 per domain: as soon as a 7th XmlHttpRequest (on any tab) to the same domain is fired, it is queued until one of the other 6 finish.

Wha

相关标签:
8条回答
  • 2020-11-21 11:54

    I have writen a single file AJAX tester. Enjoy it!!! Just because I have had problems with my hosting provider

    <?php /*
    
    Author:   Luis Siquot
    Purpose:  Check ajax performance and errors
    License:  GPL
    site5:    Please don't drop json requests (nor delay)!!!!
    
    */
    
    $r = (int)$_GET['r'];
    $w = (int)$_GET['w'];
    if($r) { 
       sleep($w);
       echo json_encode($_GET);
       die ();
    }  //else
    ?><head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
    
    var _settimer;
    var _timer;
    var _waiting;
    
    $(function(){
      clearTable();
      $('#boton').bind('click', donow);
    })
    
    function donow(){
      var w;
      var estim = 0;
      _waiting = $('#total')[0].value * 1;
      clearTable();
      for(var r=1;r<=_waiting;r++){
           w = Math.floor(Math.random()*6)+2;
           estim += w;
           dodebug({r:r, w:w});
           $.ajax({url: '<?php echo $_SERVER['SCRIPT_NAME']; ?>',
                   data:    {r:r, w:w},
                   dataType: 'json',   // 'html', 
                   type: 'GET',
                   success: function(CBdata, status) {
                      CBdebug(CBdata);
                   }
           });
      }
      doStat(estim);
      timer(estim+10);
    }
    
    function doStat(what){
        $('#stat').replaceWith(
           '<table border="0" id="stat"><tr><td>Request Time Sum=<th>'+what+
           '<td>&nbsp;&nbsp;/2=<th>'+Math.ceil(what/2)+
           '<td>&nbsp;&nbsp;/3=<th>'+Math.ceil(what/3)+
           '<td>&nbsp;&nbsp;/4=<th>'+Math.ceil(what/4)+
           '<td>&nbsp;&nbsp;/6=<th>'+Math.ceil(what/6)+
           '<td>&nbsp;&nbsp;/8=<th>'+Math.ceil(what/8)+
           '<td> &nbsp; (seconds)</table>'
        );
    }
    
    function timer(what){
      if(what)         {_timer = 0; _settimer = what;}
      if(_waiting==0)  {
        $('#showTimer')[0].innerHTML = 'completed in <b>' + _timer + ' seconds</b> (aprox)';
        return ;
      }
      if(_timer<_settimer){
         $('#showTimer')[0].innerHTML = _timer;
         setTimeout("timer()",1000);
         _timer++;
         return;
      }
      $('#showTimer')[0].innerHTML = '<b>don\'t wait any more!!!</b>';
    }
    
    
    function CBdebug(what){
        _waiting--;
        $('#req'+what.r)[0].innerHTML = 'x';
    }
    
    
    function dodebug(what){
        var tt = '<tr><td>' + what.r + '<td>' + what.w + '<td id=req' + what.r + '>&nbsp;'
        $('#debug').append(tt);
    }
    
    
    function clearTable(){
        $('#debug').replaceWith('<table border="1" id="debug"><tr><td>Request #<td>Wait Time<td>Done</table>');
    }
    
    
    </script>
    </head>
    <body>
    <center>
    <input type="button" value="start" id="boton">
    <input type="text" value="80" id="total" size="2"> concurrent json requests
    <table id="stat"><tr><td>&nbsp;</table>
    Elapsed Time: <span id="showTimer"></span>
    <table id="debug"></table>
    </center>
    </body>
    

    Edit:
    r means row and w waiting time.
    When you initially press start button 80 (or any other number) of concurrent ajax request are launched by javascript, but as is known they are spooled by the browser. Also they are requested to the server in parallel (limited to certain number, this is the fact of this question). Here the requests are solved server side with a random delay (established by w). At start time all the time needed to solve all ajax calls is calculated. When test is finished, you can see if it took half, took third, took a quarter, etc of the total time, deducting which was the parallelism on the calls to the server. This is not strict, nor precise, but is nice to see in real time how ajaxs calls are completed (seeing the incoming cross). And is a very simple self contained script to show ajax basics.
    Of course, this assumes, that server side is not introducing any extra limit.
    Preferably use in conjunction with firebug net panel (or your browser's equivalent)

    0 讨论(0)
  • 2020-11-21 11:56

    According to IE 9 – What’s Changed? on the HttpWatch blog, IE9 still has a 2 connection limit when over VPN.

    Using a VPN Still Clobbers IE 9 Performance

    We previously reported about the scaling back of the maximum number of concurrent connections in IE 8 when your PC uses a VPN connection. This happened even if the browser traffic didn’t go over that connection.

    Unfortunately, IE 9 is affected by VPN connections in the same way:

    0 讨论(0)
  • 2020-11-21 11:57

    Wrote my own test. tested the code on stackoverflow, works fine tells me that chrome/FF can do 6

    var change = 0;
    var simultanius = 0;
    var que = 20; // number of tests
    
    Array(que).join(0).split(0).forEach(function(a,i){
        var xhr = new XMLHttpRequest;
        xhr.open("GET", "/?"+i); // cacheBust
        xhr.onreadystatechange = function() {
            if(xhr.readyState == 2){
                change++;
                simultanius = Math.max(simultanius, change);
            }
            if(xhr.readyState == 4){
                change--;
                que--;
                if(!que){
                    console.log(simultanius);
                }
            }
        };
        xhr.send();
    });
    

    it works for most websites that can trigger readystate change event at different times. (aka: flushing)

    I notice on my node.js server that i had to output at least 1025 bytes to trigger the event/flush. otherwise the events would just trigger all three state at once when the request is complete so here is my backend:

    var app = require('express')();
    
    app.get("/", function(req,res) {
        res.write(Array(1025).join("a"));
        setTimeout(function() {
            res.end("a");
        },500);
    });
    
    app.listen(80);
    

    Update

    I notice that You can now have up to 2x request if you are using both xhr and fetch api at the same time

    var change = 0;
    var simultanius = 0;
    var que = 30; // number of tests
    
    Array(que).join(0).split(0).forEach(function(a,i){
        fetch("/?b"+i).then(r => {
            change++;
            simultanius = Math.max(simultanius, change);
            return r.text()
        }).then(r => {
            change--;
            que--;
            if(!que){
                console.log(simultanius);
            }
        });
    });
    
    Array(que).join(0).split(0).forEach(function(a,i){
        var xhr = new XMLHttpRequest;
        xhr.open("GET", "/?a"+i); // cacheBust
        xhr.onreadystatechange = function() {
            if(xhr.readyState == 2){
                change++;
                simultanius = Math.max(simultanius, change);
            }
            if(xhr.readyState == 4){
                change--;
                que--;
                if(!que){
                    document.body.innerHTML = simultanius;
                }
            }
        };
        xhr.send();
    });

    0 讨论(0)
  • 2020-11-21 12:01

    One trick you can use to increase the number of concurrent connections is to host your images from a different sub domain. These will be treated as separate requests, each domain is what will be limited to the concurrent maximum.

    IE6, IE7 - have a limit of two. IE8 is 6 if you have a broadband - 2 (if it's a dial up).

    0 讨论(0)
  • 2020-11-21 12:03

    With IE6 / IE7 one can tweak the number of concurrent requests in the registry. Here's how to set it to four each.

    [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]
    "MaxConnectionsPerServer"=dword:00000004
    "MaxConnectionsPer1_0Server"=dword:00000004
    
    0 讨论(0)
  • 2020-11-21 12:04

    The network results at Browserscope will give you both Connections per Hostname and Max Connections for popular browsers. The data is gathered by running tests on users "in the wild," so it will stay up to date.

    0 讨论(0)
提交回复
热议问题