如何降低CPU消耗

wtogether 2006-11-13 12:20:21
最近做了个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

...全文
678 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wtogether 2006-11-14
  • 打赏
  • 举报
回复
还有建设性的意见么
chaoliu1 2006-11-13
  • 打赏
  • 举报
回复
全部或部分事先生成静态网页,可以吗?
wtogether 2006-11-13
  • 打赏
  • 举报
回复
Private Sub XMLStart()
Select Case strDoctype
Case "wml"
Printf "<?xml version=""1.0"" encoding=""utf-8""?>"
Printf "<!DOCTYPE wml PUBLIC ""-//WAPFORUM//DTD WML 1.1//EN"" ""http://www.wapforum.org/DTD/wml_1.1.xml"">"
Printf "<wml>"
Case "html"
Printf "<?xml version=""1.0"" encoding=""utf-8""?>"
Printf "<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">"
Printf "<html xmlns=""http://www.w3.org/1999/xhtml"">"
Case "xhtml"
Printf "<?xml version=""1.0"" encoding=""utf-8""?>"
Printf "<!DOCTYPE html PUBLIC ""-//WAPFORUM//DTD XHTML Mobile 1.0//EN"" ""http://www.wapforum.org/DTD/xhtml-mobile10.dtd"">"
Printf "<html xmlns=""http://www.w3.org/1999/xhtml"">"
End Select
End Sub

Private Sub XMLEnd()
If strDoctype = "wml" Then
Printf "</wml>"
Else
Printf "</html>"
End If
End Sub

Public Sub HeadStart()
If strDoctype <> "wml" Then
Printf Replace("<title>$(Title)</title>", "$(Title)", Server.HTMLEncode(strTitle))
End If
Printf "<head>"
End Sub

Public Sub HeadEnd()
Printf "</head>"
End Sub

Public Sub SetMeta(strName, strValue)
Dim strTmp
strTmp = "<meta http-equiv=""$(Name)"" content=""$(Value)"" />"
strTmp = Replace(strTmp, "$(Name)", Server.HTMLEncode(strName))
strTmp = Replace(strTmp, "$(Value)", Server.HTMLEncode(strValue))
Printf strTmp
End Sub

Public Sub BodyStart()
If strDoctype = "wml" Then
Printf Replace("<card id=""index"" title=""$(Title)"">", "$(Title)", Server.HTMLEncode(strTitle))
Else
Printf "<body>"
End If
End Sub

Public Sub BodyEnd()
Printf "</p>"
If strDoctype = "wml" Then
Printf "</card>"
Else
Printf "</body>"
End If
End Sub

Public Property Let Align(strIn)
Dim strTmp
strTmp = LCase(strIn)
If IsEmpty(strAlign) Then
strAlign = strTmp
Printf Replace("<p align=""$(Align)"">", "$(Align)", strAlign)
ElseIf strAlign <> strTmp Then
strAlign = strTmp
Printf "</p>"
Printf Replace("<p align=""$(Align)"">", "$(Align)", strAlign)
End If
End Property

Public Function T(strIn)
If strDoctype = "wml" Then
T = Replace(Server.HTMLEncode(strIn), "$", "$$")
Else
T = Server.HTMLEncode(strIn)
End If
End Function

Public Function A(strHref, strText, varImg)
Dim strTmp
strTmp = "<a href=""$(Href)"" title=""确定"">$(Img)$(Text)</a>"
strTmp = Replace(strTmp, "$(Href)", Server.HTMLEncode(strHref))
strTmp = Replace(strTmp, "$(Text)", Server.HTMLEncode(strText))
If Left(varImg, 1) = "<" Then
strTmp = Replace(strTmp, "$(Img)", varImg)
ElseIf varImg <> "" Then
strTmp = Replace(strTmp, "$(Img)", Img(varImg, ""))
Else
strTmp = Replace(strTmp, "$(Img)", "")
End If
A = strTmp
End Function

Public Function Img(strSrc, strAlt)
Dim strTmp
strTmp = "<img src=""$(Src)"" alt=""$(Alt)"" />"
strTmp = Replace(strTmp, "$(Src)", Server.HTMLEncode(strSrc))
strTmp = Replace(strTmp, "$(Alt)", Server.HTMLEncode(strAlt))
Img = strTmp
End Function

Public Sub FormStart(strAct, strMet, strEnc)
If strDoctype = "wml" Then
strFormTemp = "<anchor title=""确定"">$(Submit)<go href=""$(Action)"" method=""$(Method)"">$(Postfield)</go></anchor>"
strFormTemp = Replace(strFormTemp, "$(Action)", Server.HTMLEncode(strAct))
strFormTemp = Replace(strFormTemp, "$(Method)", LCase(strMet))
Else
Dim strTmp
strTmp = "<form action=""$(Action)"" method=""$(Method)"" enctyped=""$(Enctype)"">"
strTmp = Replace(strTmp, "$(Action)", Server.HTMLEncode(strAct))
strTmp = Replace(strTmp, "$(Method)", LCase(strMet))
strTmp = Replace(strTmp, "$(Enctype)", LCase(strEnc))
Printf strTmp
End If
End Sub

