How to enhance ASP program performance (1)

xiaoxiao2021-03-06  109

Performance is a very important feature. You need to design a performance indicator in advance, otherwise you will rewrite the program for this. That is to say: How do you optimize the ASP program?

This paper proposes some techniques that optimize ASP applications and VBScript, many techniques and defects have been discussed. The recommendations listed here have been tested at http://www.microsoft.com and other sites, they have work very well. This article assumes that you have basic knowledge developed by ASP, including VBScript or JScript, ASP applications, ASP SESSION, and other ASP built-in objects (Request, Response, and Server).

Usually, ASP execution performance is far more than just ASP code itself! The end of this article lists resources related to performance, which contains ASP and non-ASP portions, including ActiveX Data Objects (ADO), Component Object Model (COM), Database, and Internet Information Server (IIS) )Configuration. In addition to these, there are some very good links worth watching.

Tips 1: Cache frequently used data on the web server

A typical case is that the ASP page retrieves data from the background store, and then forms the result in the hypercurrent markup language (HTML). Regardless of the speed of the database, retrieving data from memory is much faster than from the background storage device. Reading data from a local hard drive is usually very fast. Therefore, improve performance can be implemented by data on the cache server, whether it is cacked in memory, or in the local hard disk.

The cache is a classic "spatial change time". If the cache is appropriate, you can see a significant performance improvement. In order to make the cache valid, it is necessary to ensure that cache data is often reused, and it is also cumbersome. The cache filled with old data is a waste of memory.

The data that does not change often is a better object of the cache because it is not necessary to consider the synchronization operation after this data update. Combination box, reference table, DHTML code, extended tag language string, menu, and site configuration variables (including data source names DSNS, Internet protocol address IP, and web paths) are well cache objects. Note: To cache data expressions instead of the data itself. If an ASP page changes frequently and firms (such as the entire product catalog), you should consider pre-generating HTML instead of each time a request is taken.

Tips 2: Cache often used in Application or Session objects

The Application and Session object in the ASP are a convenient container that caches data in memory. You can assign data to Application and Session objects, which will remain in memory during HTTP calls. The data in the session is? A user service, the data in Application is shared by all users.

When will I need to load data in Application and Session? Typically, the data is loaded when the application starts or when the session begins. In order to load data at this time, add the appropriate code to Application OnStart () or Session OnStart (). These functions are located in the file global.asa, and if it does not exist, add it. You can also transfer it when the data is first required, add code to the ASP page, check if the data exists, if you don't find it, you will be transferred. Here is an example that represents a classic performance processing technology called "lazy evalution": until it is needed, then calculate. The examples are as follows: $ # @ 60;%

Function geteMPloymentStatusList

DIM D

D = Application ("EMPLOYMENTSTATUSLIST")

IF d = "" "

FetChemploymentStatusList Function (Not Shown)

Fetches Data from DB, Returns an Array

D = fetchemploymentstatuslist ()

Application ("EMPLOYMENTSTATUSLIST") = D

END IF

GetEmploymentStatusList = D

END FUNCTION

% $ # @ 62;

A similar function code can be written for different data.

What format should be saved in? Any variable type can be used because all script variables are different. For example, it can be saved as a string, integer or data. Typically, the contents of the ADO record set are stored in one of these variable types. In order to remove data from the ADO record, you need to copy data from the VBScript variable, each time a field. Use any ADO record set function functions getRows (), getString () or Save () (ADO 2.5) is very fast and simple, here there is a function, describing how to use getRows () Return 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

A deeper tip of the above code is to cache HTML for the list. Here 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 = "$ # @ 60; select name =" "EmploymentStatus" $ # @ 62; "& vbcrlf

Set fldname = rs.fields ("statusname") ADO FIELD BINDING

Do Until Rs.eof

Next Line Violates DONT DO STRING CONCATS,

But ITS OK BECAUSE WE Are Building a cache

S = S & "$ # @ 60; option $ # @ 62;" & fldName & "$ # @ 60; / option $ # @ 62;" & vbcrlf

rs.movenext

Loop

S = S & "$ # @ 60; / select $ # @ 62;" & vbcrlf

Rs.close

SET RS = Nothing See Release Early

FetChemploymentStatusList = S return Data as a string

END FUNCTION

In a suitable environment, you can cache the ADO recordset itself in Application or Session, but there is 2 points:

ADO must be a free thread mark requires a Disconnected RecordSet method If you do not guarantee the above two conditions, do not cache an ADO record set because it produces a lot of hazards.

When the data is saved in the Application or Session, the data will remain, unless the program changes it, the session variable expires or the web application is restarted. What should I do if the data needs to be updated? You can call only the ASP page that only administrators can access to update the data, or automatically update the data through the function periodic. In the following example, the clock tag is saved with the cache data. After a period of time, the data is refreshed.

$ # @ 60;%

Error Handing Not Shown ...

