JQuery AJAX HTTP连接

If I have a jQuery POST call that looks something like:

$.post(url : "myserver/1", data : params, success: function () { 
    $.post(url : "myserver/2", data : params2); 
});

How many HTTP Connections would this be opening? I'm assuming it opens 2.

I'm trying to replicate what a webpage would do in a Python script (as a performance testing type script). It's much faster if I do: (pseudo code)

for i in 0 to 5000:
     Open HTTP Connection
     POST
     POST
     Close HTTP Connection

vs

for i in 0 to 5000:
     Open HTTP Connection
     POST
     Close
     Open HTTP Connection
     POST
     Close

I assume the second test is what the browser would be doing. I essentially want to replicate how fast 5000 hits (back to back) to the page would be. Obviously I could parallelize this task -- but that's not what I'm looking for here

Now, assuming jquery is making 2 HTTP connections, is there an easy method of maintaining a persistent connection?

jQuery can't control the number of connections - the browser opens as many as it likes (typically 2-8) and then pushes your request through them. Your code would probably only open one connection, since you are waiting for the first request to finish before you initiate the second request.

You can't use threads in javascript of course, but you can initiate as many requests as you like at once and this can max out the number of connections allowed by your browser.

You probably want something like:

var CONCURRENCY=4, qty = 5000, cnt = 0
function testOnce() {
  cnt = cnt + 1;
  if (cnt >= qty) { return; }
  $.post(url : "myserver/" + cnt, data : params, success: testOnce)
}
for (var i=0; i<CONCURRENCY; i++) {
  testOnce()
}

Even this way, you are only going to open a few connections and send all your requests through those connections. That won't really give your server application the beating you are looking for, including the tcp set-up and tear-down. Browsers are specifically design to avoid hammering any one server. If you make too many requests, they will wind up waiting for an available connection in your browser's network manager.

If you disable http keep-alive on the server, you'll get closer, but that might not be possible or might otherwise influence the results. With it disabled, each request will be forced to establish a new connection. But that's unrealistically pessimistic. It all depends. Realistic load testing isn't that easy. But often you don't need it to be that realistic - what is it you really want to test?

Wireshark will show you the nitty-gritty of what's really going on.