28 ASP performance optimization rules recommended by Microsoft (1)

Source: Internet
Author: User

This article describes how to optimize ASP applications and Visual Basic & reg; Scripting Edition (VBScript. This article discusses many traps. The recommendations listed in this article have been tested in http://www.microsoft.com and on other sites, with remarkable results. This document assumes that you have a basic understanding of ASP development, including VBScript and/or JScript, ASP Application, ASP Session, and other ASP inherent Object Request, Response, and Server.

Generally, ASP performance mainly depends on many factors other than ASP code. We do not list all information in an article. At the end of this article, we list performance-related resources. These links cover ASP and non-ASP topics, including ActiveX & reg; Data Object (ADO), Component Object Model (COM), database, and Internet Information Server (IIS) configuration. These are some of our favorite links-be sure to check them out.

Tip 1: cache frequently used data on a Web server

A typical ASP page retrieves data from the back-end data storage, and converts the result to an ultra-Text Markup Language (HTML ). Regardless of the database speed, retrieving data from the memory is always much faster than retrieving data from the back-end data storage. Reading data from a local hard disk is usually faster than retrieving data from a database. Therefore, data can usually be cached on the Web server and stored in memory or disk) to improve performance.

Cache is a traditional practice of exchanging space for time. If the cached content is correct, you can see that the performance will be significantly improved. To make the cache effective, you must save the frequently used data and re-calculate the data.) large overhead. If old data is cached, the memory will be wasted.

Data that does not change frequently is a good candidate for caching, because you do not have to worry about synchronizing the data with the database over time. Combo list, reference table, DHTML fragment, Extended Markup Language (XML) string, menu item, and site configuration variables include data source name (DSN), Internet Protocol (IP) address, and Web path) they are good cache candidates. Note that you can cache the "representation" of data instead of the data itself. If the ASP page is rarely changed and the cache overhead is large, for example, the entire product directory), you should consider generating HTML in advance instead of re-displaying each request.

Where should data be cached and what are the cache policies? Data is usually cached in the memory or disk of the Web server. The following two tips describe these two methods.

Tip 2: cache frequently used data in Application or Session objects

ASP Application and Session objects provide a convenient container for caching data in the memory. You can assign data to Application and Session objects, which are stored in memory between HTTP calls. Session data is stored separately by each user, while Application data is shared among all users.

When will the data be loaded into the Application or Session? Generally, data is loaded when an Application or Session is started. To load data during Application or Session startup, add the appropriate code to Application_OnStart () or Session_OnStart. These functions should be in Global. asa. If not, you can add them. You can also load the data at the first time you need it. To this end, add some code on the ASP page or write a reusable Script Function) to check whether the data exists. If not, load the data. This is a traditional performance technology called "inert computing"-it is not calculated until you know you need a value. For example:

<%
Function GetEmploymentStatusList
Dim d
D = Application (? EmploymentStatusList ?)
If d = ?? Then
'Fetchemploymentstatuslist function (not shown)
'Fetches data from DB, returns an Array
D = FetchEmploymentStatusList ()
Application (? EmploymentStatusList ?) = D
End If
GetEmploymentStatusList = d
End Function
%>

You can write similar functions for each required data block.

What format should data be stored? Any variant type can be stored, because all script variables are of a variant type. For example, you can store strings, integers, or arrays. Typically, you store the content of the ADO record set as one of these variable types. To obtain data from the ADO record set, you can manually copy the data to the VBScript variable, one field at a time. Using an ADO record set persistence function GetRows (), GetString (), or Save () (ADO 2.5) can speed up and make it easier. The details are beyond the scope discussed in this article. However, the following is a function example to illustrate how to use GetRows () to return an array of record set data:

'Get Recordset, return as an Array
Function FetchEmploymentStatusList
Dim rs
Set rs = CreateObject (? ADODB. Recordset ?)
Rs. Open? Select StatusName, StatusID from EmployeeStatus ?, _
? Dsn = employees; uid = sa; pwd = ;?
FetchEmploymentStatusList = rs. GetRows ()? Return data as an Array
Rs. Close
Set rs = Nothing
End Function


Further improve the preceding example to cache HTML as a list instead of an array. The following is a simple example:

'Get Recordset, return as HTML Option list
Function FetchEmploymentStatusList
Dim rs, fldName, s
Set rs = CreateObject (? ADODB. Recordset ?)
Rs. Open? Select StatusName, StatusID from EmployeeStatus ?, _
? Dsn = employees; uid = sa; pwd = ;?
S =? <Select name = ?? EmploymentStatus? & VbCrLf
Set fldName = rs. Fields (? StatusName ?) 'Ado Field Binding
Do Until rs. EOF
'Next line violates Don't Do String Concats,
'But it's OK because we are building a cache
S = s &? <Option>? & FldName &? </Option>? & VbCrLf
Rs. MoveNext
Loop
S = s &? </Select>? & VbCrLf
Rs. Close
Set rs = Nothing 'see Release Early
FetchEmploymentStatusList = S' Return data as a String
End Function

Under appropriate conditions, the ADO record set itself can be cached in the Application or Session scope. There are two warnings:

ADO must be marked as a free thread
The disconnected record set must be used.
If the two requirements cannot be met, do not cache the ADO record set. In the following "non-agile components" and "Do not cache connections" techniques, we will discuss the risks of storing COM objects in the Application or Session scope.

When you store data in the Application or Session scope, the data is retained until you change it programmatically, the Session expires, or the Web Application restarts. What if data needs to be updated? To manually update Application data, you can access ASP pages that only Administrators can access to update data. Alternatively, you can use the function to automatically refresh data on a regular basis. The following example stores the timestamp with cached data and refreshes the data after a period of time.

<%
'Error handing not shown...
Const UPDATE_INTERVAL = 300 'refresh interval, in seconds
'Function to return the employee status list
Function GetEmploymentStatusList
UpdateEmploymentStatus
GetEmploymentStatusList = Application (? EmploymentStatusList ?)
End Function
'Periodically update the cached data
Sub UpdateEmploymentStatusList
Dim d, strLastUpdate
StrLastUpdate = Application (? LastUpdate ?)
If (strLastUpdate = ??) Or _
(UPDATE_INTERVAL <DateDiff (? S ?, StrLastUpdate, Now) Then
'Note: two or more CILS might get in here. This is okay and will simply
'Result in a few unnecessary fetches (there is a workaround for this)
'Fetchemploymentstatuslist function (not shown)
'Fetches data from DB, returns an Array
D = FetchEmploymentStatusList ()
'Update the Application object. Use Application. Lock ()
'To ensure consistent data
Application. Lock
Application (? EmploymentStatusList ?) = Events
Application (? LastUpdate ?) = CStr (Now)
Application. Unlock
End If
End Sub

For more information, see World's Fastest ListBox with Application Data.

It is not a good practice to cache large arrays in a Session or Application object. Before accessing any element of the array, the syntax of the script language requires that the entire array be copied temporarily. For example, if you map an array consisting of 100,000 elements in a string to a local weather station, the array is cached in the Application object, ASP must copy all the 100,000 weather stations to a temporary array before extracting a string. In this case, it would be better to create a custom component using a custom method to store weather stations-or use a dictionary component.

Let's warn you not to drop the baby and bath water together: Arrays can be quickly searched and stored in memory as neighboring key data pairs. Indexing a dictionary peso to index an array is much slower. Select a data structure that provides the best performance based on your actual situation.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.