Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## com.opensymphony.xwork2.util.profiling
Class UtilTimerStack

    java.lang.Object
      com.opensymphony.xwork2.util.profiling.UtilTimerStack

    public class UtilTimerStack

extends Object

A timer stack.

Struts2 profiling aspects involves the following :-

- ActionContextCleanUp
- FreemarkerPageFilter
- DispatcherFilter

- Dispatcher

- creation of DefaultActionProxy

- creation of DefaultActionInvocation

- creation of Action

- execution of DefaultActionProxy

- invocation of DefaultActionInvocation

- invocation of Interceptors
- invocation of Action
- invocation of PreResultListener
- invocation of Result

XWork2 profiling aspects involves the following :-

- creation of DefaultActionProxy

- creation of DefaultActionInvocation

- creation of Action

- execution of DefaultActionProxy

- invocation of DefaultActionInvocation

- invocation of Interceptors
- invocation of Action
- invocation of PreResultListener
- invocation of Result

Activating / Deactivating of the profiling feature could be done through:-

System properties:-

      -Dxwork.profile.activate=true

This could be done in the container startup script eg. CATALINA_OPTS in catalina.sh (tomcat) or using "java -Dxwork.profile.activate=true -jar start.jar" (jetty)

Code :-

      UtilTimerStack.setActivate(true);

      // or

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

      // or

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

This could be done in a static block, in a Spring bean with lazy-init="false", in a Servlet with init-on-startup as some numeric value, in a Filter or Listener's init method etc.

Parameter:-

     <action ... >
      ...
      <interceptor-ref name="profiling">
          <param name="profilingKey">profiling</param>
      </interceptor-ref>
      ...
     </action>

     or

     <action .... >
     ...
      <interceptor-ref name="profiling" />
     ...
     </action>

     through url

     http://host:port/context/namespace/someAction.action?profiling=true

     through code

     ActionContext.getContext().getParameters().put("profiling", "true);

To use profiling activation through parameter, one will need to pass in through the 'profiling' parameter (which is the default) and could be changed through the param tag in the interceptor-ref.

Warning:

Profiling activation through parameter will need the followings:-

- Profiling interceptor
- dev mode on (struts.devMode=true in struts.properties)

One could filter out the profile logging by having a System property as follows. With this 'xwork.profile.mintime' property, one could only log profile information when its execution time exceed those specified in 'xwork.profile.mintime' system property. If no such property is specified, it will be assumed to be 0, hence all profile information will be logged.

      -Dxwork.profile.mintime=10000

One could extends the profiling feature provided by Struts2 in their web application as well.

        String logMessage = "Log message";
        UtilTimerStack.push(logMessage);
        try {
          // do some code
        }
        finally {
          UtilTimerStack.pop(logMessage); //this needs to be the same text as above
        }

or

       String result = UtilTimerStack.profile("purchaseItem: ",
           new UtilTimerStack.ProfilingBlock() {
                public String doProfiling() {
                   // do some code
                   return "Ok";
                }
           });

Profiled result is logged using commons-logging under the logger named 'com.opensymphony.xwork2.util.profiling.UtilTimerStack'. Depending on the underlying logging implementation say if it is Log4j, one could direct the log to appear in a different file, being emailed to someone or have it stored in the db.

Version:
  $Date$ $Id$

Nested Class Summary
`static interface` | `UtilTimerStack.ProfilingBlock<T>` A callback interface where code subjected to profile is to be executed.

Field Summary
`static String` | `ACTIVATE_PROPERTY` System property that controls whether this timer should be used or not.
`protected static ThreadLocal<ProfilingTimerBean>` | `current`
`private static Log` | `log`
`static String` | `MIN_TIME` System property that controls the min time, that if exceeded will cause a log (at INFO level) to be created.

Constructor Summary
`UtilTimerStack()`

Method Summary
`private static long` | `getMinTime()` Get the min time for this profiling, it searches for a System property 'xwork.profile.mintime' and default to 0.
`static boolean` | `isActive()` Determine if profiling is being activated, by searching for a system property 'xwork.profile.activate', default to false (profiling is off).
`static void` | `pop(String name)` End a preformance profiling with the `name` given.
`private static void` | `printTimes(ProfilingTimerBean currentTimer)` Do a log (at INFO level) of the time taken for this particular profiling.
`static
`<T> T`
` | `profile(String name, UtilTimerStack.ProfilingBlock<T> block)` A convenience method that allows `block` of code subjected to profiling to be executed and avoid the need of coding boiler code that does pushing (UtilTimeBean.push(...)) and poping (UtilTimerBean.pop(...)) in a try ...
`static void` | `push(String name)` Create and start a performance profiling with the `name` given.
`static void` | `setActive(boolean active)` Turn profiling on or off.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### current

    protected static ThreadLocal<ProfilingTimerBean> current

### ACTIVATE_PROPERTY

    public static final String ACTIVATE_PROPERTY

  System property that controls whether this timer should be used or not. Set to "true" activates the timer. Set to "false" to disactivate.

  See Also:
    Constant Field Values

### MIN_TIME

    public static final String MIN_TIME

  System property that controls the min time, that if exceeded will cause a log (at INFO level) to be created.

  See Also:
    Constant Field Values

### log

    private static final Log log

Constructor Detail

### UtilTimerStack

    public UtilTimerStack()

Method Detail

### push

    public static void push(String name)

  Create and start a performance profiling with the `name` given. Deal with profile hierarchy automatically, so caller don't have to be concern about it.

  Parameters:
    `name` - profile name

### pop

    public static void pop(String name)

  End a preformance profiling with the `name` given. Deal with profile hierarchy automatically, so caller don't have to be concern about it.

  Parameters:
    `name` - profile name

### printTimes

    private static void printTimes(ProfilingTimerBean currentTimer)

  Do a log (at INFO level) of the time taken for this particular profiling.

  Parameters:
    `currentTimer` - profiling timer bean

### getMinTime

    private static long getMinTime()

  Get the min time for this profiling, it searches for a System property 'xwork.profile.mintime' and default to 0.

  Returns:
    long

### isActive

    public static boolean isActive()

  Determine if profiling is being activated, by searching for a system property 'xwork.profile.activate', default to false (profiling is off).

  Returns:
    `true`, if active, `false` otherwise.

### setActive

    public static void setActive(boolean active)

  Turn profiling on or off.

  Parameters:
    `active` -

### profile

    public static <T> T profile(String name,
                                UtilTimerStack.ProfilingBlock<T> block)
                     throws Exception

  A convenience method that allows `block` of code subjected to profiling to be executed and avoid the need of coding boiler code that does pushing (UtilTimeBean.push(...)) and poping (UtilTimerBean.pop(...)) in a try ... finally ... block.

  Example of usage:

               // we need a returning result
         String result = UtilTimerStack.profile("purchaseItem: ",
             new UtilTimerStack.ProfilingBlock() {
                  public String doProfiling() {
                     getMyService().purchaseItem(....)
                     return "Ok";
                  }
             });

  or

         // we don't need a returning result
         UtilTimerStack.profile("purchaseItem: ",
             new UtilTimerStack.ProfilingBlock() {
                  public String doProfiling() {
                     getMyService().purchaseItem(....)
                     return null;
                  }
             });

  Type Parameters:
    `T` - any return value if there's one.
  Parameters:
    `name` - profile name
    `block` - code block subjected to profiling
  Returns:
    T
  Throws:
    `Exception`

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2000-2007 Apache Software Foundation. All Rights Reserved.