7

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.

ccellar
  • 10,156
  • 2
  • 35
  • 56
Russ Clarke
  • 16,600
  • 3
  • 36
  • 43

1 Answers1

10

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();
Paul Lemke
  • 5,255
  • 3
  • 45
  • 66
  • 1
    Note that if MiniProfiler is not started then executingStep will be null, so you will need "if (executingStep != null) executingStep.Dispose()" for this case. – eddiegroves Jan 30 '15 at 05:18