前一段時間有反映說是一個使用了struts2的生產系統(tǒng)的頁面顯示速度太慢。登錄后發(fā)現(xiàn)確實如此,于是進行了一番性能調優(yōu)的研究和測試。
一,根據(jù)struts2官方的性能調優(yōu)說明進行了一些參數(shù)的修改。
http://struts.apache.org/2.x/docs/performance-tuning.html
http://cwiki.apache.org/WW/performance-tuning.html
Turn off logging and devMode.(關閉logging和Devmode)
這個當然沒問題,但是全部關閉logging不現(xiàn)實,我只是關閉了struts2相關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).
不確定應該如何修改
Copy the static content from the Struts 2 jar when using the Ajax theme (Dojo) or the Calendar tag.
關于這點,后面會提到
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.
照做
進行上述修改后,發(fā)現(xiàn)頁面打開的速度并沒有明顯的提高.
二,此時我已經基本鎖定網頁打開速度慢的原因與ajax(或者說是dojo)有關。因為dojo的js庫大概有450K左右,先嘗試使用gzip壓縮javascript,減小傳輸量,看能否加快頁面的加載速度
在Tomcat的server.xml的connector中添加如下配置,激活gzip功能
compression="on"
compressionMinSize="2048"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml,text/javascript,application/x-javascript,application/javascript"
進行上述修改后,發(fā)現(xiàn)頁面打開的速度還是沒有明顯的提高.
三,經過上述兩個實驗,覺得應該是struts2所封閉的dojo的性能問題了。于是引入JQuery.
JQuery的js文件最小是55K, gzip后應該更小,頁面的響應速度明顯改善(一個數(shù)量級以上的提高),主要原因在于與服務器交互的處理上極大地提升了效率。而且頁面處理代碼更加簡潔明了。
最后,我刪除了所有的<s:head theme="ajax"/>和 <s:head/>(如果頁面中加入<s:head />,那么在Struts2生成的html中后包含dojo.js),使用JQuery來完成所有的Ajax和javascript功能。