Child pages
  • Profiling

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

Struts2 supports build-in profilingProfiling 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

Wiki Markup
{snippet:id=profilingAspect_struts2|javadoc=true|url=com.opensymphony.xwork2.util.profiling.UtilTimerStack}

Activating / Deactivating Profiling

Wiki Markup
{snippet:id=activationDescription|javadoc=true|url=com.opensymphony.xwork2.util.profiling.UtilTimerStack}

Using

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

Code Block
xml
xml

<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

Wiki Markup
{snippet:id=activationThroughSystemProperty|lang=xml|javadoc=true|url=com.opensymphony.xwork2.util.profiling.UtilTimerStack}
Wiki Markup
{snippet:id=activationThroughSystemPropertyDescription|javadoc=true|url=com.opensymphony.xwork2.util.profiling.UtilTimerStack}

Activate Through code

Wiki Markup
{snippet:id=activationThroughCode|lang=xml|javadoc=true|url=com.opensymphony.xwork2.util.profiling.UtilTimerStack}
Wiki Markup
{snippet:id=activationThroughCodeDescription|javadoc=true|url=com.opensymphony.xwork2.util.profiling.UtilTimerStack}

Activate Through parameter

Wiki Markup
{snippet:id=activationThroughParameter|lang=xml|javadoc=true|url=com.opensymphony.xwork2.util.profiling.UtilTimerStack}
Wiki Markup
{snippet:id=activationThroughParameterDescription|javadoc=true|url=com.opensymphony.xwork2.util.profiling.UtilTimerStack}
Warning
Wiki Markup
{snippet:id=activationThroughParameterWarning|lang=xml|javadoc=true|url=com.opensymphony.xwork2.util.profiling.UtilTimerStack}

Filtering profile information

Wiki Markup
{snippet:id=filteringDescription|javadoc=true|url=com.opensymphony.xwork2.util.profiling.UtilTimerStack}
Wiki Markup
{snippet:id=filteringCode|lang=xml|javadoc=true|url=com.opensymphony.xwork2.util.profiling.UtilTimerStack}

Write profiling code

{snippet:id=methodDescription|javadoc=true|url=com.opensymphony.xwork2.util.profiling.UtilTimerStack}
Wiki Markup
Code Block

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:

Code Block
xml
xml

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

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:

Code Block

-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

{snippet:id=method1|lang=xml|javadoc=true|url=com.opensymphony.xwork2.util.profiling.UtilTimerStack}
Wiki Markup
Code Block
java
java

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

Wiki Markup
{snippet:id=method2|lang=xml|javadoc=true|url=com.opensymphony.xwork2.util.profiling.UtilTimerStack}

Profiling Log files

{snippet:id=profileLogFile|javadoc=true|url=
Wiki Markup
Code Block
java
java

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