Using StackExchange MiniProfiler

Hi, I’m trying to reproduce the profiling info available in SS Profiler but using StackExchange Miniprofiler.

Therefore I want to have the following steps profiled:

  • Deserialize Request
  • Executing Request Filters
  • Execute Service
  • Executing Response Filters
  • Writing to Response

I created my Service Runner to handle the Execute Service: and the Executing Request Filters steps:

  public class ProfiledServiceRunner<T> : ServiceRunner<T>
    {
        public ProfiledServiceRunner(IAppHost appHost, ActionContext actionContext)
            : base(appHost, actionContext)
        {
        }

        public override void OnBeforeExecute(IRequest requestContext, T request)
        {
            requestContext.Items["MiniProfilerExecute"] = MiniProfiler.Current.Step("Executing Service");
            base.OnBeforeExecute(requestContext, request);
        }

        public override object OnAfterExecute(IRequest requestContext, object response)
        {
            var disposable = requestContext.Items["MiniProfilerExecute"] as IDisposable;
            if (disposable != null)
                disposable.Dispose();
            return base.OnAfterExecute(requestContext, response);
        }

        public override void BeforeEachRequest(IRequest requestContext, T request)
        {
            requestContext.Items["MiniProfilerRequest"] = MiniProfiler.Current.Step("Executing Request");
            base.BeforeEachRequest(requestContext, request);
        }

        public override object AfterEachRequest(IRequest requestContext, T request, object response)
        {
            var disposable = requestContext.Items["MiniProfilerRequest"] as IDisposable;
            if (disposable != null)
                disposable.Dispose();
            return base.AfterEachRequest(requestContext, request, response);
        }
    }

The Executing Service step works fine. But I get a weird result for my Executing Request step, which has a negative duration:

http://content.screencast.com/users/carlos.mendes/folders/Jing/media/e4b2439e-e2e4-4bc2-899e-459cb7249343/2015-04-16_2123.png! (please remove the ! from the URL to see the result. apparently new users cannot post images)

What I’m I missing here?

Also, how can I profile the missing steps?

  • Deserialize Request
  • Executing Request Filters
  • Executing Response Filters
  • Writing to Response

Thanks in advance

Profiling the missing steps would require modifying the ServiceStack source code and inject custom profiler at different parts of the code-base.

I’ve added a MiniProfilerExtensions.CustomStepFn that would allow you to intercept the existing calls to the built-in Mini Profiler with your own, in this commit: https://github.com/ServiceStack/ServiceStack/commit/b06b778b7bf72fbdd16c72d8e17a427e5ed9cbc1

This change is now available on MyGet.

FYI, I’ve also increased Discourse default quota’s for new users so new posts should be less restrictive.

Thank you Demis.

I cannot use this with the StackExchange MiniProfiler:

Maybe I didn’t get the correct way of using this.

Thank you also increasing the quota’s in the forum!

It’s a delegate that you should be able to use to call your Custom Profiler instead of the built-in profilter, e.g with something like:

MiniProfilerExtensions.CustomStepFn = (oldProfiler, name) => 
    StackExchange.Profiling.MiniProfiler.Current.Step(name);

Got it. This is great! Thanks again