What I have noticed in most of the projects that I have been part of is that the Performance is considered as a least priority feature. Well its definitely not a feature, performance should be built in each feature and measured as early and frequently as possible.
I have collated few key points to fine tune any web application’s (targeting .Net and IIS but not limited to) performance.
How to enable bundling and minification in MVC: http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification
Note: This could also lead to the client not getting the latest updates when something changes, ensure that you have the version in the file name, when you update the file also change the version number.
<configuration> <system.webServer> <staticContent> <clientCache cacheControlMode="UseExpires" httpExpires="Tue, 19 Jan 2038 03:14:07 GMT" /> </staticContent> </system.webServer> </configuration>
You can read more about client cache here.
Hence always keeps JS and CSS as separate files and add them as links in the view.
Note: Best practice is to add the link to the style file at the top of the view and JS at the bottom of the view file.
5. Use Url Compression: Since IIS 7+ allows easy way of compressing the response using gzip protocol and the browser decompresses the response on the client side. This would considerable reduce the network latency while transporting data.
There are two types of compression Static and Dynamic based on the contents. JS, CSS, Images and other static contents would be part of static compression, but the views, data would be come under dynamic compression. You can enable them using the following settings in the configuration file.
<urlCompression doStaticCompression="true" doDynamicCompression="false" />
above picture shows a simple request to the webserver which implements the dynamic compression. Data transferred over the wire is 4.7 KB after decompressing on the client side the data is 45 KB .
Note: The dynamic compression puts load on the server as each request as to be compressed so use it wisely.
You can read more about setting up the compression here
6. Use Output Caching: Use output caching for regularly used views or pages which have no dynamic updates. This can be done using an attribute (OutputCache) to the action in MVC
More reading here.
7. Use Data Caching: Reduces the database or the disk i/o by caching the regularly used data to in-memory cache. There are many providers in the market and also a default one available with IIS.
8. ASP.Net Pipeline Optimization: ASP.net has many http modules waiting for request to be processed and would go through the entire pipeline even if it’s not configured for your application.
All the default modules will be added in the machine.config place in “$WINDOWS$\Microsoft.NET\Framework\$VERSION$\CONFIG” directory. One could improve the performance by removing those modules which you wouldn’t require.
<httpModules> <!--<span class="code-comment"> Remove unnecessary Http Modules for faster pipeline </span>--> <remove name="Session" /> <remove name="WindowsAuthentication" /> <remove name="PassportAuthentication" /> <remove name="AnonymousIdentification" /> <remove name="UrlAuthorization" /> <remove name="FileAuthorization" /> </httpModules>
9. Avoid Session State: Session states should always be kept really small in size, if we cannot avoid the circumstances, then we should use session as distributed in-memory cache. Never use a database backed session provider.
11. Compile in Release mode: Always set the build configuration to release mode for the website. For obvious reasons.
12. Turn Tracing off: Tracing is a good functionality, but each of the functionality would add an overhead instead use the asynchronous logging mechanism.
13. Async and Await: Since the async controllers are available since the MVC3.0 now we can have non-blocking requests to the webserver which improves the throughput of the requests made. You can read more about this @ http://www.campusmvp.net/blog/async-in-mvc-4.
14. HTTP Limitations: By default HTTP protocol doesn’t allow more than two concurrent requests from the same user and those requests are also limited by the browsers
Firefox 2: 2 Firefox 3+: 6 Opera 9.26: 4 Opera 12: 6 Safari 3: 4 Safari 5: 6 IE 7: 2 IE 8: 6 IE 10: 8 Chrome: 6
But there are scenarios where you would a webserver connecting to a webservice requesting data frequently then this restriction can degrade the performance. .Net has a way to overcome this restriction and allow users to make multiple concurrent calls to the service.
<system.net> <connectionManagement> <!-- Add address from the trusted connection only --> <add address="*" maxconnection="100" /> </connectionManagement> </system.net>
Some of the tools worth mentioning
b. Chrome Inspector – Chrome browser’s audit tab allows to run the checks for performance.
c. Firebug for firefox, IE’s F12 window and Chrome’s element inspector could be used to track the network utilization and track all the http request made and can gauge which needs your attention.
d. Net Profiler – Available with Visual studio ultimate
e. ANTs profiler
f. For Entity framework there is ayande profiler http://hibernatingrhinos.com/Products/EFProf