2

I'm using jQuery to benchmark the time API Requests take. For this, i'll run the same request repeated in a specific interval. Everything runs just fine.

BUT if I reduce the interval to be significant lower than the time the API Request takes to return, the benchmarked time will add up.

Try to decrease the Interval in the JSBin Example to 100 and you'll see what I mean:

Reduced JSBin Example: http://jsbin.com/azuqUho/4/edit

I've visualized this here: Interval high enough Interval to low

Since I can't pass the now variable to the success function of getJSON, i'm not sure how to solve this problem.

Any suggestions?

Thanks, Simon

Satpal
  • 132,252
  • 13
  • 159
  • 168
Fannon
  • 383
  • 3
  • 14
  • You can only have 1 active ajax call running at a time. You shouldn't be using intervals to run the call. It should just call itself again in the callback, and then stop after *n* iterations. – Reinstate Monica Cellio Sep 20 '13 at 10:19
  • Thats not easy in my case because I have multiple targets aswell as iterations. And I couldn't create a scenario where the server has several requests at the same time (which is a common case) – Fannon Sep 20 '13 at 11:31
  • @Archer not exactly right. There can be more then one. But its limited. I had 5 in mind. But as I see 6 is usual. Depends on browser and browser config (for example, IE8 handle 6 requests at same time when on with broadband, 2 if dial up) Also see my answer where I only allow 5 connections at time, then the benchmark works correctly. – nbar Sep 20 '13 at 12:22
  • @nbar - thanks for the clarification. I've had issues in the past where I could only execute 1 ajax call at a time, so I guess something else was causing the problem. – Reinstate Monica Cellio Sep 20 '13 at 12:52

2 Answers2

1

its the browsers connection limit

A browser only do x requests at a time. If you run the intervall too fast, then the requests that runs parallel is greater then x. Lets say x=5 and you run 6 requests. The first 5 requests will return nearly at the same time, the 6. request will take ~double the time (it have to wait until one of the first 5 request is finished)

To prove it I do only 5 requests at a time if(requests<6) { and we see the time stays the same http://jsbin.com/azuqUho/12/edit

nbar
  • 6,028
  • 2
  • 24
  • 65
  • Ok, thats interesting! Havent thought about the connection limit! So it is the browser that is "delaying" the requests and the measured time is actually correct? Thanks! – Fannon Sep 20 '13 at 12:24
  • @Fannon yes its the browsers connection limit that delaying the requests. Would be nice if you accept the answer :) – nbar Sep 20 '13 at 12:43
  • 1
    @Fannon that could be interessting: http://stackoverflow.com/a/985704/2481955 http://stackoverflow.com/a/14768266/2481955 Would be nice to write a function to determinate the connection limit – nbar Sep 20 '13 at 13:21
0

Using a servlet to do the benchmark would avoid counting the waiting time due to the limit of requests that can be sent by the browser.

Michael Laffargue
  • 10,116
  • 6
  • 42
  • 76