Apache Struts 2 Documentation > Home > Guides > Core Developers Guide > Profiling
Added by tm_jee, last edited by Dave Newton on Feb 16, 2008  (view change) show comment

Profiling software looks for bottlenecks in program execution. In addition to the profiling services provided by IDEs and standalone profilers, the framework provides its own internal support for profiling.

Profiling aspects

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

Activating / Deactivating Profiling

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

Through System property

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

Through 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.

Through 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.

Profiling activation through parameter will need the followings:-
<ul>
 <li>Profiling interceptor</li>
 <li>dev mode on (struts.devMode=true in struts.properties)
</ul>

Filtering profile information

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

Write profiling code

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

Using UtilTimerStack's push and pop

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

Using a UtilTimerStack's ProfileBlock template

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

Profiling Log files

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.

Next: Debugging