views:

78

answers:

4

What is the most efficient way to collect and report performance statistic analysis from an application?
If I have an application that uses a series of network apis, and I want to report statistics at runtime, e.g.

Method doA() was called 3 times and consumed on avg 500ms  
Method doB() was called 5 times and consumed on avg 1200ms etc  

Then, I thought of using a well defined data structure (of collection) that each thread updates per remote call, and this can be used for the report.

But I think that it will make the performance worse, for the time spend for statistics collection. Am I correct? How would I procceed if I used a background thread for this, and the other threads that did the remote calls were unaware of this collection gathering?

Thanks

A: 

There are different ways to go about this:

  1. Dynamic alteration (Easy to Code - Very likely to Alter performance): Here you use code weaving (AspectJ or similar library) or dynamic proxying to do the counting. This has the pro of being very fast to setup and could give you the general idea. However, it carries the most overhead to your performance and will alter your results if your application is very finely tuned.

  2. Static alteration (Needs more code - lower performance impact): Here you create some ThreadLocals or a ConcurrentHashMap based on how do you want to track your stats (Per Thread or Across the application), impact here is a map lookup with a string key, no locking guaranteed and an integer increment. Which shouldn't mess up your data and is pretty constant and can be measured alone and factored out from your stats.

Hope this helps.

MahdeTo
A: 

I believe the easiest way to do what you want is to add slf4j profiling to your application and modify the reporting methods. This is not a transparent solution, but is quite simple.

http://www.slf4j.org/extensions.html#profiler

Thorbjørn Ravn Andersen
+1  A: 

See this: "Use aspectj to profile selected methods" http://stackoverflow.com/questions/538010/use-aspectj-to-profile-selected-methods

Seven Seconds Mengesha
A: 

aspectj or ThreadLocal coding seems a bit of overengineering to me.

I think that JavaSimon or Jamon with some ad-hoc starts/stops in your app is a very good fit for you, see: http://code.google.com/p/javasimon/wiki/GettingStarted#Hello_world

http://jamonapi.sourceforge.net/#SimpleExamples

evernat