How to improve performance of web application or portal

In this article i will explain some of the points that all developers must be familiar. Whenever we are planning to develop a web application or web portal we must know these point so that performance of our application is good. So following are the considerable points:–

  • Turn off Tracing unless until required

Tracing is one of the wonderful features which enables us to track the application’s trace and the sequences. However, again it is useful only for developers and you can set this to “false” unless you require to monitor the trace logging.

How it affects performance

Enabling tracing adds performance overhead and might expose private information, so it should be enabled only while an application is being actively analyzed.

Solution

When not needed, tracing can be turned off using:

<trace enabled=”false” requestLimit=”10” pageoutput=”false”

traceMode=”SortByTime” localOnly=”true”>

  • Turn off Session State, if not required

One extremely powerful feature of ASP.NET is its ability to store session state for users, such as a shopping cart on an e-commerce site or a browser history.

How it affects performance

Since ASP.NET manages session state by default, you pay the cost in memory even if you don’t use it, i.e., whether you store your data in-process or on state server or in a SQL database, session state requires memory and it’s also time consuming when you store or retrieve data from it.

Solution

You may not require session state when your pages are static or when you do not need to store information captured in the page.

In such cases where you need not use session state, disable it on your web form using the directive:

<@%Page EnableSessionState=”false”%>

In case you use the session state only to retrieve data from it and not to update it, make the session state read only by using the directive:

<@%Page EnableSessionState =”ReadOnly”%>

  • Disable View State of a Page if possible

View state is a fancy name for ASP.NET storing some state data in a hidden input field inside the generated page. When the page is posted back to the server, the server can parse, validate, and apply this view state data back to the page’s tree of controls.

View state is a very powerful capability since it allows state to be persisted with the client and it requires no cookies or server memory to save this state. Many ASP.NET server controls use view state to persist settings made during interactions with elements on the page, for example, saving the current page that is being displayed when paging through data.

How it affects performance

  1. There are a number of drawbacks to the use of view state, however.
  2. It increases the total payload of the page both when served and when requested. There is also an additional overhead incurred when serializing or deserializing view state data that is posted back to the server.
  3. View state increases the memory allocations on the server. Several server controls, the most well known of which is the DataGrid, tend to make excessive use of view state, even in cases where it is not needed.

Solution

Pages that do not have any server postback events can have the view state turned off.

The default behavior of the ViewState property is enabled, but if you don’t need it, you can turn it off at the control or page level. Within a control, simply set the EnableViewState property to false, or set it globally within the page using this setting:

<%@ Page EnableViewState=”false” %>

If you turn view state off for a page or control, make sure you thoroughly test your pages to verify that they continue to function correctly.

  • Set debug=false in web.config

When you create the application, by default this attribute is set to “true” which is very useful while developing. However, when you are deploying your application, always set it to “false“.

How it affects performance

Setting it to “true” requires the PDB information to be inserted into the file and this results in a comparatively larger file and hence processing will be slow.

Solution

Therefore, always set debug=”false” before deployment.

  • Avoid Response.Redirect

Response.Redirect() method simply tells the browser to visit another page.

How it affects performance

Redirects are also very chatty. They should only be used when you are transferring people to another physical web server.

Solution

For any transfers within your server, use .transfer! You will save a lot of needless HTTP requests. Instead of telling the browser to redirect, it simply changes the “focus” on the Web server and transfers the request. This means you don’t get quite as many HTTP requests coming through, which therefore eases the pressure on your Web server and makes your applications run faster.

Tradeoffs

  1. .transfer” process can work on only those sites running on the server. Only Response.Redirectcan do that.
  2. Server.Transfer maintains the original URL in the browser. This can really help streamline data entry techniques, although it may make for confusion when debugging

5. A) To reduce CLR Exceptions count, use Response.Redirect (".aspx", false) instead ofresponse.redirect (".aspx").

  • Use the String builder to concatenate string

How it affects performance

String is Evil when you want to append and concatenate text to your string. All the activities you do to the string are stored in the memory as separate references and it must be avoided as much as possible.

