Bah!
I have recently installed the MiniProfiler from http://code.google.com/p/mvc-mini-profiler/ attempting to find all the slow calls on my site. (There are quite a few. But I've fixed a lot of it as a result of the profiler)
There were two common calls that I noticed were being reported by the profiler to take over a second to return in some cases. This didn't make any sense. The profiler showed the call (in mvc itself) to take around 2-4ms but the overall call took over 1 second. I spent quite a bit of time trying to narrow down the problem.
I noticed that the subsequent ajax calls are always instant but those were on delayed loads and the initial call times seemed to always grow from the initial page load so I decided to check in firebug and IE's firebug equivalent and the reported times are the same. I decided then to delay the calls made by the page by about 10ms using a setTimeout()
. Suddenly the calls are exactly what they should be in terms of how long they take to complete. (Note the actual calls are instant but the reported times aren't)
I can not figure out why this is happening. I've tried to reproduce it using a new project and attempting to duplicate the behavior but alas, all is well in the new project. My initial thoughts were that the initial ajax calls were using the same connection due to the keep alive header, however, all the responses returned with "Closed" on the keep alive so I'm not sure that is the case.
Here are the results from the different services
Mini Profiler
Firebug
IE's firebug
Note that I'm fairly certain the difference between the page results and the first call are a result of the images being loaded for the page.
Javascript Helper function
<script type='text/javascript'>
function LoadJson(url, callback, error) {
$.ajax({
url: url,
type: "POST",
dataType: "json",
success: function (ret) {
callback(ret);
},
error: function (ret) {
//alert(url);
}
});
}
</script>
Actual call
$(function () {
LoadJson('/ajax/activeform', function (ret) {
//Do something
});
);
Any help in understanding this would be appreciated. Thanks :)