這一個個案遇到的是 XML 的內容就 71mb, 含 DOM object 一起的話RAM 大約占用 150mb, 在 apply style 套 xsl 時, 瞬間的 RAM 再會多用掉500多MB, 像寫的這麼爛的程式, 真的很怕有很多人同時點下去執行, 應該要把Web Server 弄掉 memory overflow 錯誤.
有圖有真相, 畫面如下:
測試方案1號: 使用 ASP 寫的 StringBuilder(cASPString.asp),
圖片1.1: 程式執行前的RAM, 大約用掉 127mb:
圖片1.2: 程式執行前的RAM, DOM 大約用掉 150mb 左右:
(說明: 由於沒有在用 RAM 回收的太快, 所以數字不太準.)
測試方案2號: 使用 ASP 呼叫 .Net StringBuilder
圖片2.1: 程式執行前的RAM, 大約用掉 133mb:
圖片2.2: 程式執行前的RAM, DOM 大約用掉 150mb 左右:
在 DOM ready 好的時候, 可以看到 RAM 的使用快速成長, 會從 300mb 變 500, 再長大到 700mb, 最後在 860mb 右右停留 2~3秒, 再回來 200MB 左右.
測試結論: 在 Asp 使用 StringBuilder(cASPString.asp), 和呼叫 .Net 的 String Builder 效能上差不多, 主要的瓶頸在 XML 太大.
'====================================
' MODULE: cASPString.asp
' AUTHOR: www.u229.no
' CREATED: May 2006
'====================================
' COMMENT: A fast string class for classic ASP.
'====================================
' ROUTINES:
' - Public Property Get NumberOfStrings()
' - Public Property Get NumberOfBytes()
' - Public Property Get NumberOfCharacters()
' - Private Sub Class_Initialize()
' - Public Sub Append(sNewString)
' - Public Function ToString()
' - Public Sub Reset()
'====================================
'====================================
Class ASPStringBuilder
'====================================
'// MODULE VARIABLES
Dim m_sArr '// Array holding the strings.
Dim m_lResize '// Factor for rezising the array.
Dim m_lStrings '// Number of strings appended.
'// PROPERTIES
Public Property Get NumberOfStrings()
NumberOfStrings = m_lStrings
End Property
Public Property Get NumberOfBytes()
NumberOfBytes = LenB(Join(m_sArr, ""))
End Property
Public Property Get NumberOfCharacters()
NumberOfCharacters = Len(Join(m_sArr, ""))
End Property
'------------------------------------------------------
' Comment: Initialize default values.
'------------------------------------------------------
Private Sub Class_Initialize()
m_lResize = CLng(50)
m_lStrings = CLng(0)
ReDim m_sArr(m_lResize)
End Sub
'------------------------------------------------------
' Comment: Add a new string to the string array.
'------------------------------------------------------
Public Sub Append(sNewString)
If Len(sNewString & "") = 0 Then Exit Sub
'// If we have filled the array, resize it.
If m_lStrings > UBound(m_sArr) Then ReDim Preserve m_sArr(UBound(m_sArr) + m_lResize)
'// Append the new string to the next unused position in the array.
m_sArr(m_lStrings) = sNewString
m_lStrings = (m_lStrings + 1)
End Sub
'------------------------------------------------------
' Comment: Return the strings as one big string.
'------------------------------------------------------
Public Function ToString()
ToString = Join(m_sArr, "")
End Function
'------------------------------------------------------
' Comment: Reset everything.
'------------------------------------------------------
Public Sub Reset()
Class_Initialize
End Sub
'====================================
End Class
'====================================
讓 asp call .net string builder 的方法:
namespace MyObject
{
public class StringBuilderObject
{
public StringBuilder MyStringBuilder = new StringBuilder("");
public StringBuilderObject()
{
}
public void Append(string value)
{
this.MyStringBuilder.Append(value);
}
public void Remove(int startIndex, int length)
{
this.MyStringBuilder.Remove(startIndex, length);
}
public override string ToString()
{
return this.MyStringBuilder.ToString();
}
}
}





沒有留言:
張貼留言