I have set-up the examples from this MSDN article Using Asynchronous Methods in ASP.NET MVC 4 and have done some benchmarking to see what I come up with.
Server Configuration:
- Windows 7 Professional (x64)
- IIS 7.5
- Intel Core i7-2600S @ 2.80HGz
- 8GB Memory
- AppPool > Maximum Worker Processes: 10
I set-up 2 controllers Sync
and Async
and did some testing using a loader tool for benchmarking. The loader tool just sends 50-60 constant requests for one minute. Each controller is calling the same webservice 3 times. The code for each is below:
SYNC:
public ActionResult Sync()
{
var g1 = GetGizmos("url1");
var g2 = GetGizmos("url2");
var g3 = GetGizmos("url3");
return Content("");
}
public object GetGizmos(string uri)
{
using (WebClient webClient = new WebClient())
{
return JsonConvert.DeserializeObject(
webClient.DownloadString(uri)
);
}
}
ASYNC:
public async Task<ActionResult> Async()
{
var g1 = GetGizmosAsync("url1");
var g2 = GetGizmosAsync("url2");
var g3 = GetGizmosAsync("url3");
var a1 = await g1;
var a2 = await g2;
var a3 = await g3;
return Content("");
}
public async Task<object> GetGizmosAsync(string uri)
{
using (HttpClient httpClient = new HttpClient())
{
var response = await httpClient.GetAsync(uri);
return (await response.Content.ReadAsAsync<object>());
}
}
First question, does anyone know why Async is taking longer, running less and causing timeouts, whereas sync version isn't? I would think using Async for this would be faster, no timeouts, etc. Just doesn't seem right, am I doing something wrong here? What can be done to improve/fix it?
Second question, Using WebRequests in general, is there a way to speed that up? I have set the following in my global.asax
but still unsure if the usage is correct:
System.Net.ServicePointManager.DefaultConnectionLimit = 1000;
Also, any other suggestions to help speed up an application performing these types of tasts will be very helpful.