i.e. When a string is modified, the run time will create a new string and return it, leaving the original to be garbage collected. Most of the time, this is a fast and simple way to do it, but when a string is being modified repeatedly, it begins to be a burden on performance: all of those allocations eventually get expensive.

Solution

Use String Builder whenever string concatenation is needed so that it only stores the value in the originalstring and no additional reference is created.

  • Avoid throwing exceptions

How it affects performance

Exceptions are probably one of the heaviest resource hogs and causes of slowdowns you will ever see in web applications, as well as windows applications.

Solution

You can use as many try/catch blocks as you want. Using exceptions gratuitously is where you loseperformance. For example, you should stay away from things like using exceptions for control flow.

  • Use Finally Method to kill resources
  1. The finally method gets executed independent of the outcome of the Block.
  2. Always use the finally block to kill resources like closing database connection, closing files and other resources such that they get executed independent of whether the code worked in Try or went toCatch.
  • Use Client Side Scripts for validations

User Input is Evil and it must be thoroughly validated before processing to avoid overhead and possible injections to your applications.

How It improves performance

Client site validation can help reduce round trips that are required to process user’s request. In ASP.NET, you can also use client side controls to validate user input. However, do a check at the Server side too to avoid the infamous JavaScript disabled scenarios.

  • Avoid unnecessary round trips to the server

How it affects performance

Round trips significantly affect performance. They are subject to network latency and to downstream server latency. Many data-driven Web sites heavily access the database for every user request. While connection pooling helps, the increased network traffic and processing load on the database server can adversely affectperformance.

Solution

  1. Keep round trips to an absolute minimum
  2. Implement Ajax UI whenever possible. The idea is to avoid full page refresh and only update the portion of the page that needs to be changed
  • Use Page.ISPostBack

Make sure you don’t execute code needlessly. Use Page.ISPostBack property to ensure that you only perform page initialization logic when a page is first time loaded and not in response to client postbacks.

  • Include Return Statements with in the Function/Method

How it improves performance

Explicitly using return allows the JIT to perform slightly more optimizations. Without a return statement, each function/method is given several local variables on stack to transparently support returning values without the keyword. Keeping these around makes it harder for the JIT to optimize, and can impact theperformance of your code. Look through your functions/methods and insert return as needed. It doesn’t change the semantics of the code at all, and it can help you get more speed from your application.

  • Use Foreach loop instead of For loop for String Iteration

Foreach is far more readable, and in the future it will become as fast as a For loop for special cases likestrings. Unless string manipulation is a real performance hog for you, the slightly messier code may not be worth it.

  • Avoid Unnecessary Indirection

How it affects performance

When you use byRef, you pass pointers instead of the actual object.

Many times, this makes sense (side-effecting functions, for example), but you don’t always need it. Passing pointers results in more indirection, which is slower than accessing a value that is on the stack.

Solution

When you don’t need to go through the heap, it is best to avoid it there by avoiding indirection.

  • Use “ArrayLists” in place of arrays

How it improves performance

An ArrayList has everything that is good about an array PLUS automatic sizing, Add, Insert, Remove, Sort, Binary Search. All these great helper methods are added when implementing the IList interface.

Tradeoffs

The downside of an ArrayList is the need to cast objects upon retrieval.

  • Always check Page.IsValid when using Validator Controls

Always make sure you check Page.IsValid before processing your forms when using Validator Controls.

  • Use Paging

Take advantage of paging’s simplicity in .NET. Only show small subsets of data at a time, allowing the page to load faster.

Tradeoffs

Just be careful when you mix in caching. Don’t cache all the data in the grid.

  • Store your content by using caching

How it improves performance

ASP.NET allows you to cache entire pages, fragment of pages or controls. You can also cache variable data by specifying the parameters on which the data depends. By using caching, you help ASP.NET engine to return data for repeated request for the same page much faster.

When and Why Use Caching

A proper use and fine tune of caching approach will result in better performance and scalability of your site. However, improper use of caching will actually slow down and consume lots of your server performance and memory usage.

