Showing posts with label Asynchronous MVC 5. Show all posts
Showing posts with label Asynchronous MVC 5. Show all posts

Wednesday 30 September 2015

Async/Await in MVC 5 and Performance test


Asynchronous feature in MVC 5 


No one wants to develop a lazy loading  website and unresponsive application , but it takes some time to go with maximum performance .To use the latest , efficient techniques and features available will make us help in  building the robust application .  

Asynchronous  feature is one of the important feature used to develop a maximum utilization of the threads running in thread pool in server side .


Comparison of Traditional way of Approach and Modern way:


Traditional Approach  : 

In this approach , the threads in the thread pool will be blocked until there is a request processed completely .It means the server takes more time to process a request for database operations request will block the thread until the response comes .



Here each request is associated with one different thread and blocks the thread .
In real scenario if there are more request comes , then if all threads are blocked for some reasons then for new request to process it will take time until there is a free thread to associate .

Modern Approach  :


When request comes , the request is first associated with the thread in the thread pool ,
but if the request take more time for its database operation then the thread is released.
Once the entire operation is done, the request will again assiciate the thread and fulfills the request .

The advantage of using the asynchronous feature is when there is delay in the database operations to perform when request comes . Threads will de-associate then the released threads may be used by other request which comes in but where as in traditional approach there is a blockage for the thread which deceases the performance .

Here is the benchmark test for testing the performance  : 

Note  : The test is dependent on the machine you run.




Traditional Approach :

To server 1000 request at particular time , the time taken to finish all the requests is766 ms.












Modern Approach :

To server 1000 request at particular time , the time taken to finish all the requests is 366 ms.














Asynchronous Programming in MVC 5 

Controller method to perform asynchronously :

//Traditional Way of approach , using this the thread will be blocked until the request is fulfilled.
 public  ActionResult Traditional()
        {
            var listofrecords =  entities.ToList();
            return View(listofrecords);
        }


//Modern Way of approach , in this case the thread is not blocked instead the thread is released if there is a long processing for Database operations to perform .

 public async Task<ActionResult> ModernApproach()
        {          
            var listofrecords = await entities.ToListAsync();
                return View(listofrecords);
        } 

To have a website to be load faster and with minimum usage of the resources and traffic .
I recommend to use this feature for your upcoming projects.