前一段時(shí)間有反映說(shuō)是一個(gè)使用了struts2的生產(chǎn)系統(tǒng)的頁(yè)面顯示速度太慢。登錄后發(fā)現(xiàn)確實(shí)如此,于是進(jìn)行了一番性能調(diào)優(yōu)的研究和測(cè)試。
一,根據(jù)struts2官方的性能調(diào)優(yōu)說(shuō)明進(jìn)行了一些參數(shù)的修改。
http://struts.apache.org/2.x/docs/performance-tuning.html
http://cwiki.apache.org/WW/performance-tuning.html
Turn off logging and devMode.(關(guān)閉logging和Devmode)
這個(gè)當(dāng)然沒(méi)問(wèn)題,但是全部關(guān)閉logging不現(xiàn)實(shí),我只是關(guān)閉了struts2相關(guān)package的logging
Do not use interceptors you do not need.
把struts.xml中不需要的interceptor統(tǒng)統(tǒng)刪除
Use the correct HTTP headers (Cache-Control & Expires).
不確定應(yīng)該如何修改
Copy the static content from the Struts 2 jar when using the Ajax theme (Dojo) or the Calendar tag.
關(guān)于這點(diǎn),后面會(huì)提到
Create a freemarker.properties file in your WEB-INF/classes directory.
照做
Create the freemarker.properties file and add the following setting (or whatever value you deem fitting):
template_update_delay=60000
照做
Enable Freemarker template caching
As of Struts 2.0.10, setting the property struts.freemarker.templatesCache to true will enable the Struts internal caching of Freemarker templates. This property is set to false by default.
照做
進(jìn)行上述修改后,發(fā)現(xiàn)頁(yè)面打開(kāi)的速度并沒(méi)有明顯的提高.
二,此時(shí)我已經(jīng)基本鎖定網(wǎng)頁(yè)打開(kāi)速度慢的原因與ajax(或者說(shuō)是dojo)有關(guān)。因?yàn)閐ojo的js庫(kù)大概有450K左右,先嘗試使用gzip壓縮javascript,減小傳輸量,看能否加快頁(yè)面的加載速度
在Tomcat的server.xml的connector中添加如下配置,激活gzip功能
compression="on"
compressionMinSize="2048"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml,text/javascript,application/x-javascript,application/javascript"
進(jìn)行上述修改后,發(fā)現(xiàn)頁(yè)面打開(kāi)的速度還是沒(méi)有明顯的提高.
三,經(jīng)過(guò)上述兩個(gè)實(shí)驗(yàn),覺(jué)得應(yīng)該是struts2所封閉的dojo的性能問(wèn)題了。于是引入JQuery.
JQuery的js文件最小是55K, gzip后應(yīng)該更小,頁(yè)面的響應(yīng)速度明顯改善(一個(gè)數(shù)量級(jí)以上的提高),主要原因在于與服務(wù)器交互的處理上極大地提升了效率。而且頁(yè)面處理代碼更加簡(jiǎn)潔明了。
最后,我刪除了所有的<s:head theme="ajax"/>和 <s:head/>(如果頁(yè)面中加入<s:head />,那么在Struts2生成的html中后包含dojo.js),使用JQuery來(lái)完成所有的Ajax和javascript功能。