Public Sub FormEnd()
If strDoctype <> "wml" Then
Printf "</form>"
End If
End Sub

Public Function Input(strName, strType, strValue)
Dim strTmp
If strDoctype = "wml" And LCase(strType) = "submit" Then
strTmp = Replace(strFormTemp, "$(Submit)", strValue)
strTmp = Replace(strTmp, "$(Postfield)", "")
Else
If strDoctype = "wml" Then
strTmp = "<postfield name=""$(Name)"" value=""$($(Name))"" />$(Postfield)"
strTmp = Replace(strTmp, "$(Name)", Server.HTMLEncode(strName))
strFormTemp = Replace(strFormTemp, "$(Postfield)", strTmp)
End If
strTmp = "<input name=""$(Name)"" type=""$(Type)"" value=""$(Value)"" />"
strTmp = Replace(strTmp, "$(Name)", Server.HTMLEncode(strName))
strTmp = Replace(strTmp, "$(Type)", LCase(strType))
strTmp = Replace(strTmp, "$(Value)", Server.HTMLEncode(strValue))
End If
Input = strTmp
End Function

Public Sub SelectStart(strName, strPrefix)
Dim strTmp
strTmp = "$(Prefix)<select name=""$(Name)"">"
strTmp = Replace(strTmp, "$(Prefix)", Server.HTMLEncode(strPrefix))
strTmp = Replace(strTmp, "$(Name)", Server.HTMLEncode(strName))
Printf strTmp
End Sub

Public Sub SelectEnd(strSuffix)
Println Replace("</select>$(Suffix)", "$(Suffix)", Server.HTMLEncode(strSuffix))
End Sub

Public Sub Options(strValue, strText)
Dim strTmp
strTmp = "<option value=""$(Value)"">$(Text)</option>"
strTmp = Replace(strTmp, "$(Value)", Server.HTMLEncode(strValue))
strTmp = Replace(strTmp, "$(Text)", Server.HTMLEncode(strText))
Printf strTmp
End Sub
End Class

Dim xml
Set xml = New IXML
xml.Title = "移动门户"
xml.SetPrefix "wml"
xml.HeadStart
xml.SetMeta "Cache-Control", "no-cache"
xml.SetMeta "Cache-Control", "max-age=0"
xml.HeadEnd
xml.BodyStart
xml.Align = "left"
xml.Println xml.T("<欢迎访问移动门户>")
xml.Println xml.A("http://localhost", "WAP", "http://localhost/images/logo.gif")
xml.Align = "left"
xml.FormStart "test.asp", "post", ""
xml.Println "姓名:" & xml.Input("Name", "text", "")
xml.Println "密码:" & xml.Input("Pass", "password", "")
xml.SelectStart "Sex", "性别:"
xml.Options "0", "女"
xml.Options "1", "男"
xml.SelectEnd ""
xml.Println xml.Input("", "submit", "提交数据")
xml.FormEnd
xml.Println xml.T(Request.ServerVariables("QUERY_STRING"))
xml.Align = "center"
xml.Printf "[分类频道]"
xml.Align = "left"
xml.Printf xml.A("http://xxxx", "xxxx", "")
xml.Printf "|"
xml.Printf xml.A("http://yyyy", "yyyy", "")
xml.Printf "|"
xml.Println xml.A("http://zzzz", "zzzz", "")
xml.BodyEnd
Set xml = Nothing
%>


本来想把核心模块都移植成COM的,但是根据2.asp的CPU消耗,很是踌躇
Apq001 2006-11-13
  • 打赏
  • 举报
回复
如何降低CPU消耗?

以下标准答案
---------------------------------------->








































关机。
wtogether 2006-11-13
  • 打赏
  • 举报
回复
to chaoliu1(潮流)
部分静态也测试过的,把用户变量放在静态文件中,然后用stream读出文件再把用户变量加进去,这个也占用CPU的很,60左右的CPU,首页比较好处理,用Application缓存了的,这个没什么问题,就是其他带有用户验证的分页面和文章比较占CPU

to yaozhg(恋妹是不对的)
现在的服务器配置是2个Xeon 2.4G超线程(dxdiag里显示是4个CPU),1GDDR,内存不怎么耗,就是IIS耗CPU

to zsrui(机箱)
数据库没什么问题,索引都有,就是静态页面无法应用,因为没COOKIE,用户信息需要用QUERY_STRING传

下午顺便做了下其他语言的测试
asp.net的Response.Write("Hello World!");占用30个CPU,不过命名空间倒没有CPU增长,用System.Xml创建XmlDocument并且输出xml也是30个CPU,比ASP的COM好用

Apache 2.0.59 + PHP 5.2.0的echo "Hello World!";是50个CPU

Apache 2.0.59 + Tomcat 6.0.1的out.println("Hello World!");是Apache 30个CPU,Tomcat 20个CPU
zsrui 2006-11-13
  • 打赏
  • 举报
回复
优化思路:能索引的索引,能静态的静态,能取消的取消
yaozhg 2006-11-13
  • 打赏
  • 举报
回复
如果是这样,那就增加CPU和内存。。。


BAYERN 2006-11-13
  • 打赏
  • 举报
回复
深奥..

UP之

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