如何降低CPU消耗
最近做了个WAP项目,访问的人一多,CPU就100,但是由于终端限制,无COOKIE,无Javascript,无框架,所以静态页面是没法用的,同时站点里多数页面都得有用户权限检测,所以搜索用户表是怎么也偷不了鸡的,就算不检测,同样需要把用户信息通过QUERY_STRING传递到下个需要检测的页面。
于是做了如下测试,测试的频率都是一样的:100个并发,每个并发20个请求,间隔100毫秒,这样的频率最多相当1000个并发
1.asp
<%@language="vbscript"%>
<%
Option Explicit
Response.Write "Hello World!"
%>
CPU在30左右
2.asp
<%@language="vbscript"%>
<%
Option Explicit
Dim xmlDoc
Set xmlDoc = Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
xmlDoc.insertBefore xmlDoc.createProcessingInstruction("xml", "version=""1.0"" encoding=""utf-8"""), xmlDoc.childNodes(0)
xmlDoc.appendChild xmlDoc.createElement("root")
xmlDoc.Save Response
Set xmlDoc = Nothing
%>
CPU在40左右
3.asp,是一个简单VBS类,无任何COM,纯String操作,用到If Then/Select Case结构,只有Server.HTMLEncode/InStr/LCase/Replace/CBool/Left这些系统函数,没有&连接,CPU就在60-70间徘徊了,加上并发测试程序的20个CPU以及其他系统的CPU,整个系统已经100个CPU了
<%@language="vbscript"%>
<%Option Explicit%>
<%
Class IXML
Private blnCodePage
Private strDoctype
Private strTitle
Private strAlign
Private strFormTemp
Private Sub Class_Initialize()
blnCodePage = CBool(InStr(Request.ServerVariables("SERVER_SOFTWARE"), "5.0") = 0)
Response.Charset = "utf-8"
If blnCodePage Then
Response.CodePage = 65001
End If
strTitle = "WAP"
End Sub
Private Sub Class_Terminate()
Call XMLEnd
End Sub
Public Property Let Title(strIn)
strTitle = strIn
End Property
Public Sub SetPrefix(strIn)
Dim strAccept
strAccept = Request.ServerVariables("HTTP_ACCEPT")
strDoctype = LCase(strIn)
Select Case strDoctype
Case "wml"
Response.ContentType = "text/vnd.wap.wml"
Case "html"
Response.ContentType = "text/html"
Case "xhtml"
If InStr(strAccept, "application/vnd.wap.xhtml+xml") <> 0 Then
Response.ContentType = "application/vnd.wap.xhtml+xml"
ElseIf InStr(strAccept, "application/xhtml+xml") <> 0 Then
Response.ContentType = "application/xhtml+xml"
Else
Response.ContentType = "text/html"
strDoctype = "html"
End If
Case Else
If InStr(strAccept, "application/vnd.wap.xhtml+xml") <> 0 Then
Response.ContentType = "application/vnd.wap.xhtml+xml"
strDoctype = "xhtml"
ElseIf InStr(strAccept, "application/xhtml+xml") <> 0 Then
Response.ContentType = "application/xhtml+xml"
strDoctype = "xhtml"
ElseIf InStr(strAccept, "text/vnd.wap.wml") <> 0 Then
Response.ContentType = "text/vnd.wap.wml"
strDoctype = "wml"
Else
Response.ContentType = "text/html"
strDoctype = "html"
End If
End Select
Call XMLStart
End Sub
Public Sub Echo(varIn)
Printf varIn
Printf vbCrLf
End Sub
Public Sub Printf(varIn)
If blnCodePage = False Then
Response.BinaryWrite ConvertStringToUTF8(varIn)
Else
Response.Write varIn
End If
End Sub
Public Sub Println(varIn)
Printf varIn
Printf "<br/>"
End Sub
Private Function ConvertStringToUTF8(strIn)
Dim ret
Dim i
Dim uByte
Dim lByte(2)
For i = 1 To Len(strIn)
uByte = AscW(Mid(strIn, i, 1))
If uByte < 0 Then uByte = uByte + 65536
If uByte < 128 Then
ret = ret & ChrB(uByte)
ElseIf uByte < 2048 Then
lByte(1) = &H80 Xor (uByte And &H3F)
uByte = uByte \ (2 ^ 6)
lByte(0) = &HC0 Xor (uByte And &H1F)
ret = ret & ChrB(lByte(0)) & ChrB(lByte(1))
Else
lByte(2) = &H80 Xor (uByte And &H3F)
uByte = uByte \ (2 ^ 6)
lByte(1) = &H80 Xor (uByte And &H3F)
uByte = uByte \ (2 ^ 6)
lByte(0) = &HE0 Xor (uByte And &HF)
ret = ret & ChrB(lByte(0)) & ChrB(lByte(1)) & ChrB(lByte(2))
End If
Next
ConvertStringToUTF8 = ret
End Function