Question

We use interceptors to measure the execution time of a bean's public method invocation. nonetheless, when a bean's method invoke other private methods, it seems to ignore the audit interceptor.

How can we measure the execution time of private methods as well?

Was it helpful?

Solution

Using AOP

You could implement a benchmarker using an Aspect-Oriented Programming library like AspectJ.

For instance, see:

Using a Profiler

  • You could implement your own agent extension (for instance, for JProfiler).
  • Or you could give up on your interceptors and simply inspect from any profiler allow to capture snapshots and to record execution times.

Using JVMTI

Which is what some profilers do, actually.

You could resort to using the JVMTI API (not entirely sure this would fly, to be honest) to implement your own code inspector and directly hook yourself into the JVM.


The Sneaky and Evil Inlining Issue

Regarding jb's (valid) concern in his answer that private methods might be inlined at either compilation time or runtime, some JVMs may not do it or allow to disable this feature.

  • Oracle's JRockit has a -XnoOpt option that would disable optimizations (including this particular one).
  • Oracle/Sun's HotSpot at least used to have -XX:-Inline (not sure it still exists or does anything).

However, it means you don't measure exactly what you'd have in production when the inlining is activated. Still, probably handy for inspecting your code.

OTHER TIPS

Interceptors are applied by EJB container on invocation of interface methods - your private methods are invisible to it. What about using profiling tool instead?

Well AFAIK private methods can be inlined at JVM lesiure (even at compilation time), so they can't be profiled since they might not exist in bytecode.

I suppose you could mark your methods protected --- so they will not be inlined even in production, and then profile it.

If you want to profile your test instance you might try visualvm --- visualvm is a very nice alternative. VisualVM is a graphical tool to profile JVM istances that does all the instrumentation needed. http://visualvm.java.net/. Moreover it is a standard tool in most jdk distros.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top