Question

I've only just started looking a profiling for an MVC3 application.

I found MiniProfiler first, and then found recommendations for using Glimpse. Both seem great, and I'd rather use Glimpse, but I'd like to add entries to the timeline for specific actions.

MiniProfiler has a nice feature where you can grab the current MiniProfiler context and add a step with the using() command. Is there a similar thing with Glimpse?

I did find an entry with someone explaining how to do it the long way, but wondered if there might have been a shorter way to do this, since then.

Was it helpful?

Solution 2

The Google group thread you mentioned is currently the approach that is being developed in version-2 of Glimpse. There you can find the GlimpseTimeline class that does what you want.

You could take this class and copy it in your own project while waiting for v2 to be released. Just keep in mind that you need to alter it a little bit, as some things like the MessageBroker are to be retrieved from the GlimpseConfiguration.GetConfiguredMessageBroker() etc...

But it might suit your needs for now...

OTHER TIPS

Thanks to this stackoverflow question, I found a gist that implements an equivalent of Glimpse v2's GlimpseTimeline for Glimpse v1.

From your code call:

using (Timeline.Capture("FormulaEvaluator.Evalauate"))
{
    // Code to time
}

And here's the implementation of Timeline.Capture:

public static class Timeline
{
        public static IDisposable Capture(string eventName)
        {
        #pragma warning disable 618
            var timer = GlimpseConfiguration.GetConfiguredTimerStrategy()();
            if (timer == null)
                return null;
            var broker = GlimpseConfiguration.GetConfiguredMessageBroker();
            if (broker == null)
                return null;
        #pragma warning restore 618
            return new TimelineCapture(timer, broker, eventName);
        }

}

public class TimelineCapture : IDisposable
{
        private readonly string _eventName;
        private readonly IExecutionTimer _timer;
        private readonly IMessageBroker _broker;
        private readonly TimeSpan _startOffset;

        public TimelineCapture(IExecutionTimer timer, IMessageBroker broker, string eventName)
        {
            _timer = timer;
            _broker = broker;
            _eventName = eventName;
            _startOffset = _timer.Start();
        }

        public void Dispose()
        {
            _broker.Publish(new TimelineMessage(_eventName, _timer.Stop(_startOffset)));
        }
}

public class TimelineMessage : ITimelineMessage
{
        private static readonly TimelineCategoryItem DefaultCategory = new TimelineCategoryItem("MyApp", "green", "blue");

        public TimelineMessage(string eventName, TimerResult result)
        {
            Id = Guid.NewGuid();
            EventName = eventName;
            EventCategory = DefaultCategory;
            Offset = result.Offset;
            StartTime = result.StartTime;
            Duration = result.Duration;
        }

        public Guid Id { get; private set; }
        public TimeSpan Offset { get; set; }
        public TimeSpan Duration { get; set; }
        public DateTime StartTime { get; set; }
        public string EventName { get; set; }
        public TimelineCategoryItem EventCategory { get; set; }
        public string EventSubText { get; set; }
}

I made a mashup of droyads gist and GlimpseTimeline from v2. GlimpseTimeline v1 gist

public class TimelineMessage : ITimelineMessage
{
    public TimelineMessage()
    {
        Id = Guid.NewGuid();
    }

    public Guid Id { get; private set; }
    public TimeSpan Offset { get; set; }
    public TimeSpan Duration { get; set; }
    public DateTime StartTime { get; set; }
    public string EventName { get; set; }
    public TimelineCategoryItem EventCategory { get; set; }
    public string EventSubText { get; set; }
}

public static class GlimpseTimeline
{
    private static readonly TimelineCategoryItem DefaultCategory = new TimelineCategoryItem( "User", "green", "blue" );

    public static OngoingCapture Capture( string eventName )
    {
        return Capture( eventName, null, DefaultCategory, new TimelineMessage() );
    }

