Question

I'm looking to publish custom performance counters for certain important function calls in the system. I'd like to continously monitor these performance counters in a production environment.

Is there a way for me to mark certain functions with a custom attribute that could measure the time it took to execute given function? I'd like to avoid injecting custom code and thus polluting business-related functions with monitoring code.

How can a code from an attribute track the time it took for function to execute?

Please do not suggest use of a Profiler. I'm not looking to debug or benchmark performance. But simply wanting to track it in on a production 24/7 basis.

Was it helpful?

Solution

Well, you need some code to execute your function and analyze your attribute so it will know that it needs to execute something (e.g. execution time measurement, in your case) before and after your function - it could be using the Decorator design pattern, Reflection, Proxy object and so on.

As you tagged, it is an aspect-programming issue and you can take advantage of PostSharp third-party, for example, in order to achieve it.

Take a look at this specific page about Performance Counters using PostSharp, it is indeed uses Attributes: http://www.postsharp.net/aspects/examples/performance

To elaborate a little on @pasty's comment, you can intercept your methods invocations with Interception using Unity DI Container, it is indeed requires more code to write, here is a simple sample flow chart of the Unity interception feature:

shows the objects that are involved with interception behaviors in this example

Note to step 3, where a proxy object is actually returned to the client and thanks to that, the interception behavior is achieved.

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