Performance: using AspectJ to log run time of all

2019-08-05 19:09发布

问题:

I have a requirement to log the run time of every single method in the application to the DB using logback and AspectJ. It's an enterprise application with roughly a few thousand classes and on average about 10 methods inside each class.

Am I walking into major performance issues here ? I know it's over the top to log every single method, but let's look past that for now.

回答1:

Thousands of classes with about ten methods per class and you have to implement trace logging for all of them?

Short answer: Yes.

Long answer: Hell yes.
Your performance will be (more or less) heavily affected. To log run time of methods you'll need (minimally) an around advice that takes the time at method entry and exit, constructs a string and sends that to the logging framework. Depending on what your tens of thousands methods do, for a lot of them that may be even more time consuming than the actual execution of the method - the smaller the method the bigger the overhead.

I can't speak for your application, but upon doing this for one of mine the actual net-performance with activated tracing just about halved. Whether that will hold true for your application as well or qualifies as your 'major performance issues', only you can tell.

Thus my advice would be to simply try it. Weaving a trace advice around every method within a namespace can be done easily enough. Then it's just a matter of comparing performances between the traced and non-traced applications.