一、 引言
在web表單中,我們使用ajax來從客戶端(通過javascript)調用服務端方法,而在ajax內部則進行xmlhttprequest調用。我測試了一些以不同方式實現的ajax函數。另外,我還監控分析了進行ajax調用的性能和生命周期。結果,我發現在web表單中使用ajax時存在一些嚴重的問題。不過,我也找到了這些問題的一種解決方法。在本文中,我正是想與各位分析這一問題及其相應的解決方案。
二、 在使用ajax時所遇到的性能問題
對于每一個ajax調用來說,我們都要創建包含ajax方法的類的一個實例。另外,如果我們在類級上使用new關鍵字的話,我們還要為字段、屬性及其它類級的變量創建實例。
三、 實現方案
我創建了一個工程,它包含兩個web表單:webform1.aspx和webform2.aspx,還有一個類student.vb。這兩部分code-behind頁面都使用了一個ajax函數getdata()和一個student類型的公共變量。借助于mxlogger類,我記錄下每一個階段的執行流程。
注意:webform2.aspx的ajax函數getdata()是共享的,而在webform1中,它不是共享的。
'student.vb
public class student
sub new()
mxlogger.addlog("from student.constructor")
end sub
dim _name as string
public property name() as string
get
return _name
end get
set(byval value as string)
_name = value
end set
end property
end class
'webform1.aspx.vb
public class webform1
public student as new student
sub new()
mxlogger.addlog("from webform1.constructor")
end sub
<ajax.ajaxmethod(ajax.httpsessionstaterequirement.read)> _
public function getdata() as string
mxlogger.addlog("from webform1.ajax.getdata()")
return "i m a non shared function"
end function
end class
'webform2.aspx.vb
public class webform2
public student as new student
sub new()
mxlogger.addlog("from webform2.constructor")
end sub
<ajax.ajaxmethod(ajax.httpsessionstaterequirement.read)> _
public shared function getdata() as string
mxlogger.addlog("from webform2.ajax.getdata()")
return "i m a shared function"
end function
end class
四、 測試應用程序
· 測試用例1:
運行webform1.aspx并且從javascript中調用getdata() ajax函數三次。
· 測試用例2:
運行webform2.aspx并且從javascript中調用getdata()ajax函數三次。
對于上面的測試用例,我得到如下的日志輸出數據:
//請注意,為了解釋之目的,我在其中手工加入了一些日志行
log for the test case 1: ( non ajax shared function )
-------while loading the page--------
5/9/2006 10:37:29 am>>from student.constructor
5/9/2006 10:37:29 am>>from webform1.constructor
5/9/2006 10:37:29 am>>from webform1.ajax.getdata()
-------first call for getdata()--------
5/9/2006 10:37:29 am>>from student.constructor
5/9/2006 10:37:29 am>>from webform1.constructor
5/9/2006 10:37:29 am>>from webform1.ajax.getdata()
-------second call for getdata()--------
5/9/2006 10:37:29 am>>from student.constructor
5/9/2006 10:37:29 am>>from webform1.constructor
5/9/2006 10:37:29 am>>from webform1.ajax.getdata()
-------third call for getdata()--------
5/9/2006 10:37:30 am>>from student.constructor
5/9/2006 10:37:30 am>>from webform1.constructor
5/9/2006 10:37:30 am>>from webform1.ajax.getdata()
log for the test case 2: ( shared ajax function )
-------while loading the page--------
5/9/2006 10:37:09 am>>from student.constructor
5/9/2006 10:37:09 am>>from webform2.constructor
5/9/2006 10:37:09 am>>from webform2.ajax.getdata()
-------first call for getdata()--------
5/9/2006 10:38:11 am>>from webform2.ajax.getdata()
-------second call for getdata()--------
5/9/2006 10:38:11 am>>from webform2.ajax.getdata()
-------third call for getdata()--------
5/9/2006 10:38:11 am>>from webform2.ajax.getdata()
我們可以看到,在上面的日志輸出數據中,對于測試用例1來說,我們能夠看到更多的來自于webform1和student的構造器的日志數據。
五、 結論
我的建議是,在所有可能的地方,我們應該使用針對于ajax的共享方法,以便它不會創建更多的web表單實例和類級的字段。這樣以來,我們就可以減少從gc中調用finalize()的次數。
新聞熱點
疑難解答
圖片精選