Monday, 23 December 2013

Increase Performance in an asp.net Application

Increase Performance in an asp.net Application

 For every enterprise level application the key to make that application successful is the responsiveness of the application. ASP.NET offers a great deal of the features for developing web based enterprise applications, but sometimes, due to adhering to best practices, the application is not as fast as it should be. Here are the some useful suggestions to make your application super fast.

  1. Always set debug=”false” in web.config production environment.
  2. Always set trace=”false” in web.config production environment
  3. If you are using asp.net 2.0 or higher version then always use precompiled version of your code and also prefer web application project over website. If you are using website then always publish it and then upload that precompiled version of your site in production environment.
  4. Always compile your project in Release Mode before uploading application to production environment.
  5. Decrease your html kb as much as you can for that use tables less html using div’s and if possible then do not give big name to your control it will increase your html kb as asp.net uses client Id to differentiate all the controls. If you are creating custom controls then you can overwrite your clientid and uniqueId.
  6. Use cache api as much as possible it will decrease your server roundtrip and boost application performance. ASP.NET 2.0 or higher version provides functionality called sqlcachedependancy for your database caching. It will validate cache with your database operation like insert,update and delete and if not possible with it then use the file base caching.
  7. Remove blank spaces from your html it will increase your kb. You can use regular expression to remove white spaces. I will post the code for removing white spaces next posts.
  8. For asp.net 2.0 and higher version use master pages. It will increase your performance.
  9. Prefer database reader over dataset unless and until you have specific reason to use database.
  10. Use ADO.NET asynchronous calls for ado.net methods. asp.net 2.0 or higher version is supporting your performance. If you are using same procedure or command multiple time then use ADO.NET Prepare command it will increase your performance.
  11. Do IIS performance tuning as per your requirement.
  12. Disable view state for your controls if possible. If you are using asp.net 2.0 or higher version then use asp.net control state instead of view state. Store view state in session or database by overriding the default methods for storing view state.
  13. User Server.Transfer instead of response.redirect.
  14. Always use inproc session state if possible.
  15. Use Ajax for your application wisely. Lots of Ajax calls for a page will also decrease your performance.
  16. Measure your application performance with tools like redgate profiler,firebug and whyslovw from yahoo.
  17. User System.Text.StringBuilder for string concatenation its 4 times more faster then the normal strings.
  18. Right JavaScript in .Js files and place it as bottom of the application.
  19. Use Separate CSS files for styling of application.
  20. User database paging over normal paging while displaying huge amount of data.
  21. Call web service from java script instead of server side. Use asynchronous calls to call a web method from web service.

Tips to improve asp.net application performance


  • Deploy with ‘Release' build and ensure ‘Debug' is set to false in the web.config
Make sure you use Release Build mode and not Debug Build when you deploy your site to production. When you create the application, by default the attribute ‘Debug' is set to "true" in the web.config which is very useful while developing. However, when you are deploying your application, always set it to "false".  <compilation debug="false" ... />
  • Disable ViewState if you don’t need it
The ViewState is not needed when the following conditions are true: 
  a)The page does not post back (if it is used only to display data, only output page)
  b)The events of the server controls are not handled
  c)The controls are repopulated on each page refresh
  • Consider using caching
Caching avoids redundant work. If you use caching properly, you can avoid unnecessary database lookups and other expensive operations.ASP.NET allows you to cache entire pages, fragment of pages or controls. You can cache also variable data by specifying the parameters that the data depends. By using caching you help ASP.NET engine to return data for repeated request for the same page much faster.
  • Consider using HTTP compression
HTTP compression is supported by most modern browsers and by IIS. HTTP Compression is a define a way to compress content transferred from Web servers across the World Wide Web to browsers. The impact of compression is that the number of transmitted bytes is reduced and thus a higher performance is gained.
  • Consider disabling tracing
Before you deploy your application, disable tracing because it is not recommended to turn it on while the application is running in production. Tracing may cause performance issues.
<configuration> 
     <system.web>                   
          <trace enabled="false" pageOutput="false" /> 
     </system.web>
</configuration>
  • Consider using paging for large result sets
Paging large query result sets can significantly improve the performance of an application. The paging can be done at the SQL procedure level this will reduces the back-end work on the database, it be done at the server level to reduce the size of data that is sent to the client and that will be rendered as Html content.
  • Use 'using' statment to dispose resources
The using statement defines a scope at the end of which an object will be disposed even if an exception is thrown, please note that the object that you are trying to dispose should implement 'System.IDisposable'.The following code fragment demonstrates this.   using (SqlConnection cn = new SqlConnection(connectionString))
{
    using (SqlCommand cm = new SqlCommand(commandString, cn))
    {
        cn.Open();
        cm.ExecuteNonQuery();
    }
}
 
  • Stored Procedures
Stored procedures provide improved performance to your asp.net application. When you develop stored procedures, keep the following recommendations in mind:  
·        
Use Set NOCOUNT ON in stored procedures, if you turn on the NOCOUNT option, stored procedures will not return the row-count information to the client, and this will prevent SQL Server from sending the DONE_IN_PROC message for each statement in the stored procedure.·         Do not use the sp_ prefix for custom stored procedures, Microsoft does not recommend to use the prefix "sp_" in the user-created stored procedure name, because SQL Server always looks for a stored procedure beginning with "sp_" in the master database.
 
  • String Management
Use the += operator when the number of appends is known.
Use the StringBuilder object when the number of appends is unknown.  

  • Consider using DTO pattern
In some cases to satisfy a single client request it require making multiple calls to the remote interface, so instead of doing multiple calls consider using Data Transfer Object (DTO) that holds all the data required by the remote call in one single object.
  • Consider using Server.Transfer instead of Response.Redirect
Both cause a new page to be processed, but the interaction between the client and server is different in each situation. Server.Transfer acts as an efficient replacement for the Response.Redirect method. Response.Redirect specifies to the browser to request a different page. Because a redirect forces a new page request, the browser makes two requests to the Web server, so the Web server handles an extra request. IIS 5.0 introduced a new function, Server.Transfer, which transfers execution to a different ASP page on the server. This avoids the extra request, resulting in better overall system performance

 

No comments:

Post a Comment