云自无心水自闲

天平山上白云泉,云自无心水自闲。何必奔冲山下去,更添波浪向人间!
posts - 288, comments - 524, trackbacks - 0, articles - 6
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

我的struts2项目性能调优三步曲

Posted on 2008-07-01 13:33 云自无心水自闲 阅读(6657) 评论(7)  编辑  收藏 所属分类: Java心得体会Struts2DojoJQuery
前一段时间有反映说是一个使用了struts2的生产系统的页面显示速度太慢。登录后发现确实如此,于是进行了一番性能调优的研究和测试。
一,根据struts2官方的性能调优说明进行了一些参数的修改。
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不现实,我只是关闭了struts2相关package的logging

Do not use interceptors you do not need.
把struts.xml中不需要的interceptor统统删除

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.
照做

进行上述修改后,发现页面打开的速度并没有明显的提高.

二,此时我已经基本锁定网页打开速度慢的原因与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"
进行上述修改后,发现页面打开的速度还是没有明显的提高.

三,经过上述两个实验,觉得应该是struts2所封闭的dojo的性能问题了。于是引入JQuery.
JQuery的js文件最小是55K, gzip后应该更小,页面的响应速度明显改善(一个数量级以上的提高),主要原因在于与服务器交互的处理上极大地提升了效率。而且页面处理代码更加简洁明了。

最后,我删除了所有的<s:head theme="ajax"/>和 <s:head/>(如果页面中加入<s:head />,那么在Struts2生成的html中后包含dojo.js),使用JQuery来完成所有的Ajax和javascript功能。




评论

# re: 我的struts2项目性能调优三步曲  回复  更多评论   

2008-07-01 14:38 by Greco
我在使用struts ajax 的时候也发觉慢的问题
然后用Firebug查看发现加载的js太多,而且有些不是必须的
所以按照yahoo ui 的15条军规来看
这绝对是影响页面速度的关键

# re: 我的struts2项目性能调优三步曲  回复  更多评论   

2008-07-01 23:42 by YYX
struts2 的ajax主题很失败,我和LZ一样也是开始用了,后来发现不但影响速度而且效果也不行,转而用jquery自己完成ajax功能

# re: 我的struts2项目性能调优三步曲  回复  更多评论   

2008-07-02 07:33 by 云自无心水自闲
其实struts2另外一个性能的瓶颈在于ognl,但是我现在还没有什么好方法来解决

# re: 我的struts2项目性能调优三步曲  回复  更多评论   

2008-07-02 07:49 by 云自无心水自闲
ognl已经升级到2.7,据称性能有显著提高,但是与MVEL相比,还是有一个数量级的差别。 :(

# re: 我的struts2项目性能调优三步曲  回复  更多评论   

2008-07-02 07:52 by 云自无心水自闲
这是TSS上的OGNL与MVEL的性能对比的文章

# re: 我的struts2项目性能调优三步曲  回复  更多评论   

2008-07-02 07:52 by 云自无心水自闲
http://www.theserverside.com/news/thread.tss?thread_id=46083

# re: 我的struts2项目性能调优三步曲  回复  更多评论   

2009-10-27 14:23 by fengye_nothing
aaaaaaaaaaaaa

只有注册用户登录后才能发表评论。


网站导航: