关于UtilTimerStack类的使用--XWork2、Struts2内置性能诊断类
一、UtilTimerStack做什么用的?
这个本来是Xwork2(Struts2的核心)的相关的工具类,可以用来测试一些逻辑操作所消耗的时间(以毫秒为单位),其本身使用一个
ArrayList作为存放ProfilingTimerBean的容器,而行为以Stack栈(后入先出)为主。在打印的时候支持缩进显示,显示相关的
调用关系。类ProfilingTimerBean主要是用来记录一些相关的信息,例如主要标识、开始时间、结束时间。
二、UtilTimerStack如何使用?
UtilTimerStack的调用方法不多且都为静态函数,但在调用之前必须先开启诊断功能,
主要有以下三种方法开启:
UtilTimerStack.setActivate(true);
或者 System.setProperty("xwork.profile.activate", "true");
或者 System.setProperty(UtilTimerStack.ACTIVATE_PROPERTY, "true");
<action ... >
...
<interceptor-ref name="profiling">
<param name="profilingKey">profiling</param>
</interceptor-ref>
...
</action>
再通过URL访问: http://host:port/context/namespace/someAction.action?profiling=true
或者通过代码加入请求参数: ActionContext.getContext().getParameters().put("profiling", "true);
指定超时打印功能,只有当运行时间超过时才会进行记录,开启方法:
例子代码:
代码输出:
0
1
2
3
2010-3-21 16:16:52 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: [1828ms] - test used time:
[1828ms] - 0 times
[1828ms] - 1 times
[1516ms] - 2 times
三、UtilTimerStack内部机制?
ProfilingTimerBean类结构
主要成员:
List<ProfilingTimerBean> children;//记录子节点为,打印时需要
ProfilingTimerBean parent = null;//记录父节点,顶点的父节点为null
String resource;//主要标识,名称
long startTime;//开始时间
long totalTime;//结束时间
UtilTimerStack的压栈(push)动作:
UtilTimerStack的出栈(pop)动作:
关于UtilTimerStack类的使用--XWork2、Struts2内置性能诊断类
原文:http://www.cnblogs.com/quchengfeng/p/4930922.html