|
Introduction
Performance tuning can be tricky. It's especially tough in Internet-related projects with lots of components running around, like HTML client, HTTP network, Web server, middle-tier components, database components, resource-management components, TCP/IP networks, and database servers. Performance tuning depends on a lot of parameters and sometimes, by changing a single parameter, performance can increase drastically.
Performance Issues
ASP application performance involves two parts:
- HTML page performance
- Response time
Further, we can boil down response rime to:
- ASP page performance
- Network bandwidth
- Database issues
Let's see each of them in detail.
HTML Page Performance
HTML page performance is purely a client problem. This problem can be related to the client’s hardware and the bandwidth. Apart from these, there are few other factors that can affect the page performance.
As we all know, the smaller the file size, the better the performance. Here are a few elements that can reduce the HTML file size, which will improve the page load performance in the browser.
- Avoid lot of images. When a browser requests a page, it makes N number of calls to the Web server to display N number of images. This will slow down the page load process. When you can’t avoid multiple images, try to show the images as thumbnails.
- Frames are another elements that will slow down the load process. For frames also, there will be N number of requests to display N number of frames.
- If you can avoid tables, that's good news. But we can’t always do that, so try to avoid nested tables. This will boost the load performance.
-
If you know your users, you can design better pages. Well, this is not possible in the Internet. But, if you are developing an Intranet product for a corporation, then the corporation will have a standard browser. For example, if the corporation’s standard browser is a 4.0 browser then you can avoid some complex tables and you can use Cascading Style Sheets to position your HTML elements.
- Avoid redundant tags. Let's take the following example:
<Body><br>
<P><font face="Verdana" size="4"><br>
</font></P><br>
<P><font face="Verdana" size="4"><br>
</font></P><br>
<P><font face="Verdana" size="4"><br>
</font></P><br>
</Body><br>
You can avoid the <font> tag
Source 2:
<Body><br>
<font face="Verdana" size="4"><br>
<P><br>
</P> <br>
<P> <br>
</P><br>
<P> <br>
</P><br>
<font> </Body><br>
- Avoid lot of jazzy comments. This will reduce the file size. I hate to leave comments in the HTML page. This will allow the others the view my page and understand my style.
- Avoid long file names and use relative path names to identify other files.
- Even you can reduce the formatting and indenting for the HTML page. This will minimize the page size.
- now you WYSIWYG HTML editor. Some of the WYSIWYG HTML editors will put lot of unwanted HTML tags in the page. So, it’s a good idea to clean up the unwanted tags, once you have designed your page with the WYSIWYG editor.
- Avoid Java Applets in the HTML pages, when they are not needed. For example, if you are only using the Java Applets for animations, then try to use animated gif files or a Flash animation. They are faster than Java Applets.
ASP Page Performance
- Reading from the object variable is always slower than reading from the local variable. So, if you are going to read from the object variable often, then store it in a local variable and access it.
Slower:
if Myobj.Value = 0 then
Do something
elseif Myobj.Value > 0 then
Do something
elseif Myobj.Value < 0 then
Do something
end if
Faster:
MyVar = Myobj.Value
if MyVar = 0 then
Do something
elseif MyVar > 0 then
Do something
elseif MyVar < 0 then
Do something
end if
- If you are using VBScript 5.0 or later, then you can use the With ... End With statements.
Slower:
Myobj.FirstName = "Srinivasa"
Myobj.LastName = "Sivakumar"
Myobj.City = "Chicago"
Faster:
With Myobj
.FirstName = "Srinivasa"
.LastName = "Sivakumar"
.City = "Chicago"
End with
- Before doing a bulky ASP code processing, you can check to make sure Response.IsClientConnected. If not, don’t process the page. This will reduce the load on the server.
- As always, avoid session variables because each ASP page runs in a different thread and session calls will be serialized one by one. So, this will slow down the application. Instead of session variables you can use the QueryString collection or hidden variables in the form which holds the values.
- Still you think you can't avoid the session variables. And if you have lot of session variables, then it is better to use the dictionary object.
- Disable the session access, if possible, with <%@ EnableSessionState=False %>.
- Enabling buffering will improve the performance, like Response.Buffer=True.
- Wrap all your data-access code inside a COM component. In this way you can take advantage of speed of the compiled and multithreaded. As you know, creating a database connection takes lots of system resources and time. You can avoid this time-lag by taking advantage of the connection pooling, when your component runs inside the Microsoft Transaction Server (MTS). MTS is a Windows NT-based technology that when used with Distributed COM (DCOM), lets you build COM objects that are more easily distributed across a network than when using DCOM alone
- Avoid multiple calls to the COM component. For example, if you want to write 10 values to a COM component, you will do it with 10 calls to the component. However, if you can do it with one call, then that will improve the performance.
Slower:
Myobj.FirstName = "Srinivasa"
Myobj.LastName = "Sivakumar"
Myobj.City = "Chicago"
Faster:
Instead of exposing 3 properties from the COM component, you can expose a single property called “Value,” and you can take advantage of the single call.
Let’s see how the code will look in the COM component:
Declare the private variables to hold the values of the properties. Declare three more constants to hold the property value position in the array.
Option Explicit
'Local variables to hold property values
Private mvarFirstName As String
Private mvarLastName As String
Private mvarCity As String
'Property Value Constants
Private Const CN_FirstName = 0
Private Const CN_LastName = 1
Private Const CN_City = 2
Add the “Value”property procedure.
Public Property Let Value(ByVal vData As Variant)
On Error GoTo ErrHand
'Check if the parameter value is an array
If IsArray(vData) = False Then
Err.Raise 100, App.Title & " - Property Let: Value", "Invalid property value. An array is expected."
GoTo CleanExit
Else
mvarFirstName = vData(CN_FirstName)
mvarLastName = vData(CN_LastName)
mvarCity = vData(CN_City)
End If
CleanExit:
Exit Property
ErrHand:
Err.Raise Err.Number, Err.Source, Err.Description
Exit Property
End Property
Here is how you will call the method from the ASP page.
Myobj.Value = Array("Srinivasa", "Sivakumar", "Chicago")
Even you can write the Get property procedure to pass the data from the component to the ASP application with a single call.
Note: Always have error handlers in all your functions, subs, properties, etc. This will avoid the whole system going down when an error occurs in the component. Even an error in a component could bring all the applications down in the same memory space. It could bring the Internet Information Server (IIS) down, if the component is running in the IIS memory space.
- Never, ever declare a COM component such as an ADO connection object with the application or session scope. This will also hit the performance because of the threading each call has to be marshaled between threads.
- The Apartment-Threaded COM component objects are only good for page scope. If you want to access the COM objects across the page, then consider the free-threaded or both threaded components.
Note: You can only develop Apartment-Threaded components in Visual Basic. If you are serious about the free-threaded component, then you have to develop them in VC++ or in Java.
- Do not use OnStartPage and OnEndPage methods to access the ASP intrinsic. These methods are provided for legacy support with IIS 3. Instead of this, use the ObjectContext with Implements keyword.
- When you are accessing the component across the process or machine, pass the variables to the objects as By Val. This will reduce the Marshaling overhead.
- When you have more than 100 lines of code in ASP, it is advisable to move that code to a COM component. ASP scripts are interpreted at run time and COM components are compiled.
Note: There is a time-lag for the component to be initialized, up and running. This could also affect the application performance, so be careful before making the decision between the ASP script and COM component.
- Never use components such as Microsoft Word or Excel to manipulate the data. They are out-of-process components and they are not optimized for performance.
- The other advantage of the component over the script is early binding vs. late binding.
- As a Web developer, we always like to create a large Include file that will include all the global code and constants. The problem with this approach is some of the code or the declaration will not be used in every page. So for each and every page these Include files will be processed and this will definitely slow down the page performance.
- Avoid multiple Request.Write statements and group them in to few.
Slower:
'Add a default Select Word
Response.Write " <option value=""0"">(Select a Program Manager)</option>"
¨ Do While Not objRs.EOF
Response.Write " <option value=" & objRs!ProjManKey & ">" & objRs!ProjMan & "</option>"
objRs.MoveNext
Loop
Response.Write "</font> </select>"
Faster:
Dim strHTML
'Add a default Select Word
strHTML = " <option value=""0"">(Select a Program Manager)</option>"
Do While Not objRs.EOF
strHTML = strHTML & " <option value=" & objRs!ProjManKey & ">" & objRs!ProjMan & "</option>"
objRs.MoveNext
Loop
strHTML = strHTML & "</font> </select>"
Response.Write strHTML
Network Bandwidth Issues
- In most cases network performance can ruin the ASP performance. Use 10/100 network cards for better performance.
- If your Web server and the database server are running in the same server, then it is advisable to move them into different servers.
- Even you can introduce a new middle-tier server to handle the COM components with MTS. This decision totally depends on the load on the Web and MTS servers.
Database Issues
Well, a good database design can contribute a lot to the performance. Database design issues are out of the scope of this article. Let’s see few points that could improve the database performance.
- Whenever you access the database, avoid the dynamic SQL and use stored procedures or database views. Before finalizing the SQL statement for the stored procedure or view, analyze the SQL query and make sure it is optimized, and also make sure that it uses the proper indices for record scanning.
Note: If you are using SQL Server 7 or later. then you can take advantage of the visual output of the “Query Analyzer.”
- The join type used in the SQL statement will also increase or decrease the performance of the query.
- When using the ADO Recordset objects, use the proper cursor type and lock type. For example, if you are going to fill a combo box, then you can use the cursor type adOpenForwardOnly and lock type adLockReadOnly.
- Sometimes allocating appropriate database buffers will increase the performance. For example, if you are working with the Oracle database, for each connection you should have three sessions open. If you can fine-tune these things, you’ll get drastic performance increase.
- If your site has lot of hits and your server can’t handle the entire load, then replicate the ASP application and the database.
Note: While designing the database, you have to make appropriate design considerations for database replication. When we are replicating the databases, few elements will not be replicated. For example, if you are using Oracle as the database server, Database Sequences will not be replicated. So, if you are rely on Database Sequences for your primary key, then you are in trouble.
Summary
When we talk about ASP performance, there are lots of factors and I have discussed each of them in detail. But don’t think that if you can fix all these factors, your ASP performance will increase. All these tips will not suit each and every ASP application. We have to consider them in application by application basis.
Well, make some time to read the book “Microsoft Internet Information Server Resource Kit” from MS Press.
Try these following for more information:
Improving Web Site Performance
Server Performance
Microsoft Web Capacity Analysis Tool
Simulating Cookies with the Munger
Happy programming!
About the Author
Srinivasa Sivakumar is a software consultant in for TransTech, Inc., based in Chicago. He specializes in VB, SQL Server, MTS, and ASP. In addition to reading, he likes to readand watch Tamil movies and listen to Tamil soundtracks. He can be reached at srinivasasivakumar@india.com.
|