Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Timing interceptor for CDI/Weld

DZone's Guide to

Timing interceptor for CDI/Weld

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Lately we did some performance tuning, and in order to find out which methods are the hotspots I wrote a simple timing interceptor (it uses some code from a Seam forum post by Tobias Hill). Apart from timing the execution time of single methods, it gathers all results from a request and prints out a short sorted summary at the end containing the number of invocations, average time and so on.

You can find the source code and some documentation on github (softwaremill-debug module of the softwaremill-common project, licensed under Apache2), and the artifacts in our maven repository.

There are in fact four parts:

Interceptor

The interceptor is a regular CDI interceptor which times the execution of a method and stores the result in a thread-local storage. It must be enabled in beans.xml and can be applied via an annotation (@Timed).

Servlet filter

The servlet filter prints out the summary after a request completes. You just need to add it in your web.xml. It is also possible to print out the current stats at any point by invoking TimingResults.dumpCurrent().

Extension

The portable extension can save you some time if you want to intercept a lot of methods. Instead of adding the annotation manually, it automatically adds the interceptor to all beans for which the full class name (including the package) includes one of the elements of the timed.autoadd system property.

The extension is also a good example on how to automatically add interceptors in CDI (using Seam Solder); in essence it boils down to the following:

 

public <T> void processAnnotatedType(
@Observes ProcessAnnotatedType<T> event) {
if (interceptAnnotatedType(event.getAnnotatedType())) {
log.info("Adding the timing interceptor for " +
event.getAnnotatedType().getJavaClass().getName());
AnnotatedTypeBuilder<T> builder = new AnnotatedTypeBuilder<T>()
.readFromType(event.getAnnotatedType());
builder.addToClass(new TimedImpl());
event.setAnnotatedType(builder.create());
}
}

Proxy

For timing methods on objects which aren’t managed by the CDI container, there’s a timing proxy. Simple wrap your object with TimingProxy.createFor and any invocations will be timed and added to the current results (of the current request).

Hope you’ll find it useful!

 

From http://www.warski.org/blog/?p=372

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}