Good candidate to use caching is if you have infrequent chance of data or static content of web page.

  • Use low cost authentication

Authentication can also have an impact over the performance of your application. For example, passport authentication is slower than form-base authentication which in here turn is slower than Windows authentication.

  • Minimize the number of web server controls

How it affects performance

The use of web server controls increases the response time of your application because they need time to be processed on the server side before they are rendered on the client side.

Solution

One way to minimize the number of web server controls is by taking into consideration, the usage of HTML elements where they are suited, for example if you want to display static text.

  • Avoid using unmanaged code

How it affects performance

Calls to unmanaged code are a costly marshaling operation.

Solution

Try to reduce the number calls between the managed and unmanaged code. Consider doing more work in each call rather than making frequent calls to do small tasks.

  • Avoid making frequent calls across processes

If you are working with distributed applications, this involves additional overhead negotiating network and application level protocols. In this case, network speed can also be a bottleneck. Try to do as much work as possible in fewer calls over the network.

  • Cleaning Up Style Sheets and Script Files
  1. A quick and easy way to improve your web application’s performance is by going back and cleaning up your CSS Style Sheets and Script Files of unnecessary code or old styles and functions. It is common for old styles and functions to still exist in your style sheets and script files during development cycles and when improvements are made to a website.
  2. Many websites use a single CSS Style Sheet or Script File for the entire website. Sometimes, just going through these files and cleaning them up can improve the performance of your site by reducing the page size. If you are referencing images in your style sheet that are no longer used on your website, it’s a waste of performance to leave them in there and have them loaded each time the style sheet is loaded.
  3. Run a web page analyzer against pages in your website so that you can see exactly what is being loaded and what takes the most time to load.
  • Design with ValueTypes

Use simple structs when you can, and when you don’t do a lot of boxing and unboxing.

Tradeoffs

ValueTypes are far less flexible than Objects, and end up hurting performance if used incorrectly. You need to be very careful about when you treat them like objects. This adds extra boxing and unboxing overhead to your program, and can end up costing you more than it would if you had stuck with objects.

  • Minimize assemblies

Minimize the number of assemblies you use to keep your working set small. If you load an entire assembly just to use one method, you’re paying a tremendous cost for very little benefit. See if you can duplicate that method’s functionality using code that you already have loaded.

  • Encode Using ASCII When You Don’t Need UTF

By default, ASP.NET comes configured to encode requests and responses as UTF-8.

If ASCII is all your application needs, eliminating the UTF overhead can give you back a few cycles. Note that this can only be done on a per-application basis.

  • Avoid Recursive Functions / Nested Loops

These are general things to adopt in any programming language, which consume lot of memory. Always avoid Nested Loops, Recursive functions, to improve performance.

  • Minimize the Use of Format ()

When you can, use toString() instead of format(). In most cases, it will provide you with the functionality you need, with much less overhead.

  • Place StyleSheets into the Header

Web developers who care about performance want browser to load whatever content it has as soon as possible. This fact is especially important for pages with a lot of content and for users with slow Internet connections. When the browser loads the page progressively the header, the logo, the navigation components serve as visual feedback for the user.

When we place style sheets near the bottom part of the HTML, most browsers stop rendering to avoid redrawing elements of the page if their styles change thus decreasing the performance of the page. So, always place StyleSheets into the Header.

  • Put Scripts to the end of Document

Unlike StyleSheets, it is better to place scripts to the end of the document. Progressive rendering is blocked until all StyleSheets have been downloaded. Scripts cause progressive rendering to stop for all content below the script until it is fully loaded. Moreover, while downloading a script, the browser does not start any other component downloads, even on different hostnames.

So, always have scripts at the end of the document.

  • Make JavaScript and CSS External

Using external files generally produces faster pages because the JavaScript and CSS files are cached by the browser. Inline JavaScript and CSS increase the HTML document size but reduce the number of HTTP requests. With cached external files, the size of the HTML is kept small without increasing the number of HTTP requests thus improving the performance.

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s