Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stepping MVC Mini Profiler without nested usings

We're modelling a complicated system based around a complicated entity relationship in Dynamics CRM 4.0

Due to the nature of development we've had to implement a repository style pattern and have lots of different providers that relate to each other.

What I really want to do is to profile their constructors and various lazy getters, but I want to model this at the top level possible.

The problem is, of course, Scope - If I wrap the constructor in a using block, it's not available to anything else. If I extend the using block so that everything that references the object I'm profiling comes into scope, then the profiler isn't just profiling the constructor - it's timing everything else.

Similarly, there's a level of nesting, If I nest the usings correctly, then the code becomes unreadable.

I've had a look at Profiler.Inline but that doesn't serve my purposes.

What I'd really like to do is this:

ref = Profiler.StartStep("Creating CRM Model");
//Do horrible CRM work
var myNewHorribleObject = CRM.ModelHorribleStuff(...);
Profiler.StopStep(ref);

ref = Profiler.StartStep("How long does it take to get X");
var data = Repository.GetSomething(myNewHorribleObject.SomeId);
Profiler.StopStep(ref);

ref = Profiler.StartStep("How long does it take to get Y");
var newData = Repository.GetSomethingElse(myNewHorribleObject.ContextId);
Profiler.StopStep(ref);

Does that make sense ? Hopefully I'm overlooking something in Mini Profiler, but I'd welcome any suggestions!

I would like to remodel the code a bit, but there is no time for that and whilst it looks odd, we actually have a pretty good cyclomatic complexity.

like image 586
Russ Clarke Avatar asked Feb 29 '12 11:02

Russ Clarke


1 Answers

Yes this is possible. Instead of using the using just use "MiniProfiler.Current.Step("blah")". This will return an object that implements IDisposeable. When that object is actually disposed of is when the timings will stop. You can even nest things with using statements just like normal.

Example:

 IDisposable executingStep;
 executingStep= MiniProfiler.Current.Step("Some code after this");
 // do long code
 Thread.Sleep(100);
 using (profiler.Step("Step 2313"))
 {
     Thread.Sleep(100);
 }
 executingStep.Dispose();
like image 99
Paul Lemke Avatar answered Nov 03 '22 02:11

Paul Lemke