MVC Profiler - Manual Add Step

I have a lot of small actions (10-30 per page) and they are part of a recursive call, so I cannot easily count them as one step in the profiler. I get 30 short and great counters when I only need the total.

Is there a simple and easy way to manually add a step to the mini profiler? or force it to sum steps with the same name?

Also, is it possible to somehow place textual information there, and not just timings?

+3


source to share


2 answers


I need this too, so I did this little thing. It works really well. Many of the useful properties in MiniProfiler are internal or similar, so some code is duplicated from the MiniProfiler source. Also note that because it is intended for inner loops, it is not rounded down to 1/10 of a millisecond.



public class GroupTiming : IDisposable
{
    private Timing _timing;
    private MiniProfiler _profiler;
    private readonly Stopwatch _sw;

    public GroupTiming( MiniProfiler profiler, Timing timing )
    {
        _timing = timing;
        _profiler = profiler;
        _profiler.Head = _timing;
        _sw = new Stopwatch();
        _sw.Start();
    }

    public void Dispose()
    {
        _timing.DurationMilliseconds += (decimal)(1000*_sw.ElapsedTicks) / (decimal)Stopwatch.Frequency;
        _profiler.Head = _timing.ParentTiming;
    }
}

public static class MiniProfilerExtensions
{
    public static IDisposable StepGroup( this MiniProfiler profiler, string name )
    {
        return profiler == null ? null : profiler.StepGroupImpl( name );
    }

    private static IDisposable StepGroupImpl( this MiniProfiler profiler, string name )
    {
        Timing timing = null;

        if( profiler.Head.Children != null )
        {
            timing = profiler.Head.Children.SingleOrDefault( _ => _.Name == name );
        }

        if( timing == null )
        {
            using( timing = new Timing( profiler, profiler.Head, name ) ) { }
        }

        return new GroupTiming( profiler, timing );
    }
}

      

+2


source


You want to wrap your recursive calls inside a using statement. Something like that:

using (profiler.Step("Step A"))
    { 
        // something recursive here
        Thread.Sleep(100);
    }

      

Step A will be the total of your calls. Within this statement, you can do whatever you want. "Step A" can be any information you need. You can add any string value and it will appear in the profiler. You can also add simple text information simply by taking a "step" without using.



MiniProfiler.Current.Step("Some code after this");

      

This just inserts one line into the profiler's output window. I've used this for throwing exceptions or other debugging information.

0


source







All Articles