Search code examples
c#asynchronousmvc-mini-profiler

Async profiling with MiniProfiler.Current.AddProfilerResults version 2 and above


In version 1.9 of the mini profiler I found a way to get the SQL timings of asynchronous DB calls into the profiler results. Basically, I added a ContinueWith to my async task and then added all the SQL timings to the step that initiated the async action:

foreach (var sqlTiming in completedResult.SqlTimings)
{
    currentStep.AddSqlTiming(sqlTiming);
}

However, this no longer works in 2.0.2 - those SQL timings just disappear. There appears to be a new method that does exactly what I want (source, line 487):

/// <summary>
/// Adds <paramref name="externalProfiler"/>'s <see cref="Timing"/> hierarchy to this profiler's current Timing step,
/// allowing other threads, remote calls, etc. to be profiled and joined into this profiling session.
/// </summary>
public static void AddProfilerResults(this MiniProfiler profiler, MiniProfiler externalProfiler)
{
    ...

So it looks as if this is what I should do now:

MiniProfiler.Current.AddProfilerResults(profilerFromAsyncTask);

But, while that doesn't throw an error, it doesn't seem to add anything at all to the results. I can get the steps to appear in the step with:

currentStep.AddChild(profilerFromAsyncTask.Root)

However that still discards the SQL timings and results in negative times for the step that starts the async task.

Is there something I need to do to get the results from AddProfilerResults to appear with the SQL timings?


Solution

  • Async is not currently an actively supported or tested scenario for mini-profiler. Until there is a supported mechanism to do that, I'm loathe to dig in to find a way to hack it in, only for that hack to evaporate with the next change. Long term, async is something we should handle - it just hasn't been a requirement so far.

    I suspect your best option would be to create a suggestion on the project site (or add a comment on an existing suggestion). Or perhaps even better: figure out a mechanism to do it (that works without breaking anything or being risky) and submit a change-set.