9
 50 Tips to B oost AS P .NET Perf ormance When we are looking to optimize the performance of web applications we sh ould keep in mind about Memory Load, Processor Load and Network Bandwidth. Here are 50 best practices to im prove the performance and scalability of ASP.NET applications. 1. Page.IsPostBack Property  Keep code which only needs to be loaded once inside an IsPostBack block. if(!IsPostBack) { BindDropDownList(); LoadDynamicControls(); } As a result there will be no unnecessary database hits and server pro cessing. 2. Enable Buffering  A buffer is a region in main memor y to store temporary data for input and o utput .Data retrival from memory is faster than data retrieval from disk. We should leave buffering on unless there is any specific reason to t urn it off. By default buffering is enable. 3. Remove unused HttpModules  There may be lot o f HttpModules in Machine.Config that are not actually required for a particular application. In this scenario we should remove those u nused HttpModules from application specific web.config file. 4. Trim Page Sizes  Reduce page size by removing any unnecessary space and tab characters from the page. As a result network traffic will be reduced. 5. Use a CDN  Not a performance tip exclusive to ASP.NET but an important step in speeding up a site is to use a Content Delivery Network (CDN) . CDN¶s minimize the latency site visitors experience when t hey request a larger file from a data center t hat is located geographically far away. CDN¶s cache files at numerous edge locations around the world to minimize latency. If you are using Azure co nsider using the Windows Azure CDN , Amazon¶s CloudFront cheap and easy to integrate into a website (if you happen to have a WordPress blog you can integrate S3 and CloudFront into WordPress ) 6. Server.Transfer and Response.Redirect  

50 Tips to Boost ASP

Embed Size (px)

Citation preview

8/8/2019 50 Tips to Boost ASP

http://slidepdf.com/reader/full/50-tips-to-boost-asp 1/9

 

50 Tips to Boost ASP.NET Performance

When we are looking to optimize the performance of web applications we should keep in mind about MemoryLoad, Processor Load and Network Bandwidth. Here are 50 best practices to improve the performance and

scalability of ASP.NET applications.

1. Page.IsPostBack Property 

Keep code which only needs to be loaded once inside an IsPostBack block.

if(!IsPostBack)

{

BindDropDownList();

LoadDynamicControls();

}

As a result there will be no unnecessary database hits and server processing.

2. Enable Buffering 

A buffer is a region in main memory to store temporary data for input and output .Data retrival from memory isfaster than data retrieval from disk. We should leave buffering on unless there is any specific reason to turn it

off. By default buffering is enable.

3. Remove unused HttpModules 

There may be lot of HttpModules in Machine.Config that are not actually required for a particular application.

In this scenario we should remove those unused HttpModules from application specific web.config file.

4. Trim Page Sizes 

Reduce page size by removing any unnecessary space and tab characters from the page. As a result network 

traffic will be reduced.

5. Use a CDN 

 Not a performance tip exclusive to ASP.NET but an important step in speeding up a site is to use a ContentDelivery Network (CDN) . CDN¶s minimize the latency site visitors experience when they request a larger file

from a data center that is located geographically far away. CDN¶s cache files at numerous edge locations aroundthe world to minimize latency.

If you are using Azure consider using the Windows Azure CDN , Amazon¶s CloudFront cheap and easy tointegrate into a website (if you happen to have a WordPress blog you can integrate S3 and CloudFront into

WordPress)

6. Server.Transfer and Response.Redirect 

8/8/2019 50 Tips to Boost ASP

http://slidepdf.com/reader/full/50-tips-to-boost-asp 2/9

8/8/2019 50 Tips to Boost ASP

http://slidepdf.com/reader/full/50-tips-to-boost-asp 3/9

Acquire shared resources late and release them as early as possible. Avoid locking unless absolutelynecessary. Do not set lock on the ³this;´ it is better to use a private object to lock on as follows:

public Class Test

