Measure performance for overriden methods
eshref opened this issue · 5 comments
Hi. I have following scenario.
class Parent
{
[Time]
virtual SomeMethod()
}
In some cases I need to override SomeMethod as below and measure performance.
class Child:Parent
{
[Time]
override SomeMethod()
}
How can I only keep only overriden method's performance data.
Can you show us the output that is currently being produced by MethodTimer? And is the overridable method still calling the base or not?
Hello @GeertvanHorrik , thank you for quick response. Below is my code:
public class ParentService
{
[Time]
public virtual void SomeMethod()
{
Thread.Sleep(100);
}
}
public class ChildService:ParentService
{
[Time]
public override void SomeMethod()
{
Thread.Sleep(200);
base.SomeMethod();
}
}
Below code i added to web api controller:
[HttpGet]
public ActionResult GetPerformance()
{
ChildService service = new ChildService();
service.SomeMethod();
return this.Ok();
}
This is output:
ParentService.SomeMethod 107ms
ChildService.SomeMethod 325ms
While researching this issue i just noticed that 7 ms added by Fody to ParentService and 25 ms to ChildService method. Is this normal?
I don't think there is a way around this since you are still explicitly calling the base class implementation (and thus running it's injected stopwatch handler).
There are 2 things you could do here:
- Not call base (is this intended, is this required, etc)?
- In your method logger, create exceptions where overrides ignore the log of the base call
I do think what you are seeing is correct behavior since you are running both methods and they both take time.
- It is required.
public static void Log(MethodBase methodBase, TimeSpan elapsed)
you mean we can somehow check inside this method that methodBase was overriden and base call performed?
About 2: yes, you can create your own implementation and keep track of the calls that were recently made (e.g. a number of last 10 items). But if I am using MethodTimer, I am interested in both the overridden and base implementation behavior (timing).
If you are only interested in the "overall" time of the method, you could create a non-virtual method that you time, and don't time the virtual and overridden method. In this case you will always get the "total time", no matter whether it was overridden or not.