- Incoming browser requests are mapped to controller actions
- A controller action might return a View
- View handles the display of the data
- Most often the Views are created from the ViewModel data
How to Improve View Performance ?
Tip 01 : Run in Release Mode
- You should always make sure that your application is Compiled in Release Mode
- Your Web.config file Must Looks Like below on Production
<compilation debug="false" targetFramework="4.0">
<assemblies>
</assemblies>
</compilation>
Why This Needs ?
Why This Needs ?
# MVC Will Not do any View Look-up Caching if you are running your application
in Debug Mode
in Debug Mode
Tip 02 : Use only the View Engines that you need
- MVC framework supports having multiple view engines
- Can Configured simultaneously in your application
- And will query each in turn when it is trying to find a View
- In MVC 3 we register two view engines by default (WebForms and Razor)
- There is no reason to pay the performance price for something you are not using
- So make sure you specify only the view engines you need in your Global.asax file
Global.asax file Looks Like below :
protected void
Application_Start()
{
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());//add razor view engine
Tip 03 : Avoid passing null ViewModels to Views
When it Happens ?
- You pass in a null ViewModel to a View that uses strongly-typed html helpers
Such as in the View :
@model PawLoyalty.ViewModels.Product
@{
@Html.TextBoxFor(m =>
m.ProductName);
}
- This frequently happens in Insert (Add) scenarios
- Strongly-typed html helpers like above,
- Will try to emit the value of the ViewModel using the provided expression
- However when something along the expression chain is null a NullReferenceException will be thrown,
- When the expression gets evaluated,
- MVC’s expression evaluator catches the exception
- But on a page with multiple such html helpers the cost of the exception adds up
- You can avoid that cost by always passing an instance of the ViewModel to the View
Bad Way :
[HttpGet]
public ActionResult Add() //get
empty data for user input operation
{
return
View(); //here the
model instance defaults to null
}
[HttpGet]
public ActionResult Add()//get
empty data for user input operation
{
return
View(new Product());//here the ViewModel Product has been sent
}
When it Happens :
- Your IIS server has the URL Rewrite module installed
- When you are running ASP.NET MVC 3,
- None of the applications on the server are using it
What is URL Rewrite ?
# Enables Web administrators to create powerful rules to implement URLs
# That are easier for users to remember and easier for search engines to find
Why is it Bad ?
How to Uninstall URL Rewrite ?
# Win 7 --> Controller Panel --> Programs and Features -->
# Enables Web administrators to create powerful rules to implement URLs
# That are easier for users to remember and easier for search engines to find
Why is it Bad ?
- When performing URL generation (e.g : Html.ActionLink)
- MVC 3 checks to see if the currently requested URL has been rewritten by the URL Rewrite module
- The act of checking need significant computing power to solve (because it involves checking server variables)
- So if you are not using URL Rewrite you should Turn it Off (Uninstall)
How to Uninstall URL Rewrite ?
# Win 7 --> Controller Panel --> Programs and Features -->
Important Note : MVC 2 always performs this check.So turning
URL Rewrite off (uninstall) will not make a difference
Tip 05 : Enabling Output Caching
What is Output Caching ?
What is Output Caching ?
- Enables you to cache the content returned by a controller action
- The same content does not need to be generated each and every time the same controller action is invoked
- Caching enables you to avoid performing redundant work on the Server
- Can apply for either individual controller action or an entire controller class
How to Do That ?
- Using [OutputCache] attribute
Lets take a simple Example
public class HomeController : Controller
{
[OutputCache(Duration
= 10, VaryByParam = "none")] //cached for 10 seconds
public ActionResult Index()
{
return
View();
}
}
- Read operations Only
When Not to Use it ?
- Writes operations.Such as Update,Delete and Inserts
- Pages which are User Specific.e.g. shopping cart page
Summery of Performance Gain Chart :
Pages/Sec - How many pages can load per second
Page Time (ms) - How long will it take to load a page
Base - Page that has not done any performance gain operations
Conclusion
- You can see that highest performance gain can get by using OutputCache operation
- But OutputCache having it's own Limitations
- I will explain more about MVC caching in My future blog post
- If your page is load under 1 second then you may not worry too much about performance gain
I hope this helps to You.Comments and feedback greatly appreciated.
Happy Coding.
Happy Coding.
MVC Related Articles
Very good.
ReplyDeleteHi Claudio,
DeleteThanks for your appreciation.
Do Keep in Touch.
like this.
ReplyDelete-Peter Ernst-
Hi Peter,
DeleteThanks for your feedback.
Do Keep in Touch.
Good article,very useful, thanks. ;)
-Henrique Adriano -
Hi Henrique,
DeleteYour welcome.
Do Keep in Touch.
Nice article and helpful.
ReplyDelete-Askar-
Hi Askar,
DeleteThanks for your response.
Do Keep in Touch.
Nice article..
ReplyDeleteHi Mukesh,
DeleteAwesome man, let me know if you have any questions...
Great piece of work
ReplyDeleteHi hacky,
DeleteThanks for your feedback.
Do Keep in Touch.
Hi,
ReplyDeleteThat is a great piece for work for the beginners.
I liked the stuff.
With Regards,
Vishnu Nadesh
Hi Vishnu,
DeleteYour welcome.
Do Keep in Touch.
Very useful article. Thanx.
ReplyDelete-smita naik-
Hi smita,
DeleteThanks for your feedback.
Do Keep in Touch.
I would add one from the top of my head:
ReplyDeleteIf you are using a remote Session provider, disable Session for controllers that do not use it,
or limit its scope with the SessionStateAttribute and System.Web.SessionState.SessionStateBehavior.
Cheers.
-Valeriano Tórtola Luis-
Hi Valeriano,
DeleteThanks for your Tip.
Do Keep in Touch.
If you are using InProc session, the Session object is just a Dictionary that is kept in memory. But, if you are using a remote session provider, like for example the one that uses SQL Server, a Session Server, or Azure Caching, every time that you load the page, the session is retrieved and deserialized, and when the page is about to exit,
Deleteit is serialized and saved. Of course, this implies a delay.
Also, if in your application the site can do multiple parallel requests, using Session won't allow the parallelism and the requests will be handled in serial fashion.
http://tech-journals.com/jonow/2011/10/22/the-downsides-of-asp-net-session-state
--Valeriano Tórtola Luis--
Thanks...
ReplyDeleteHi Saranga,
DeleteYour welcome.
Do Keep in Touch.
Thanks a lot sampath, This article is very use full. keep the good work.
ReplyDeleteHi RAZIN,
DeleteThanks for your appreciation.
Do Keep in Touch.
Very Useful article.. Thanks
ReplyDeleteHi chandima,
DeleteThanks, I’m glad you enjoyed it!
Nice Article its very useful for me
ReplyDeleteWhen testing performance, be sure to use an application performance monitoring (APM) tool so you can track page load times, slow database queries, etc for every request in your app. You can also use them to see interactions with web services, caching, queues, etc. They are really invaluable tools for performance testing and tuning!
ReplyDeleteThanks,
Matt Watson with Stackify APM (http://www.stackify.com)