1

I watched this video : https://channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherlands/2287. So i tried to implement the usage of async/await in a controller. SO this is basicaly what I did :

 public class HomeController : Controller
    {
        private static WebClient _webClient = new WebClient();
        public async Task<ActionResult> IndexAsync()
        {
            var data = await _webClient.DownloadStringTaskAsync("http://stackoverflow.com/");
            return View("Index", (object)data);
        }
        public ActionResult Index()
        {
            var data = _webClient.DownloadString("http://stackoverflow.com/");
            return View("Index", (object)data);
        }
    }

Then I used Apache Benchmark and did the two following tests :

ab -n 100 -c 100 http://localhost:53446/Home/index

and

ab -n 100 -c 100 http://localhost:53446/Home/indexasync

And I got the exact same performance (I have 8 CPU core). Why is that ?

9
  • 1
    What kind of performance improvement were you expecting?
    – trashr0x
    Nov 10, 2016 at 15:55
  • 3
    You don't have multiple async operations running concurrently, so I would expect no speedup whatsoever.
    – user47589
    Nov 10, 2016 at 15:56
  • 2
    For a single request you are more likely to see a drop in performance. Async alleviates load by allowing more concurrent requests to be handled.
    – Ant P
    Nov 10, 2016 at 15:57
  • the -c parameter implies 100 concurrent requests and -n is the request count, so basically I'm sending 100 concurrent request to my app which will do 100 concurrent requests to SO. Nov 10, 2016 at 16:01
  • 2
    @goenning: Incorrect. Async != parallel processing. You can use Task.WhenAll to run them in parallel, but if you just await each line, they'll run serially. Nov 10, 2016 at 16:40

3 Answers 3

12

Async is not about performance. That's just categorically incorrect. In fact, an async request will often be less performant that sync, simply because there's additional overhead involved with async.

The reason to use async is about efficient resource-management and scale. A typical web server process will have around 1000 threads. This is often called the "max requests", as one thread general equals one request. If you have an 8 core CPU, you should ideally have a process per core (in IIS those are called "web workers"). So, theoretically, you'd have around 8000 threads total to work with.

That's quite a lot actually, though a modern web page consumes more requests than most people think. The page itself is one request, but that page will have images and external JS and CSS files, all of which generate a request, and will often utilize AJAX, for further requests. The point is that while 8000+ threads is still quite a lot to have in your pool, you could still very well run out if the server is under significant load.

Async merely gives you breathing room above that limit. In situations where the thread enters a wait-state, it can be returned to the pool to field other requests while whatever external action is being completed. The alternative is that the thread would just sit there idle (sync). That's really all there is to it. It's entirely about tasking those otherwise idle threads with some other bit of work, which could mean the difference between requests queuing up and timing out or being handled, even if slowly.

4
  • I agree with you, but by default Asp.Net MVC keeps 8 threads (msdn.microsoft.com/en-us/library/…), so here after the 8th request my 9th and 10th are supposed to be way slower because asp.net will wait Xms (I think it's 500, but not sure) before creating a new one. Nov 10, 2016 at 16:29
  • Your assumptions here are incorrect. There's some overhead in spinning up threads, so this setting exists as a way to front load that a certain amount. However, whether the threads are existing or created doesn't really matter, and certainly doesn't matter concerning the difference between sync and async, as that must happen either way. It's only when all the available threads are in play that you start to see any difference between sync and async, regardless of how or when they got created. Nov 10, 2016 at 16:34
  • I still I don't get how there is a performance improvement on the channel9 video. Thanks for your time Nov 10, 2016 at 16:52
  • 1
    Well, I haven't watched this video and don't have a spare hour and half to do so now, so I can't really comment on that. Nov 10, 2016 at 16:58
5

Running a load test that exhausts the thread pool is difficult to do on a local box. It's a lot easier to pretend the thread pool is exhausted by artificially restricting it, as I do in my gist:

protected void Application_Start()
{
  int workerThreads, ioThreads;
  ThreadPool.GetMaxThreads(out workerThreads, out ioThreads);
  ThreadPool.SetMaxThreads(Environment.ProcessorCount, ioThreads);
  ...
}
3

There are a couple of reasons that stand out.

From Using Asynchronous Methods in ASP.NET MVC 4

the number of threads in the thread pool is limited (the default maximum for .NET 4.5 is 5,000). In large applications with high concurrency of long-running requests, all available threads might be busy. This condition is known as thread starvation.

So, running 100 request at a time will not even begin to starve your threads.

Also, a simple GET request will run very quickly. A test that performs an action that takes multiple seconds or even minutes would bear more obvious performance gains.

6
  • The limit is very high yes, but as I understood, those 5000 threads are not added to the pool by default.The threads will be added when you ask for more and this will cause request queuing. Nov 10, 2016 at 16:09
  • I just tried with a large file (42mb) and still got the same results in both async and sync Nov 10, 2016 at 16:16
  • The improvements would be more with scalability and memory than execution speed. Does the memory footprint change between the two? What if you bump the number of request to 1000 or more? This article is pretty good at explaining how async/await allows for much less memory usage: msdn.microsoft.com/en-us/magazine/dn802603.aspx
    – davidmdem
    Nov 10, 2016 at 16:23
  • Downloading a large file doesn't really matter. The async part is in sending the request and awaiting the server's response. Once your server is consuming the response, it actively needs the thread. Nov 10, 2016 at 16:29
  • Indeed i'll try with two test servers Nov 10, 2016 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.