关于UtilTimerStack类的使用--XWork2、Struts2内置性能诊断类

一、UtilTimerStack做什么用的?

这个本来是Xwork2(Struts2的核心)的相关的工具类,可以用来测试一些逻辑操作所消耗的时间(以毫秒为单位),其本身使用一个
ArrayList作为存放ProfilingTimerBean的容器,而行为以Stack栈(后入先出)为主。在打印的时候支持缩进显示,显示相关的
调用关系。类ProfilingTimerBean主要是用来记录一些相关的信息,例如主要标识、开始时间、结束时间。

二、UtilTimerStack如何使用?

UtilTimerStack的调用方法不多且都为静态函数,但在调用之前必须先开启诊断功能,

主要有以下三种方法开启:

  • 在启动时加入参数: -Dxwork.profile.activate=true
  • 在使用前、或者静态代码区、或者Servlet的初始化、或者Filter的初始化 加入代码:

UtilTimerStack.setActivate(true);

或者 System.setProperty("xwork.profile.activate", "true");

或者 System.setProperty(UtilTimerStack.ACTIVATE_PROPERTY, "true");

  • 也支持xml来定义相关拦截器(必须开启dev模式和加入Profiling拦截器):

<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);

指定超时打印功能,只有当运行时间超过时才会进行记录,开启方法:

  • 在启动时加入参数: -Dxwork.profile.mintime=1000
  • 或者代码加入:System.setProperty(UtilTimerStack.MIN_TIME, "" + 1000);

例子代码:

  1. public static void main(String[] args) throws InterruptedException {
  2. String name = "test used time:";
  3. UtilTimerStack.setActive(true);  //开启诊断功能
  4. System.setProperty(UtilTimerStack.MIN_TIME, "" + 1000); //设置最小的超时记录时间
  5. UtilTimerStack.push(name); //顶节点的记录Bean
  6. subMethod(0);
  7. UtilTimerStack.pop(name);//对应的出栈操作,名称与入栈时相同
  8. }
  9. public static void subMethod(int i) throws InterruptedException{
  10. System.out.println(i + "");
  11. UtilTimerStack.push(i + " times");
  12. if(i<3) subMethod(i+1); //递归调用,会产生缩进,最后调用i=3时不足1000,不记录
  13. Thread.sleep(i * 300);
  14. UtilTimerStack.pop(i + " times");
  15. }

代码输出:

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)动作:

  1. public static void push(String name)
  2. {
  3. if (!isActive())  //判断是否开启诊断功能
  4. return;
  5. //创建新的记录Bean,并用setStartTime方法开始记录
  6. ProfilingTimerBean newTimer = new ProfilingTimerBean(name);
  7. newTimer.setStartTime();
  8. //判断如果有父节点,则添加到父节点的Child中.
  9. ProfilingTimerBean currentTimer = (ProfilingTimerBean) current.get();
  10. if (currentTimer != null)
  11. {
  12. currentTimer.addChild(newTimer);
  13. }
  14. //再把新建的节点设置为当前的线程的局部变量current中.
  15. current.set(newTimer);
  16. }

UtilTimerStack的出栈(pop)动作:

  1. public static void pop(String name)
  2. {
  3. if (!isActive())
  4. return;
  5. //获取当前线程的变量current.get()返回顶部记录Bean
  6. ProfilingTimerBean currentTimer = (ProfilingTimerBean) current.get();
  7. //if the timers are matched up with each other (ie push("a"); pop("a"));
  8. //测试出栈的记录Bean是否与要求的对应,如果不对应则log下错误,并打印栈里的记录Bean信息
  9. //如果与要求的标识对应,则把父节点设置为当前节点.
  10. if (currentTimer != null && name != null && name.equals(currentTimer.getResource()))
  11. {
  12. currentTimer.setEndTime();
  13. ProfilingTimerBean parent = currentTimer.getParent();
  14. //如果当前节点为根节点,则开始打印这个栈里面的记录Bean信息
  15. if (parent == null)
  16. {
  17. printTimes(currentTimer);
  18. //清理当前的线程局部变量,防止一些容器使用线程池的方式处理请求.
  19. current.set(null);
  20. }
  21. else
  22. {
  23. //设置父节点为当前节点.
  24. current.set(parent);
  25. }
  26. }
  27. else
  28. {
  29. //当出栈的顺序不对称时,把剩下的都打印出来
  30. if (currentTimer != null)
  31. {
  32. printTimes(currentTimer);
  33. current.set(null);
  34. LOG.warn("Unmatched Timer.  Was expecting " + currentTimer.getResource() + ", instead got " + name);
  35. }
  36. }
  37. }