Const update_interval = 300 refresh Interval, In Seconds

Function to return the Employment 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 $ # @ 60; Datediff ("S", Strlastupdate, Now) Then

Note: Two or more calls might get in here. This is okay and willimply. ISOS OKAY AND WILL SIMP

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") = D

Application ("Lastupdate") = CSTR (now)

Application.unlock

END IF

End Sub

There is another example, see

World's Fastest Listbox with Application Data.

It must be aware that the array of cache large capacity in the session or Application object is not a good method. The rules of the scripting language must first establish a temporary backup of the entire array before accessing any elements in arrays. For example, if you cach a 100,000 element in the Application object, which contains the corresponding relationship with the US Postal Code and the local meteorological station. The ASP must first copy all 100,000 weather station information to the temporary array, then Select one of the strings for processing. In this case, create a custom component, writing a method storage weather station information, is a very good way.

Tips 3: Cache data on the web server disk and HTML page

Sometimes, there are "many" data to cache in memory. "Many" are relatively, depending on how much memory, cache item is used, and the frequency of retrieving data. In any case, if you need to cache a large amount of data in memory, consider making caches on the web server hard drive in a text or XML file format. Of course, it is also possible to mix the use of hard drive cache data and memory cache data to achieve the best cache.

Note: When testing the performance of a single ASP page, retrieving data from disk is not necessarily faster than retrieving data from the network database, but the cache reduces the call of the network database. This will significantly improve the throughput of the network during large scale call. The result of caching a fee is very useful, such as a complex stored procedure, or a large number of result data.

ASP and COM provide several tools based on disk buffering configurations. The SAVE () and open () functions of the ADO record set are responsible for saving and tune the record set on the disk. There are also some components:

Scripting.FileSystemObject allows you to create, read, and write files MSXML, Microsoft XML parsers come with Internet Explorer, support saving and loading an XML document lookuptable object (such as using on MSN) is a very good list from disk. select. Finally, consider the expression of the cache disk data, not the data itself. The pre-processed HTML can be stored as .htm or .asp files, the link directs them directly. Use

The XBuilder or Microsoft SQL Server Internet is published by the business tools that can automatically process them. Moreover, the HTML program block can also be included in the .asp file. Similarly, you can also read the HTML file from the disk using FileSystemObject, or use

XML for Early Rendering is doing this job. Tips 4: Avoid cache non-light express components in Application or Session objects

Cache data in Application or Session object is a good way, however, caching COM objects have serious defects. This work is very attractive in Application or Session objects, which is very attractive, but very unfortunate, many COM objects, including object components written with Visual Basic 6.0 or previous versions, when stored in Application or Session objects It will produce a serious bottleneck problem.

In particular, it is possible to generate bottleneck problems when the component is written is not very light. A light component is a component that is marked with ThreadingModel = Both, where the free thread is arranged (FTM), or the ThreadingModel = Neutral (neutral mode is new features in Windows 2000 and COM ). The following components are not light:

Free-threaded components (unless set to ftm) Apartment-Threaded Components Single-Threaded Components Configured Components are not light components unless they are neutral-threaded. The Apartment-Threaded component and other non-light components work well within the page, that is, they are created and released in a single ASP page.

What happens if a non-light component is cached? Non-light components cached in the session object will "lock" sessions. ASP maintains a working thread pool that responds to requests, usually, new requests are controlled by the first available working thread. If a session is locked in a thread, the request is forced to wait for the related thread to become available. Here is a class ratio: You go to a supermarket, choose some food, and pay at the No. 3 payment. Since then, as long as you buy food in that supermarket, you will go to the No. 3 card, although there are fewer other payers.

Tips 5: Do not cache database connections in Application or Session objects

Cache ADO connection is usually not a good policy. If a connection object is stored in the Application object and is used on all pages, all pages will compete for the connection. If you are stored in the ASP Session object, you will open the database connection for each user. This will defeat the use of the connection pool and the unnecessary high cost pressure is applied to the web server and the database.

To replace the cache database connection, you can create and release the ADO object in each ASP page of ADO. This will be very effective because IIS has a built-in database connection pool. More specifically, IIS automatically processes OLEDB and ODBC connect pools, which will ensure efficient operation of each page created and released.

Since the connected record set stores a reference to the database connection, do not cache the connected record set in the Application or Session object. However, the record set of the disconnected type can be safely cached, and they do not save the reference for the corresponding database connection. In order to disconnect the record, do 2 steps below:

SET RS = Server.createObject ("AdoDb.Recordset")

rs.cursorlocation = aduseclient step 1

Populate the recordset with data

RS.Open Strquery, Strovnow Disconnect The Recordset from The Data Provider and Data Source

Rs.activeConnection = Nothing Step 2

For more information on connecting pools, please visit Ado and SQL Server.

转载请注明原文地址:https://www.9cbs.com/read-103892.html

New Post(0)