    public static OngoingCapture Capture( string eventName, string eventSubText )
    {
        return Capture( eventName, eventSubText, DefaultCategory, new TimelineMessage() );
    }

    internal static OngoingCapture Capture( string eventName, TimelineCategoryItem category )
    {
        return Capture( eventName, null, category, new TimelineMessage() );
    }

    internal static OngoingCapture Capture( string eventName, TimelineCategoryItem category, ITimelineMessage message )
    {
        return Capture( eventName, null, category, message );
    }

    internal static OngoingCapture Capture( string eventName, ITimelineMessage message )
    {
        return Capture( eventName, null, DefaultCategory, message );
    }

    internal static OngoingCapture Capture( string eventName, string eventSubText, TimelineCategoryItem category, ITimelineMessage message )
    {
        if (string.IsNullOrEmpty( eventName ))
        {
            throw new ArgumentNullException( "eventName" );
        }

        #pragma warning disable 618
        var executionTimer = GlimpseConfiguration.GetConfiguredTimerStrategy()();
        var messageBroker = GlimpseConfiguration.GetConfiguredMessageBroker();
        #pragma warning restore 618

        if (executionTimer == null || messageBroker == null)
        {
            return OngoingCapture.Empty();
        }

        return new OngoingCapture( executionTimer, messageBroker, eventName, eventSubText, category, message );
    }

    public static void CaptureMoment( string eventName )
    {
        CaptureMoment( eventName, null, DefaultCategory, new TimelineMessage() );
    }

    public static void CaptureMoment( string eventName, string eventSubText )
    {
        CaptureMoment( eventName, eventSubText, DefaultCategory, new TimelineMessage() );
    }

    internal static void CaptureMoment( string eventName, TimelineCategoryItem category )
    {
        CaptureMoment( eventName, null, category, new TimelineMessage() );
    }

    internal static void CaptureMoment( string eventName, TimelineCategoryItem category, ITimelineMessage message )
    {
        CaptureMoment( eventName, null, category, message );
    }

    internal static void CaptureMoment( string eventName, ITimelineMessage message )
    {
        CaptureMoment( eventName, null, DefaultCategory, message );
    }

    internal static void CaptureMoment( string eventName, string eventSubText, TimelineCategoryItem category, ITimelineMessage message )
    {
        if (string.IsNullOrEmpty( eventName ))
        {
            throw new ArgumentNullException( "eventName" );
        }

        #pragma warning disable 618
        var executionTimer = GlimpseConfiguration.GetConfiguredTimerStrategy()();
        var messageBroker = GlimpseConfiguration.GetConfiguredMessageBroker();
        #pragma warning restore 618

        if (executionTimer == null || messageBroker == null)
        {
            return;
        }

        message
            .AsTimelineMessage( eventName, category, eventSubText )
            .AsTimedMessage( executionTimer.Point() );

        messageBroker.Publish( message );
    }

    public class OngoingCapture : IDisposable
    {
        public static OngoingCapture Empty()
        {
            return new NullOngoingCapture();
        }

        private OngoingCapture()
        {
        }

        public OngoingCapture( IExecutionTimer executionTimer, IMessageBroker messageBroker, string eventName, string eventSubText, TimelineCategoryItem category, ITimelineMessage message )
        {
            Offset = executionTimer.Start();
            ExecutionTimer = executionTimer;
            Message = message.AsTimelineMessage( eventName, category, eventSubText );
            MessageBroker = messageBroker;
        }

        private ITimelineMessage Message { get; set; }

        private TimeSpan Offset { get; set; }

        private IExecutionTimer ExecutionTimer { get; set; }

        private IMessageBroker MessageBroker { get; set; }

        public virtual void Stop()
        {
            var timerResult = ExecutionTimer.Stop( Offset );

            MessageBroker.Publish( Message.AsTimedMessage( timerResult ) );
        }

        public void Dispose()
        {
            Stop();
        }

        private class NullOngoingCapture : OngoingCapture
        {
            public override void Stop()
            {
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top