阅读:3762次   评论:2条   更新时间:2011-10-30    
来自 http://struts.apache.org/2.0.11/docs/performance-tuning.html ,工作中也用到了,很实用。


Performance tuning
The following are some tips and tricks to squeeze the most performance out of Struts 2.

Turn off logging and devMode.
DevMode allows reloading of configuration and validation related files, but because they happen on each request, this setting will totally kill your performance.
When using logging, make sure to turn off logging (esp. Freemarker generates a LOT of logging), and check if a level is enabled before printing it, or you will get the cost of the String parsing/concatination anyways.

Do not use interceptors you do not need.
If you do not require a full stack of interceptors for an Action, then try using a different one (basicStack), or remove interceptors you do not need.

Use the correct HTTP headers (Cache-Control & Expires).
When returning HTML views, make sure to add the correct headers so browsers know how to cache them.

Copy the static content from the Struts 2 jar when using the Ajax theme (Dojo) or the Calendar tag.
Struts 2 uses some external javascript libraries and cascading stylesheets for certain themes and tags. These by default are located inside the Struts 2 jar, and a special filter returns them when requesting a special path (/struts). Although Struts 2 can handle these requests, an application/servlet container is not optimized for these kind of requests. Consider moving these .js and .css files to a seperated server (Lighttpd, Apache HTTPD, ..).

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

This value determines how often Freemarker checks if it needs to reloads the templates from disk. The default value is 500 ms. Since there is no reason to check if a template needs reloading, it is best to set this to a very large value. Note that this value is in seconds and freemarker will convert this value to milliseconds.

See also: Freemarker configuration properties

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.

In Struts versions prior to 2.0.10, you had to copy the /template directory from the Struts 2 jar in your WEB_APP root to utilize Freemarker's built in chaching mechanism in order to achieve similar results.

The built in Freemarker caching mechanism fails to properly cache templates when they are retrieved from the classpath. Copying them to the WEB_APP root allows Freemarker to cache them correctly. Freemarker looks at the last modified time of the template to determine if it needs to reload the templates. Resources retrieved from the classpath have no last modified time, so Freemarker will reload them on every request.

When overriding a theme, copy all necessary templates to the theme directory.
There's a performance cost when a template cannot be found in the current directory. The reason for this is that Struts 2 must check for a template in the current theme first before falling back to the parent theme. In the future, this penalty could be eliminated by implementing a missing template cache in Struts 2.

Do not create sessions unless you need them.
Struts 2 does not create sessions unless asked to (for example, by having the createSession interceptor in your interceptor stack). Note that when you use SiteMesh however, a session will always be created (See http://forums.opensymphony.com/thread.jspa?messageID=5688 for details).

When using Freemarker, try to use the Freemarker equivalent rather than using the JSP tags.
Freemarker has support for iterating lists, displaying properties, including other templates, macro's, and so on. There is a small performance cost when using the S2 tags instead of the Freemarker equivalent (eg. <s:property value="foo"/> should be replaced by ${foo}).

评论 共 2 条 请登录后发表评论
2 楼 yizhl 2012-04-25 22:08
性能调整
以下是一些提示和技巧最性能挤压保持2。

测井和devMode关掉。
DevMode允许加载配置和验证相关档案,而是因为他们发生在每一个请求,该背景将完全杀死你的表现。
当使用测井中,一定要关掉测井(尤其是Freemarker产生很大的测井),并检查标准是使印刷前它,不然你会得到成本/ concatination解析字符串的推翻。

不要使用拦截你并不需要的东西。
如果你不需要一个完整的堆导弹拦截系统为一个动作,并且试着使用一个不同的人(basicStack),或删除拦截你并不需要的东西。

使用一个正确的HTTP头([和期满)。
当返回HTML视图,确保正确的头文件浏览器添加所以知道如何缓存。

复制的静态内容从2罐在使用支柱阿贾克斯的主题(道场)或日历标签。
第二支柱使用一些外部javascript和层叠式样式表为图书馆某些特定的主题和标签。这些默认情况下是坐落在支撑2罐和一个特殊的过滤器返回他们在寻求一个特殊的路径(/支柱)。虽然两个可以处理这些请求支柱,一个应用程序/ servlet集装箱没有优化,这样的要求。考虑到这些。js和。css文件到一个分开的服务器(Lighttpd,恶意HTTPD,…)。

创建一个freemarker。 在你的WEB-INF属性/等级目录。
创造freemarker。 属性也可以在文件中增加以下设置(或者任何你认为恰当的价值):

template_update_delay = 60000

这个值决定了经常检查是否需要Freemarker重新载入模板从磁盘。默认值是500毫秒。由于没有理由检查一个模板是否需要刷新,最好是把这个设置一个非常大的价值。注意,这种价值是在几秒钟或freemarker将转换成这个值以毫秒。

还看到:configu Freemarker
1 楼 feijing 2012-03-02 09:56
<s:property value="foo"/> should be replaced by ${foo}). 看了这么多,就知道这是啥意思.foo

发表评论

您还没有登录,请您登录后再发表评论

文章信息

Global site tag (gtag.js) - Google Analytics