Tips 9: Execution outside the process will sacrifice reliability
ASP and MTS/COM+ both have configuration options that allow you to trade reliability for performance. This exchange should be understood when creating and deploying applications.
ASP Options
ASP application can be configured to run in one of three ways. The term "isolation level" is introduced in IIS 5.0 to describe these options. The three isolation level values are low, medium and high:
Low-level isolation. This isolation level is supported in all versions of IIS and is the fastest. It executes ASP in the main IIS process. If the ASP application crashes, IIS will also crash. (To restart IIS under IIS 4.0, the Web site administrator needs to use tools such as InetMon to monitor the site. If the server fails, a batch file will be run to restart the server. IIS 5.0 introduces a reliable restart, which will automatically restart the failed server.)
Intermediate isolation. IIS 5.0 introduces this new isolation level, which is called out-of-process, because ASP runs outside of IIS processes. In intermediate isolation, all ASP applications configured to run as "intermediate" will share a single process space. This will reduce the number of processes required to run multiple off-process ASP applications on one server. Intermediate level is the default isolation level in IIS 5.0.
Advanced isolation. Supported in IIS 4.0 and IIS 5.0, advanced isolation is also out of process. If ASP crashes, the web server does not crash. The ASP application will automatically restart on the next ASP request. Using Advanced Isolation, each ASP application configured to run as Advanced will run in its own process space. This can protect ASP applications from interference with each other. Its disadvantage is that it requires establishing independent processes for each ASP application. There is a lot of overhead when you need to host more than a dozen applications on one server.
So, which option is the best? In IIS 4.0, running applications outside the process will greatly affect performance. In IIS 5.0, a lot of work has been done to minimize the performance impact of running ASP applications off-process. In fact, in most tests, ASP off-process applications in IIS 5.0 run faster than in-process applications in IIS 4.0. In any case, in-process (low isolation level) still produces the best performance on both platforms. However, if you have relatively low hit rate or have a low maximum throughput, choosing a low isolation level won't have much benefit. So, unless you need to process hundreds or thousands of pages per second per web server, there is no need to choose a low isolation level. Similarly, you should test multiple configurations and determine which situation is best for you.
Note: When you run ASP applications outside the process (intermediate or advanced isolation), they will run in MTS on NT4, and on Windows 2000, they will run in COM+. That is, they run in NT4, and they run in on Windows 2000. In Task Manager, you can see these running processes. You can also see how IIS configures MTS packages or COM+ applications for out-of-process ASP applications.
COM Options
The COM component also has three configuration options, although it is not exactly similar to the ASP option. COM components can be: "not configured", configured as "library application", or configured as "server application". "Not configured" means not registering components with COM+. Components will run in the caller's process space, that is, they are "in-process". "Library Applications" are also in-process, but benefit from COM+ services, including security, transactional and environmental support. The "server application" is configured to run in its own process space.
You may see that unconfigured components have slightly more advantages than library applications. You may also see that "library applications" have great performance advantages over "server applications". This is because the "library application" runs in the same process as ASP, while the "server application" runs in its own process. The overhead of internal process calls is much greater than that of in-process calls. Moreover, when passing data between processes (such as record sets), all data must be copied between the two processes.
shortcoming! When using the "COM Server Application", if you want to pass objects between ASP and COM, make sure that the objects implement "collection by value", i.e. MBV. The object that implements MBV copies itself from one process to another. This is better than the other way, where the object stays in the process that created it, while other processes repeatedly call the process that created the object. The disconnected ADO record set will be aggregated by value, while the connected record set will not. MBV is not implemented and will not be passed between processes. Finally, I want to tell VB programmers another way: MBV is not passed by parameters
ByVal
Obtained. MBV is implemented by the original component creator.
what to do?
If you want to complete your configuration with a reasonable exchange of performance and reliability, our recommendations are as follows:
On IIS 4.0, use the low isolation level of ASP and use the "MTS Server Package".
On IIS 5.0, use the medium isolation level of ASP and use the "COM+ Library Application".
These are very general guidelines; companies are usually allowed to run ASP at medium or high isolation levels, while single-purpose web servers can run at low isolation levels. Please weigh the trade-offs and decide on the configuration that meets your needs.
Tips 10: Explicitly use options
Use explicitly in .asp file
Options Explicit
. This directive placed at the beginning of the .asp file forces the developer to declare all variables to use. Many developers think this helps debug your application because it avoids accidentally creating new variables by typing incorrectly (e.g.
MyXLMString=...
Instead of
MyXMLString=)
。
Perhaps more importantly, declared variables are faster than undeclared variables. In fact, when the script is run, it is referenced by name every time an undeclared variable is used. The declared variables are assigned a sequence number at compile or runtime. In this way, declared variables are referenced according to this sequence number. because
Options Explicit
Force variable declarations, thus ensuring that all variables are declared for quick access.
Tips 11: Use local variables in subroutines and functions
Local variables are variables declared in subroutines and functions. In subroutines and functions, local variable access is faster than global variable access. Using local variables can also make the code clearer, so use local variables whenever possible.
Tips 12: Copy common data to script variables
When accessing COM in ASP, the commonly used object data should be copied into the script variable. This will reduce the call to the COM method, which is relatively expensive compared to accessing script variables. This technique can also cut down on expensive searches when accessing Collection and Dictionary objects.
Typically, if you intend to access object data multiple times, put the data into a script variable. The main goal of this optimization is the Request variable (Form and QueryString variables). For example, your site might pass a QueryString named UserID. Assume that the UserID variable should be referenced 12 times in a specific page. Please do not call
Request("UserID")
12 times, and at the beginning of the ASP page, UserID is assigned to a variable. Then use the variable in the page. This will save 11 COM method calls.
In practice, accessing COM properties or methods hides complex processes and a lot of overhead. Here is an example, it's just some pretty ordinary code (grammatically speaking):
= (1)
If = Then ' ...
When running this code, the following events will occur:
Variable
Foo
Resolved as a global variable.
Variable
bar
Resolved as
Foo.
members of the This will produce a COM method call.
Variable
blah
Resolved as
members of the This will also produce a COM method call.
Variable
qaz
Resolved as
members of the Yes, this will also produce a COM method call.
Call
(1)
. Another COM method call is generated. Do you understand this picture?
Execute steps 1 to 3 and will be parsed again
baz
. The system does not know how to call it
qaz
Whether to change the object model, so steps 1 to 3 must be parsed again
baz
。
Will
baz
Resolved as
members of the Perform attribute placement.
Execute steps 1 to 3 again and parse
zaq
。
Execute steps 1 to 3 again and parse
abc
。
As can be seen, it is very terrible inefficiency (and very slow). The quick way to write this code in VBScript is:
Set myobj = 'Do an analysis of blah
= (1)
If = Then '...
If you are using VBScript 5.0 or later, you can
With
To write this code with a statement:
With
.baz = .qaz(1)
If .zaq = .abc Then '...
...
End With
Please note that this technique is also effective for VB programming.
Tips 13: Avoid redefining arrays
Try to avoid
Redim
Array. From a performance-conscious perspective, if the computer is limited by physical memory, it is best to set the dimension of the array to the worst solution at the beginning instead of setting the dimension to the best solution, and then redefine the dimension as needed. This does not mean that you should allocate too much memory even though you know that you don't need that much.
The following code shows that you use it unnecessary
Dim
and
Redim
Let's solve it.
<%
Dim MyArray()
Redim MyArray(2)
MyArray(0) = "hello"
MyArray(1) = "good-bye"
MyArray(2) = "farewell"
...
' In some other code, here you don't need more space, then...
Redim Preserve MyArray(5)
MyArray(3) = "more stuff"
MyArray(4) = "even more stuff"
MyArray(5) = "yet more stuff"
%>
A better way is to just start
Dim
The array is of the correct size (5 in this case), not
Redim
Array, add the array. This may waste a little memory (if all elements are not exhausted), but what you get is speed.
Tips 14: Use response buffering
You can buffer the entire page worth output by opening the "response buffer". This minimizes the amount of data written to the browser, thereby improving overall performance. Each write will have a lot of overhead (including IIS and the amount of data sent over the cable), so the less you write, the better. The working efficiency of TCP/IP is significantly higher when sending a small number of large data blocks than when sending a large number of small data blocks, because of its low-speed startup and Nagling algorithm (used to minimize network blocking).
There are two ways to turn on response buffering. The first one is to use the "Internet Service Manager" to turn on response buffering for the entire application. This is the recommended method, in IIS 4.0 and IIS 5.0, by default, open response buffering for new ASP applications. The second type is to place the following lines of code pages at the beginning of the ASP page, thereby enabling response buffering:
<% = True %>
This line of code must be executed before any response data is written to the browser (that is, before any HTML appears in the ASP script and any cookies are used
before the collection is set). Generally, it is best to turn on response buffering for the entire application. This allows the omission of lines of code in each page above.
A common problem with response buffering is that users feel that ASP page is slow to respond (although overall response time has improved), because they need to wait until the entire page is generated before they can see the page. For long-running pages, you can set
= False
Turn off response buffering. However, a better strategy is to use
method. This method refreshes all HTML drawn into the browser by ASP. For example, after drawing 100 rows of a table with 1,000 rows, ASP can call
Forced to draw the result to the browser; this allows the user to see the first 100 rows before the remaining rows are ready. This technology gives you two unparalleled goodies – a combination of response buffering and step-by-step display of data in the browser.
(Note that in the above 1,000 row table example, many browsers, do not start drawing tables until they see the end tag. Please check the support of the target browser. To resolve this issue, split the table into multiple tables with fewer rows and call after each table
. The new version of Internet Explorer will draw the table before the table is fully downloaded, especially if the column width of the specified table is drawn faster; this avoids forcing Internet Explorer to calculate the column width by measuring the content of each cell. )
Another common problem with response buffering is the use of large amounts of memory on the server when generating large pages. For this problem, in addition to the skills to generate large pages, you can also use them cleverly.
Let's solve it.
Tips 15: Batch embedded scripts and statements
VBScript Syntax
<% = expression %>
Will"
Expressions
The value of " is written to the ASP output stream. If the response buffer is not turned on, each sentence of these statements will cause data to be written to the browser through the network, in the form of many small packages. This is very slow. Also, interpreting a small amount of scripts and HTML will result in switching between the script engine and HTML, which also reduces performance. So, use the following tip:
A call to replace the embedded dense combination expression. For example, in the following example, each row and each field has a write to the response stream, and each row has many switching between VBScript and HTML:
<table>
<% For Each fld in %>
<th><% = %></th>
<%
Next
While Not
%>
<tr>
<% For Each fld in %>
<td><% = %></td>
<% Next
</tr>
<%
Wend %>
</table>
Below is a more efficient code, with a write to the response stream in each line. All codes are included in a VBScript block:
<table>
<%
For each fld in
("<th>" & & "</th>" & vbCrLf)
Next
While Not
("<tr>")
For Each fld in %>
("<td>" & & "</td>" & vbCrLf)
Next
"</tr>"
Wend
%>
</table>
This technique works more when response buffering is disabled. It is best to enable response buffering and then observe batch processing
Is it helpful for performance?
(In this special case, the nested loop of the body of the table is constructed (
While Not ...
) can be replaced by carefully constructed calls to GetString. )
Tips 16: Use before starting a long task
If users lose patience, they can give up the ASP page before starting executing their request. If they click Refresh or jump to another page on the server, there will be a new request at the end of the ASP request queue and a disconnected request in the middle of the queue. This usually happens when the server is under high load (it has a long request queue and the corresponding response time is also long), which can only make the situation worse. If the user no longer connects, there will be no point to execute the ASP page (especially low-speed, heavyweight ASP page). Can be used
Attributes check this situation. If it returns
False
, then it should be called
And abandon the remaining content of the page. In fact, whenever ASP wants to execute a new request, IIS 5.0 encodes the method to check how long the request in the queue is. If there is more than 3 seconds, the ASP will check if the client is still connected and end the request immediately if the client is disconnected. You can use the
AspQueueConnectionTestTime
Set, adjust the timeout time of these 3 seconds.
If a page has been executed for a long time, you may also want to check at a certain time interval.
. After enabling response buffering, execute at a certain time interval.
, telling users what is going on is a good idea.
Note that in IIS 4.0,
Will not work properly unless executed first
. If buffering is enabled, it also needs to be executed
. This is not necessary in IIS 5.0-
Works very well. in any case,
All have some overhead, so use it only before executing an operation that takes at least 500 milliseconds (which is a long time to maintain throughput of dozens of pages per second). As a common rule, don't call it in every iteration of a tight loop, for example when drawing rows in a table, it may be called every 20 rows or every 50 rows.
Tips 17: Use <OBJECT> to mark instantiation objects
If you need to refer to objects that cannot be used in all code paths (especially server- or application-scope objects), use the
<object runat=server id=objname>
Tags to declare them, not using
method.
Create the object now. If you don't use that object in the future, don't waste resources.
<object id=objname>
The tag declares objname, but in fact, objname is not created at this time and is not created until its method or property is first used.
This is another example of slow calculations.
Tips 18: TypeLib declaration using ADO objects and other components
When using ADO, developers often include
to obtain access to different ADO constants. The file must be included in each page where you want to use these constants. This constant file is very large, adding a lot of overhead in terms of compilation time and script size to each ASP page.
IIS 5.0 provides the ability to bind to component type libraries. Allows you to reference the type library once on each ASP page and use it. Each page does not need to pay a price to compile constant files, and component developers do not have to generate VBScript #include files for use in ASP.
To access the ADO type library, put one of the following statements into it.
<!-- METADATA NAME="Microsoft ActiveX Data Objects 2.5 Library"
TYPE="TypeLib" UUID="{00000205-0000-0010-8000-00AA006D2EA4}" -->
or
<!-- METADATA TYPE="TypeLib"
FILE="C:\Program Files\Common Files\system\ado\" -->
Tips 19: Utilize browser verification capabilities
Popular browsers have advanced support for the following features, such as XML, DHTML, Java applets, and remote data services. Please try to make use of these features. All these technologies can reduce round trips with the Web server by performing client verification and data caching. If you are running a smart browser, the browser can do some verification for you (for example, checking if the checksum of your credit card is valid before running POST). Reiterate, try to use these features. As the client-to-server round trip is reduced, the pressure on the Web server will be reduced and network traffic is reduced (although the initial page sent to the browser may be larger), all backend resources accessed by the server are also reduced. Moreover, users do not need to extract new pages frequently, so that users can feel better. This does not alleviate the need for server-side verification. Server-side verification should still be carried out frequently. This prevents bad data from the client for some reason, such as hackers, or browsers that do not run client verification programs.
Many sites are composed of HTML created independently of the browser. This often hinders developers from taking advantage of popular browser features that can improve performance. For truly high-performance sites that have to care about your browser, a good strategy is to optimize your pages for popular browsers. Using "browser performance components" in ASP, it is easy to detect the functions of the browser. Tools such as Microsoft FrontPage can help you design and use the desired target browser and HTML version of your code. For a more detailed discussion, please check out When is Better Worse? Weighing the Technology Trade-Offs (English).
Tips 20: Avoid string concatenation in loops
Many people create strings like this in loops:
s = "<table>" & vbCrLf
For Each fld in
s = s & " <th>" & & "</th> "
Next
While Not
s = s & vbCrLf & " <tr>"
For Each fld in
s = s & " <td>" & & "</td> "
Next
s = s & " </tr>"
Wend
s = s & vbCrLf & "</table>" & vbCrLf
s
There are several problems with this method. First, the time taken to repeatedly connect the string grows at the rate of the quadratic curve; roughly calculated, the time taken to run the loop is proportional to the record number multiplied by the square of the number of fields. This can be clearly explained by giving a simple example.
s = ""
For i = Asc("A") to Asc("Z")
s = s & Chr(i)
Next
In the first iteration, get a string of characters
"A"
. In the second iteration, VBScript must reassign the string and copy two characters
"AB"
arrive
. In the third iteration, it must be reassigned again
and copy three characters to
. In the Nth (26th) iteration, it must reassign and copy N characters to
. It is the sum of 1+2+3+...+N, which is N*(N+1)/2 times.
In the above record set example, if there are 100 records and 5 fields, the internal loop will be executed 100*5=500 times, and the time it takes to complete all copying and reallocation will be proportional to 500*500=250,000. For a moderately sized record set, there will be many copies.
In this example, the code can be improved: the concatenation of the string will be
()
or embedded scripts (
<% = %>
) is replaced. If the response buffer is turned on, this operation will be very fast because
Just add data to the end of the response buffer. No redistribution anymore, so it is very effective.
Especially when converting ADO recordsets to HTML tables, consider using GetRows or GetString.
If you use JScript to connect strings, it is highly recommended to use
+=
Operator; ready to use
s += "A certain string",
Instead
s = s + "A certain string"
。
Tips 21: Enable browser and proxy caching
By default, ASP disables cache in browsers and proxy. This would make sense because ASP is born dynamic and has potentially time-sensitive information. If there is a page that does not require refreshing each view, browser and proxy caching should be enabled. This allows the browser and proxy to use a cached copy of a certain page for a certain period of time, and the length of this time can be controlled. Cache can significantly reduce server load and make users feel better.
Which dynamic page can be cached? Give an example:
Weather page, updated every 5 minutes.
List the homepage of the news or the homepage of the news release, and update 2 times a day.
Public fund operation list, basic statistics are updated 1 time every hour.
Please note that using browser or proxy cache, only a few hits are recorded on the web server. If you want to accurately measure all page viewing or post ads, you may not like using browser and proxy cache.
Browser cache is controlled by the HTTP expiration title sent to the browser by the Web server. ASP provides two mechanisms for sending titles. To set the page to expire after a certain number of minutes in the future, set
property. The following example notifies the browser: The content expires after 10 minutes:
<% = 10 %>
set up
If it is a negative number or 0, it will disable cache. Be sure to use a larger negative number, such as -1000 (greater than one day) to overcome the difference between the server clock and the browser clock. The second attribute
, allows setting the specified time for content expiration:
<% = #May 31,2001 13:30:15# %>
If you do not want to use the Response object to set the expiration time, you can write the <META> tag to HTML, usually inside the <HEAD> of the HTML file. Some browsers will respond to this instruction, but the proxy will not.
<META HTTP-EQUIV="Expires" value="May 31,2001 13:30:15">
Finally, you can identify whether the content is valid for the HTTP proxy cache. Please use
property. Set the property to "Public" to allow the proxy to cache content.
<% = "Public" %>
By default, this property is set to "Private". Note that proxy caching should not be enabled for pages that display data for a particular user, because proxy may serve user pages belonging to other users.
Tips 22: Use as much as possible Replace
Notify the browser to request a different page. This function is often used to redirect users to login or error pages. Since redirect forces a new page request, the browser must make two round trips to the Web server, and the Web server must process additional requests. IIS 5.0 introduces a new function,
, This function executes a different ASP page sent to the same server. This avoids additional round trips from the browser to the Web server, thereby improving overall system performance and improving response time to users. Please see the new direction in the redirection (in English), which discusses
and
。
You can also view a complete list of new features in Leveraging ASP in IIS 5.0 and ASP 3.0. (English)
Tips 23: Add a slash at the end of the directory URL
The relevant trick is to add a slash at the end of the URL pointing to the directory.
(/)
. If the slash is omitted, the browser will make a request to the server, notifying it that it is looking for a directory. The browser then issues a second request, adds a slash at the end of the URL, and the server responds with the default document of that directory, or if there is no default document and directory browsing is enabled, the directory list is responded. Adding a slash saves the first useless round trip. For user-friendliness, maybe you want to omit the slash at the end of the displayed name.
For example, write:
<a href="/workshop/&quo ... uot;MSDN Web
Workshop">/workshop</a>
It also works with URLs pointing to the web site homepage: Please use the following: <a href="/">, do not use <a href="">.
Tips 24: Avoid using server variables
Accessing the server variable will cause the Web site to make a special request to the server, and then collect all server variables, not just the ones you need. It's like retrieving a special piece of information from a folder in a moldy attic. When you want a piece of information, you must first go to the attic to get the folder before accessing the information. This is the same as when the performance access occurs when the server variable is requested. Subsequent access to other server variables will not cause performance access.
Never access a failed Request object (e.g.
Request("Data")
). For not being there
、
、
or
The item in it is correct
implicit call of .
Collections are much slower than other collections.
Previous page12Read the full text