Skills | performance
28 tips to improve ASP performance and appearanceLen Cardinal, Senior Advisor, Microsoft Consulting Services George v. Reilly Microsoft IIS performance lead adapted from Nancy Cluts article developer technical Worker Cheng Microsoft Corporation April 2000
Summary:This article describes techniques for optimizing ASP applications and VBScript.
Introduction
- Performance is a feature. You must design the performance beforehand, or you will have to rewrite the application later. That is, what are some good strategies for enabling Active Server Pages (ASP) application performance to be optimal? This article describes techniques for optimizing ASP applications and Visual basic®scripting Edition (VBScript). This article discusses a number of pitfalls. The recommendations listed in this article are already in http://www.microsoft.com and other sites have been tested, the effect is very significant. This article assumes that you have developed ASP, including VBScript and/or JScript, ASP application, ASP sessions, and other ASP-intrinsic objects (Request、ResponseAndServer) has a basic understanding. Typically, ASP performance is largely dependent on a number of factors outside the ASP code itself. We don't list all the information in an article, and at the end of this article we list the resources that are related to performance. These links cover ASP and non-ASP topics, including ActiveX® Data Objects (ADO), Component Object Model (COM), database, and Internet information Server (IIS) configuration. These are some of our favorite links-be sure to go and see.
Tip 1: Cache frequently used data on a WEB server
A typical ASP page retrieves data from the back-end data store and then converts the results to Hypertext Markup Language (HTML). Retrieving data from memory is much faster than retrieving data from the back-end data store, regardless of the speed of the database. Reading data from a local hard disk is usually faster than retrieving data from the database. As a result, you can typically cache data on a WEB server (stored in memory or on a disk) to improve performance. Caching is a traditional way of exchanging time in space. If you're caching the right content, you can see a significant improvement in performance. For caching to be valid, data that is frequently reused must be saved, and a large (modest) overhead is needed to recalculate the data. If all of the cached data is stale, it can cause memory waste. Data that is infrequently changed is a good candidate for caching because you do not have to worry about synchronizing the data with the database over time. combo box lists, reference tables, DHTML fragments, Extended Markup Language (XML) strings, menu items, and site configuration variables, including data source names (DSNs), Internet Protocol (IP) addresses, and Web paths, are good candidates for caching. Note that you can cache the "representation" of the data without caching the data itself. If ASP pages are rarely changed and the cache is expensive (for example, the entire product catalog), you should consider producing HTML in advance, rather than showing it again in response to each request. Where should the data be cached, and what caching policies are there? Typically, data is cached on the WEB server's memory or disk. The next two tips tell the two ways.Tip 2: Cache frequently used data in a application or Session object
AspApplicationAnd SessionObject provides a convenient container for caching data in memory. You can assign data toApplicationAnd Session object, the data is kept in memory between HTTP calls. Session data is stored separately for each user, while application data is shared among all users. When will the data be loaded into the application or session? Typically, data is loaded when the application or session is started. To load data during the application or session startup process, add the appropriate code to either Application_OnStart () or Session_OnStart (). These functions should be in Global.asa, and if they are not, you can add them. You can also mount the data the first time it is needed. To do this, add some code (or write a reusable script function) to the ASP page to check whether the data exists, and if not, load the data. This is a traditional performance technique, called lazy computing, that does not calculate a value until you know it is needed. For example, you can write a similar function for each block of data that you want. In what format should the data be stored? You can store any variant type, because all script variables are variant types. For example, you can store strings, integers, or arrays. Typically, you will store the contents of an ADO recordset as one of these variable types. To get data from an ADO recordset, you can manually copy the data to a VBScript variable, one field at a time. Using an ADO recordset Persistence function GetRows (), GetString (), or Save () (ADO 2.5) can be faster and easier. The details are beyond the scope of this article, but here is an example of a function that returns an array of recordset data using GetRows (): ' Get Recordset, return as a array Function fetchemploymentst Atuslist 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 a Array Rs. The close Set rs = The Nothing End Function makes further improvements to the above examples,To cache HTML as a list instead of an array. Here is a simple example: ' Get Recordset, return as HTML Option list Function fetchemploymentstatuslist Dim RS, fldname, s Set rs = creat Eobject (? ADODB. Recordset?) Rs. Open? Select Statusname, Statusid from Employeestatus?, _? dsn=employees;uid=sa;pwd=;? s =?? & vbCrLf Set fldname = Rs. Fields (? Statusname?) ' ADO Field Binding do Until Rs. EOF ' Next line violates Don ' t do String concats, ' but it ' OK because we are building a cache S = S &?? & Fldname &?? & VbCrLf Rs. MoveNext Loop s = S &?? & VbCrLf Rs. Close Set rs = Nothing ' Find release Early fetchemploymentstatuslist = S ' return data as a String end Function under appropriate conditions, you can Caches the ADO recordset itself in the application or session scope. There are two warnings:
- ADO must be marked as free thread
- You must use a disconnected recordset.
If these two requirements are not guaranteed, do not cache the ADO recordset. In the following "non-agile components" and "Do not cache connections" techniques, we will discuss the dangers of storing COM objects in the application or session scope. When you store data in the application or session scope, the data remains there until you programmatically change it, session expiration, or the WEB application restarts. What if the data needs to be updated? To manually force updates to application data, you can access ASP pages that only administrators can access to update the data. Alternatively, you can automatically refresh the data periodically through functions. The following example stores a timestamp with cached data and refreshes the data after a period of time. Session[/b] orApplicationIt is not a good practice to cache large arrays in an object. The syntax of the scripting language requires that the entire array be temporarily copied before any elements of the array are accessed. For example, if a 100,000-element array consisting of strings (which maps a U.S. ZIP code to a local weather station) is cached in theApplicationObject, the ASP must first copy all 100,000 meteorological stations into the staging array before extracting a string. In this case, it would be better to use a custom method to build a custom component to store the weather station-or a dictionary component. Again, don't pour the baby out with the bath water: The array can be quickly traced and stored in memory as a neighboring key data pair. Index A dictionary the peso is much slower to draw an array. Choose the data structure that provides the best performance for your situation.Tip 3: Cache data and HTML on a WEB server's disk
Sometimes, there may be too much data to slow down in memory. "Too much" is just a saying, it depends on how much memory you want to consume, and how many items to cache and how often to retrieve them. In any case, if there is too much data to slow down in memory, consider caching the data in a text or XML file on the WEB server's hard disk. You can cache data in both disk and memory, creating the most appropriate caching strategy for your site. Note When you measure the performance of a single ASP page, retrieving the data on the disk may not necessarily be faster than retrieving data from the database. However, caching reduces the load on the database and on the network. In the case of high load, this can greatly improve overall throughput. This is useful when caching a large query result, such as a multiple-table join or a composite stored procedure, or a large result set. As always, try to test the pros and cons of several schemes. ASP and COM provide some tools for building disk-based caching schemes. ADO recordsetSave ()AndOpen ()function to save and mount a recordset on a disk. You can use these methods to rewrite the code examples in the above application data caching techniques, using the file'sSave ()Instead of writing toApplicationThe code in the object. There are other components that can be used for files:
- Scripting.FileSystemObject enables you to create, read, and write files.
- The Microsoft®xml parser (MSXML) provided with Internet Explorer supports saving and loading XML documents.
- LookupTable objects (for example, on MSN) are the best choice for loading a simple list from disk.
Finally, you should consider caching the representation of the data on disk rather than the data itself. Pre-converted HTML can be stored on disk with. htm or. asp files, and hyperlinks can point directly to those files. You can automate the process of producing HTML using commercial tools such as Xbuilder, or the Microsoft®sql server™internet Publishing feature. Alternatively, you can place an HTML code fragment in an. asp file. You can also useFileSystemObjectRead HTML files from disk, or convert them using XML as early as possible.Tip 4: Avoid caching non agile components in a application or Session object
Although the data is cached in theApplicationOr <>< p="">
<>