Asp.net performance

23
Simple steps to improve performance of ASP.NET Application Brij Bhushan Mishra

Transcript of Asp.net performance

Page 1: Asp.net performance

Simple steps to improve performance of ASP.NET Application

Brij Bhushan Mishra

Page 2: Asp.net performance

Agenda Introduction Improving ASP.NET Website Performance Development Tips Some more development tips Configuration Tips Some more Configurations Tips Conclusion

Page 3: Asp.net performance

About Me Brij Bhushan Mishra Microsoft ASP.NET/IIS MVP Former CodeProject MVP Blogger/Author/Speaker

http://brijbhushan.net @brij_bhushan [email protected]

Page 4: Asp.net performance

ASP.NET Pages Keep Page size small

Include all the referred files (CSS, JS, images) Move CSS/Javascript to separate files Remove HTML comments, Extra spaces Ensure all are cached at Clients

Use isPostback judicially

Page 5: Asp.net performance

ASP.NET Pages Check your PageSource and see the

__VIEWSTATE variable Minimize ViewState – Turn off if not using ASP.NET 4 : Enable ViewState at Control Level

Controls got a new property ViewStateMode Enabled Disabled Inherit

Demo

Page 6: Asp.net performance

ASP.NET Pages Add Caching Wherever appropriate

Output Fragment Cache API

Avoid using Application variable, use cache instead. Cache is robust and provides a wide list of options for intelligently managing application level data

Page 7: Asp.net performance

Paging at Database level Most of us do paging at Application level. It could be hazardous if you have large

amount of data Optimize the paging based on your

requirement and data

Page 8: Asp.net performance

Lazy Loading is not a Silver Bullet Lazyloading is good but not every time. Blindly enabling lazy loading and using it

could degrade the performance. You might end up hitting database again and again that could be easily loaded in single database hit.

Page 9: Asp.net performance

Use Ajax calls judiciously Avoid UpdatePanels Prefer ClientSide Ajax (True Ajax)

Demo

Page 10: Asp.net performance

Use SQL profiler to check the hits Run the SQL profiler when access your page. You might be unknowingly hitting database

several times. It’s very common using any ORM tool like Entity Framework

This is one of the main reason of slow applications

Page 11: Asp.net performance

Use using to avoid Memory Leaks If a Type implements IDisposable then it is a

right candidate to use with using statement. While leaving the using block, Dispose method

will be itself called.

Page 12: Asp.net performance

Fixed the Width and Height <img /> If you know the width and height of an image.

Provide it before hand else your browser will keep guessing until it get downloaded.

Page loads faster because browser assigns the required areas to image before it gets downloaded

Page 13: Asp.net performance

Understand Loops Best loop For Next Foreach (IEnumerable) Linq loops

Page 14: Asp.net performance

String, StringBuilder, String.join For static strings, use string. For string manipulation, StringBuilder should

be used. The StringBuilder class starts with a default initial

capacity of 16 If you know the max size that will be taken care by

stringbuilder, create you object likeStringBuilder sb = new StringBuilder(2000);

StringBuilder is not best in every case If you have already set of fixed set of string to

concatenate, use string.join

Page 15: Asp.net performance

Image Sprites Have lots of images and paying an

HTTPRequest for each of them on your page? ASP.NET provides a solution that is available

via Nuget Package Run the Nuget package - Install-Package

AspNetSprites-WebFormsControl

Demo

Page 16: Asp.net performance

Bundling and Minification This feature is introduced in ASP.NET 4.5. This enables to bundle all the script files into

single file while loading at the browser. Minification is also applied to these files so that it’s size get reduced

Prior to ASP.NET 4.5, people used to write their own custom code or some third party plugins to achieve it.

Demo

Page 17: Asp.net performance

Script reference at bottom of the page Reference the scripts files at bottom of the

page. Problem with scripts that It blocks parallel

download

Page 18: Asp.net performance

HTTPCompression Turn on HTTPCompression for static and

dynamic content (for dynamic content, give time to decide)

It reduces the size significantly of HTML content

For, IIS7 and IIS 8, go to IIS Manager -> Feature View of the website . Click on Compression and enable. Only compress which file size is larger. (more than

few bytes) IIS 6 – Can be done via running some

coomand, I have written a post on it. http://brijbhushan.net/2010/10/17/how-to-enable-httpcompression-at-iis6/

Page 19: Asp.net performance

Review the files that you are downloading each page ASP.NET project by default provides many

scripts and other file with default project template. Remove them if you are not using it.

Also, have another check while shipping the project

Page 20: Asp.net performance

Remove HTTP Modules ASP.NET Page request processing

HTTPModule 1

HTTPModule 2

HTTPModule 3

HTTPModule n

HTTP Handler

Request Response

...

Page 21: Asp.net performance

Remove HTTP Modules contd. There are many HTTPModules by default

enabled. Remove all the modules which is not used

Demo

Page 22: Asp.net performance

Use startMode Attribute Whenever a site is updated, IIS recompiles the

application at first request. It may take long on the first request.

Also, most of the time, we load lot of data in cache etc at first request.

This attribute is part of IIS and is available applicationHost.config at C:\Windows\System32\inetsrv\config\applicationHost.config

It is set at application pool level as <applicationPools> <add name="MyAppWorkerProcess" managedRuntimeVersion="v4.0"

startMode="AlwaysRunning" /> </applicationPools>

Page 23: Asp.net performance

Conclusion Questions?

Thank You