19.11.16

Performance or Best Practices of Asp.net MVC Application

MVC Performance
Index:
1.)      Using Asynchronous Methods in ASP.NET MVC 4
2.)      Profile and debug your ASP.NET MVC app with Glimpse
3.)      Bundling and Minification
4.)      Using CDNs and Expires to Improve Web Site Performance
5.)      Donut Caching and Donut Hole Caching with Asp.Net MVC 4
6.)      Use Caching
7.)      Improve perceived performance of ASP.NET MVC websites with asynchronous partial views
9.)      Run your ASP.NET sites in release mode
10.)   MiniProfiler in ASP.NET MVC 4
12.)   Use AJAX
13.)   If using only Razor in MVC
14.)   Put style sheets at the top and scripts at the bottom
15.)   Make JavaScript and CSS External
16.)   Optimizing images
17.)   Avoid Empty Image src
18.)   Replaced Json.Net with a faster library (e.g. ServiceStack)
19.)   Use caching and lazy loading in a smart way
20.)   Do not put C# code in your MVC views
21.)   Use Fire & Forget when applicable
22.)   Use monitoring and diagnostic tools on the server

Summary

1.)      Using Asynchronous Methods in ASP.NET MVC 4
The ASP.NET MVC 4 Controller class in combination .NET 4.5  enables you to write asynchronous action methods that return an object of type  Task.
Making these web service calls asynchronous will increase the responsiveness of your application.


Why Use:
This might not be a problem, because the thread pool can be made large enough to accommodate many busy threads. However, 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.
When you’re doing asynchronous work, you’re not always using a thread. For example, when you make an asynchronous web service request, ASP.NET will not be using any threads between the async method call and the await. 

Where to Use:
You're calling services that can be consumed through asynchronous methods, and you're using .NET 4.5 or higher.
Parallelism is more important than simplicity of code.
You want to provide a mechanism that lets users cancel a long-running request

To realize the benefits of an asynchronous web application, you might need to make some changes to the default server configuration. You might need to increase the HTTP.sys queue limit from the default value of 1,000 to 5,000. See view Link for whole Configuration.


2.)      Profile and debug your ASP.NET MVC app with Glimpse
While Fiddler and the F-12 development tools provide a client side view, Glimpse provides a detailed view from the server. You can install Glimpse from the NuGet package manager console or from the Manage NuGet Packages console.

3.)       Bundling and Minification
Why use: Most of the current major browsers limit the number of simultaneous connections per each hostname to six. That means that while six requests are being processed, additional requests for assets on a host will be queued by the browser.

Bundling is a new feature in ASP.NET 4.5 that makes it easy to combine or bundle multiple files into a single file. You can create CSS, JavaScript and other bundles. Fewer files means fewer HTTP requests and that can improve first page load performance.
Minification performs a variety of different code optimizations to scripts or css, such as removing unnecessary white space and comments and shortening variable names to one character

Bundle Caching: Bundles set the HTTP Expires Header one year from when the bundle is created.

4.)      Using CDNs and Expires to Improve Web Site Performance
Content Delivery Network (CDN) to deliver static content ( jQuery, images, CSS files, etc.)
If another site has loaded your jQuery version from the Microsoft CDN, and your web site requests that version from the Microsoft CDN, the client cache can satisfy the request, eliminating the download cost.
Each browser has its own cache, so Firefox won’t use files cached by Chrome or IE.


5.)      Donut Caching and Donut Hole Caching with Asp.Net MVC 4
Donut caching is the best way to cache an entire web page except for one or more parts of the web page
Donut Caching was introduced which cached only one copy of the entire page for all the user except for a small part which remain dynamic.
Why Use: If you want to cache all these pages for all the users by using Output Cache with VaryByParam UserID, then the entire page would be cached every time for each user with a different user name (or whatever your dynamic part of the page is). This is not a good practice since there will be 1000 cached pages if there are 1000 logged in user at a time.

6.)      Use Caching:
The output of the Index () action method will be cached for 20 seconds. If you will not have defined the duration, it will cache it for by default cache duration 60 sec.
[OutputCache (Duration=20, VaryByParam="none")]

7.)      Improve perceived performance of ASP.NET MVC websites with asynchronous partial views
Moved the CSHTML responsible for rendering each slow section to a partial view if it was not already done.
Use jQuery to find and call. load () for each section.

Asynchronous database calls are not as straight forward as other types of asynchronous operations but sometimes it will gain so much more responsiveness to our applications. We just need to get it right and implement them properly.

9.)      Run your ASP.NET sites in release mode

10.)   MiniProfiler in ASP.NET MVC 4
MiniProfiler is a "simple but effective" profiler you can use to help find performance problems and bottlenecks.
Url: http://odetocode.com/Blogs/scott/archive/2012/09/11/elmah-and-miniprofiler-in-asp-net-mvc-4.aspx

While developing WEB Applications, when you have a requirement to retrieve data from multiple services at the same time, then it is a good idea to use Asynchronous action methods in .NET 4.5. This will make your code less complex and easy to maintain.
Url: http://www.devcurry.com/2013/04/aspnet-mvc-4-making-asynchronous-calls.html
12.)  Use AJAX
Use AJAX to update components of your UI, avoid a whole page update when possible.

13.)   If using only razor in mvc
 If you use Razor, add the following code in your global.asax.cs, by default, Asp.Net MVC renders with an aspx engine and a razor engine. This only uses the RazorViewEngine.
ViewEngines.Engines.Clear();
ViewEngines. Engines. Add.  (new RazorViewEngine ());

14.)   Put style sheets at the top and scripts at the bottom

Url: http://thomasardal.com/yslow-rule-5-and-6-%E2%80%93-put-style-sheets-at-the-top-and-scripts-at-the-bottom/

15.)   Make JavaScript and CSS External
Using external files in the real world generally produces faster pages because the JavaScript and CSS files are cached by the browser.

16.)   Optimizing images
One way to solve this is to reduce the size of the images or in other words, optimize the images. 

The following VSextension will help to optimize the images from the Visual Studio IDE itself,

17.)   Avoid Empty Image src
Otherwise, the browser makes another request to your server.
18.)   Replaced Json.Net with a faster library (e.g. ServiceStack)
https://aspguy.wordpress.com/2014/02/17/building-high-performance-asp-net-applications/

19.)   Use caching and lazy loading in a smart way
Another approach for improving the responsiveness of your site is using Lazy Loading. Lazy Loading means that an application does not have a certain piece of data, but it knows that where is that data.

20.)   Do not put C# code in your MVC views
if you include too much C# code in them, your code will not be compiled and placed in DLL files. Not only this will damage the testability of your software but also it will make your site slower because every view will take longer to get display (because they must be compiled).
21.)   Use Fire & Forget when applicable
if users can sign-up and create an account in your web site, and once they register you save their details in the database and then you send them an email, you don’t have to wait for the email to be sent to finalize the operation.
In such a case the best way of doing so is probably starting a new thread and making it send the email to the user and just get back to the main thread. This is called a fire and forgets mechanism which can improve the responsiveness of an application.
22.)   Use monitoring and diagnostic tools on the server
There might be many performance issues that you never see them by naked eyes because they never appear in error logs. Identifying performance issues are even more daunting when the application is already on the production servers where you have almost no chance of debugging.
To find out the slow processes, thread blocks, hangs, and errors and so forth it’s highly recommended to install a monitoring and/or diagnostic tool on the server and get them to track and monitor your application constantly. I personally have used NewRelic (which is a SAS) to check the health of our online sites.
https://newrelic.com/





No comments: