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

audit, jakarta-ee, java, monitoring, performance

Solution

Using AOP

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

For instance, see:

- this SO question on Catching Private or Inner Methods with AspectJ,

- or this article on Profiling with AspectJ.

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.

Problem

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?

Original source

Related problems