关于UtilTimerStack类的使用--XWork2、Struts2内置性能诊断类的更多相关文章

  1. struts2内置拦截器和自定义拦截器详解(附源码)

    一.Struts2内置拦截器 Struts2中内置类许多的拦截器,它们提供了许多Struts2的核心功能和可选的高级特 性.这些内置的拦截器在struts-default.xml中配置.只有配置了拦截 ...

  2. 扩展PHP内置的异常处理类

    在try代码块中,需要使用throw语句抛出一个异常对象,才能跳到转到catch代码块中执行,并在catch代码块中捕获并使用这个异常类的对象.虽然在PHP中提供的内置异常处理类Exception,已 ...

  3. day28 面向对象:反射,内置函数,类的内置方法

    面向对象进阶博客地址链接: http://www.cnblogs.com/Eva-J/articles/7351812.html 复习昨日内容: # 包 # 开发规范 # # hashlib # 登录 ...

  4. 2018.11.20 Struts2中对结果处理方式分析&struts2内置的方式底层源码剖析

    介绍一下struts2内置帮我们封装好的处理结果方式也就是底层源码分析 这是我们的jar包里面找的位置目录 打开往下拉看到result-type节点 name那一列就是我们的type类型取值 上一篇博 ...

  5. Struts2内置校验器——完整实例代码

    一.校验器的配置风格 1.字段校验器: <field name="被校验的字段"> <field-validator type="校验器名"& ...

  6. 洗礼灵魂,修炼python(42)--巩固篇—type内置函数与类的千丝万缕关系

    type函数的隐藏属性 相信大家都知道内置函数type是用来查看对象的数据类型的.例: 那比如我对int类查看类型呢? 有朋友会说,int是内置类啊,用自定义的应该不会这样,我们自定义一个类呢? 还是 ...

  7. python基础之反射内置方法元类

    补充内置函数 isinstance(obj,Foo)   # 判断obj是不是foo的实例 issubclass()      # 判断一个类是不是另一个类的子类 反射 什么是反射? 通过字符串来操作 ...

  8. python类详细说明、常用内置方法和self的作用

    一.类的定义 在Python中,一切皆对象,即便是类本身,也是一种type类型的特殊对象. class Person: def __init__(self, name, age): self.name ...

  9. python类与对象-如何派生内置不可变类型并修其改实例化行为

    如何派生内置不可变类型并修其改实例化行为 问题举例 自定义一种新类型的元组,对传入的可迭代对象,我们只保留 其中int类型且值大于0的元素,例如 IntTuple([1, -1, 'abc', 6, ...

随机推荐

  1. csdn仍是&quot;待定&quot;对?

    正如标题,我的博客会审查,?我们见证.如此反复.考虑到该博客平台的变化.              看来,这次最终逃脱被"待审核",看来再也不用受这个困扰了,希望以后CSDN可以在 ...

  2. Java线(一个):线程安全的和不安全

    当我们看JDK API什么时候,总是找一些类描述说:,线程安全或线程安全,例如StringBuilder在,么一句,"将StringBuilder 的实例用于多个线程是不安全的.假设须要这种 ...

  3. poj 1699 Best Sequence(AC自己主动机+如压力DP)

    id=1699" target="_blank" style="">题目链接:poj 1699 Best Sequence 题目大意:给定N个D ...

  4. HDU 3103 Shoring Up the Levees(计算几何 搜寻区域)

    主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3103 Problem Description The tiny country of Waterlog ...

  5. GCD下载图片

    cell.myimage.layer.masksToBounds=YES;     cell.myimage.layer.cornerRadius=cell.myimage.frame.size.wi ...

  6. Atitit..文件上传组件选择and最佳实践的总结(2)----HTTP

    Atitit..文件上传组件选型and最佳实践总结(2)----断点续传 1. 断点续传的原理 1 2. 怎样推断一个插件/控件是否支持断点续传?? 1 3. 经常使用的组件选型结果::马 1 4.  ...

  7. Flux是一个Facebook团队的前端开发架构

    Flux是一个Facebook团队的前端开发架构 Flux introduction 本文组成: React 官方文档翻译 相关实践心得. 内容上是Flux的介绍,例子将会在以后写出.一旦稍微多了解一 ...

  8. Tomcat通过JNDI方式链接MySql数据库

    原文:Tomcat通过JNDI方式链接MySql数据库 拷贝MySQL的JDBC驱动到Tomcat的lib路径下 配置全局数据源或者单个Web应用的局部数据源 局部数据源 在Tomcat的conf/C ...

  9. 设计模式——依赖倒置原则实例(PHP实现)

    <?php /** * 设计模式--依赖倒置原则实例 * Created by DannyWang * jue.wang@yulore.com * 2015-05-05 */ abstract ...

  10. 专业的GIS(电子地图、地理信息系统)在房地产行业的初步应用?

    时下随着智能手机在我国的迅猛发展以及网络时代的快速前进.手机APP以及web应用站点也顺势发展的如火如荼.我们国家还是一个人口大国,在吃穿不愁的今天,一个人口大国必需要面对的严峻问题就是住房问题.即使 ...