{

private stativ readonly objLock=new Object();

public static Test Singleton

{

lock(ObjLock)

{

return new test();

}

}

12. Exception Handling 

Handling exceptions in an improper way reduces the application performance drastically. We should try toavoid exceptions. Let¶s explain with some code snippet:

try

{

validateUser (username,password)

}

Catch(Exception e)

{

DisplayMessage();

}

The above code can be written in better and optimized way to avoid exception as follows:

If(validateUser)

{

//Some code

}

else

{

DisplayMessage();

}

We should avoid rethrowing exceptions because they are expensive as the stack trace is lost and a new stack 

trace must be created, for example:

8/8/2019 50 Tips to Boost ASP

http://slidepdf.com/reader/full/50-tips-to-boost-asp 4/9

try

{

//some code

}

catch (Exception ex)

{

throw ex;

}

An optimized version of the above code is as follows:

try

{

//some code

}

catch (Exception ex)

{

throw ; //stack trace information is preserved here.

}

13. Null check  

Performance can be improved if we check for a null value instead of catching exceptions from attempting toread null items. Here is a code snippet:

Object objItem=Session[³myItem´];

If(objItem==null)

{

//do something else

}

14. Enable the web gardening for multiprocessors computers 

In IIS Server there may be multiple application pools and each application pool should have at least a singleWorker Process. In multiprocessor machines the work is distributed to several processes ± one to each CPU and

this technique is known as Web Gardening. Web Gardening should have multiple Worker processes. In case of Web Garden Session Mode should be ³out proc´ and we can use ³Session State Server´ or ³SQL-Server 

Session State´. The worker processes in a Web Garden share the requests that come from that particular application pool. If any particular worker process fails, another worker process can continue to process requests

and that is the main advantage for application performance and scalability.

15. Disable Tracing 

8/8/2019 50 Tips to Boost ASP

http://slidepdf.com/reader/full/50-tips-to-boost-asp 5/9

Tracing may expose private information, such as the amount of information in view state, page processing time,etc. Enabling tracing adds performance overhead so it should be enabled only while an application is being

actively analyzed. Tracing should be turned off using

< trace enabled =´false´ - ± - ± -/> in config file.

16. Disable debug 

By default this attribute is set to ³true´ which is essential at development time, but always set debug=´false´ before deployment otherwise file size will be longer by adding pdb information which delays the page processing.

 Note : Check out  Debugging in Visual Studio Tutorial if you are looking to get started using Visual Studio for 

debugging  

17. Ensure checking of Page.IsValid 

While using Validator Controls, make sure that Page.IsValid is checked in code before processing of page.

18. Use Paging 

We should always load data in a grid type control using paging for faster page loading.

19. Avoid Recursive Functions and Nested Loops 

A lot of memory consumption occurs when using recursive functions and nested loops.  I t is always better toavoid nested loops and recursive functions to improve performance.

20. Cleaning Up code 

Find and remove unnecessary or redundant code to minimize page size. We can use tools like FXCop in for this.

21. Keep StyleSheets in the Header and Scripts to the end of Document 

Always place StyleSheets into the Header and place scripts at the end of the document. Progressive rendering is

 blocked until all StyleSheets have been downloaded and progressive rendering is stopped for all content belowthe script until it is fully loaded.

22. Keep JavaScript and CSS External 

Keeping Javascript and CSS files external instead of inline can reduce the page size and allow for faster page processing as the JavaScript and CSS files are cached by the browser.

23. Minimize the number of web server controls 

Only use the ASP.NET server controls when they are required otherwise use standard html controls for faster rendering.

24. Validate all Input received from the Users 

8/8/2019 50 Tips to Boost ASP

http://slidepdf.com/reader/full/50-tips-to-boost-asp 6/9

It is always better to validate all Input received from the users at client side to avoid the server round trip.Server side validation is required for security purpose.

25. Use stored procedures: 

As per MSDN, a stored procedure is a group of Transact-SQL statements compiled into a single execution plan.

Using stored procedure as compared to dynamic quaries boost application performance because stored

 procedure are precompiled. As a result the network traffic and server overhead are reduced. When a stored procedure is getting executed, sql server creates an execution plan in memory. Subsequent executions of the procedure are much faster because the plan is already available. On the other hand, execution plan for sql

queries are recreated for each and every execution. Another point to be noted that when when a stored procedure is executed, a message is transmitted from the server to the client that indicates the number of rows

are effected. This can be turned off to reduce network traffic by the following statement:

26. Batched Queries: 

Queries can be used in a batch and thus network traffic can be reduced: Here is an example:

³Select EmpNo, EmpName, EmpAddress from Employee´;³Select DepNo, DeptName From Department´;

From the above two queries it seems that there will be database hit twice .

Both the above queries can be executed in batched form and a single database hit will occur as follows:

³Select EmpNo, EmpName, EmpAddress from Employee; Select DepNo, DeptName From Department´;

27. Use IIS Compression 

Page size can also be reduced using Http compression in IIS. Compression tool can also be used to reduced the

size of rendered content.

28. Normalization 

We should follow normalization rules in database table design but over Normalized tables can cause excessive joins for simple requirement. We should not make excessive joins for performance overhead and hence it is

 better to normalize only as much as required keeping in mind the performance issue.

29. Efficient Coding 

While coding we should keep in mind the below issues which are potential performance drains:

1. Avoid use of Finalize method unless it is absolutely necessary.

2. Make a class sealed if inheritance is not required.3. Avoid calling GC.Collect();

4. Use X+=1 instead X=X+1 to avoid evaluating X twice;5. Use overloaded methods instead of different method names where possible.

30. Define The Scope of An Object 

8/8/2019 50 Tips to Boost ASP

http://slidepdf.com/reader/full/50-tips-to-boost-asp 7/9

8/8/2019 50 Tips to Boost ASP

http://slidepdf.com/reader/full/50-tips-to-boost-asp 8/9

8/8/2019 50 Tips to Boost ASP

http://slidepdf.com/reader/full/50-tips-to-boost-asp 9/9