As long as your MiniProfiler
instance is null (i.e. you never call MiniProfiler.Start()
), the Step()
extension method will return null. The only overhead at this point is the using
statement, which is negligible. Think of it as an extra if (false)
statement that has to execute.
I would advise against the syntax you're using where you store the IDispoable
outside of a using
block, because you don't get automatic null checking on the .Dispose()
call, e.g.
_thisStep = MiniProfiler.Current.Step("Check query string");
// if you do it this way, you will have to manually check for null every time
if (_thisStep != null) {
_thisStep.Dispose();
}
The way I usually work is to only profile once per method - if I need another step, I have to extract the code into another method, e.g.
public EventsIndexViewModel GetViewModel()
{
using (MiniProfiler.Current.Step("Generate events index view model"))
{
var pageIndex = GetPageIndex();
}
}
private int GetPageIndex()
{
using (MiniProfiler.Current.Step("GetPageIndex"))
{
// Do check... return result...
}
}
This has the added benefit of keeping my methods small :)
If you're on .NET 4.5, you can take advantage of the CallerFilePathAttribute
and use this .StepHere()
helper method I put into our Stack Overflow code, which avoids having to name every Step()
call!