You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 27 Next »

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

Error formatting macro: snippet: java.lang.NullPointerException

Using

To enable profiling, first make sure that the profiling interceptor is applied to your action, like:

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

Then enable profiling using one of the following methods:

Activate Through System property

Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException

Activate Through code

Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException

Activate Through parameter

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

Changing the activation parameter name

Set the profilingKey attribute of the profiling interceptor to the desired name:

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

Profiling activation through a parameter requires struts.devMode to be true.

Filtering profile information

One could filter out the profile logging by having a System property as follows:

-Dxwork.profile.mintime=10000 

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.

Write profiling code

One could extend 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

  • No labels