I am not so good at asynchronous programming so the question might be at the low level.
I have created an async method as below with Async CTP on ASP.NET MVC 4 Dev. Preview:
public class Movie
{
public string Title { get; set; }
public string Url { get; set; }
public string BoxArtUrl { get; set; }
}
public class MovieM {
public IEnumerable<Movie> M2009 { get; set; }
public IEnumerable<Movie> M2010 { get; set; }
public IEnumerable<Movie> M2011 { get; set; }
}
public class HomeController : AsyncController {
public async Task<ActionResult> GetMoviesM() {
var profiler = MiniProfiler.Current; // it's ok if this is null
var pageSize = 1000;
var imageCount = 0;
using (profiler.Step("Start pulling data (Async) and return it")) {
var m2009 = await QueryMoviesAsync(2009, imageCount, pageSize);
var m2010 = await QueryMoviesAsync(2010, imageCount, pageSize);
var m2011 = await QueryMoviesAsync(2011, imageCount, pageSize);
return View(new MovieM {
M2009 = m2009,
M2010 = m2010,
M2011 = m2011
});
}
}
XNamespace xa = "http://www.w3.org/2005/Atom";
XNamespace xd = "http://schemas.microsoft.com/ado/2007/08/dataservices";
XNamespace xm = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
string query = "http://odata.netflix.com/Catalog/Titles?$filter=ReleaseYear eq {0}&$skip={1}&$top={2}&$select=Url,BoxArt";
async Task<IEnumerable<Movie>> QueryMoviesAsync(int year, int first, int count) {
var client = new WebClient();
var url = String.Format(query, year, first, count);
var data = await client.DownloadStringTaskAsync(new Uri(url));
var movies =
from entry in XDocument.Parse(data).Descendants(xa + "entry")
let properties = entry.Element(xm + "properties")
select new Movie
{
Title = (string)entry.Element(xa + "title"),
Url = (string)properties.Element(xd + "Url"),
BoxArtUrl = (string)properties.Element(xd + "BoxArt").Element(xd + "LargeUrl")
};
return movies.AsEnumerable();
}
}
The code works just fine. When we run the same function on a desktop app (a WPF App for instance), we can see a tangible performance difference. The UI isn't blocked, the data is being pushed to screen instantly when it is available.
But on a web application, I really cannot see a difference. I also created the same function as sync and the both of them are nearly the same.
What I would like to know is that:
- I run this app on a machine which has Intel Core 2 Duo CPU T5750 2.00GHz. Do number of processors effect the performance of asynchronous thread on C#?
- Am I doing something wrong here from a web application point of view?