Occasionally when starting a new profiler, the step named MiniProfiler Prep takes much longer than what is being profiled to complete. For example, a request to one of my API endpoints took 129.3ms to complete, with 127.50ms of that being MiniProfiler Prep (https://github.com/MiniProfiler/dotnet/blob/master/src/MiniProfiler.AspNetCore/MiniProfilerMiddleware.cs#L77).
I think this is due to the call to SetHeadersAndState (https://github.com/MiniProfiler/dotnet/blob/master/src/MiniProfiler.AspNetCore/MiniProfilerMiddleware.cs#L142), which gets the IDs of the unviewed profiles from the underlying Storage, which I have set to an SqlServerStorage, and this query occasionally takes a while to execute. This adds a lot of time to perform an API request, which is a bit longer than I'd prefer.
There are two solutions I have thought of to work around this problem.
-
Implement MiniProfilerOptions.ResultsAuthorize and implement middleware to handle authorization instead.
-
Created a wrapper IAsyncStorage around SqlServerStorage.
- The wrapper
IAsyncStorage would internally use an SqlServerStorage for all methods (ex: public async Task SaveAsync(MiniProfiler p) => _InternalStorage.SaveAsync(p)), except for GetUnviewedIds, it would just return an empty List<Guid>, and not execute the SQL query.
While I don't see any issues with both of these methods (Haven't actually tried them yet), I would much prefer just having an option during .ConfigureServices to not get the past profilers. For example:
services.AddMiniProfiler(options => {
options.GrabPastProfilers = false;
});
This would be used in MiniProfilerOptions.SetHeadersAndState:
From:
var profilerIds = (isAuthorized ? await Options.ExpireAndGetUnviewedAsync(current.User).ConfigureAwait(false) : null)
?? new List<Guid>(1);
To:
var profilerIds = (isAuthorized && Options.GrabPastProfilers ? await Options.ExpireAndGetUnviewedAsync(current.User).ConfigureAwait(false) : null)
?? new List<Guid>(1);
Thank you for taking the time to read this, let me know if you have any questions and I'll get back to you as soon as I can.
Occasionally when starting a new profiler, the step named
MiniProfiler Preptakes much longer than what is being profiled to complete. For example, a request to one of my API endpoints took 129.3ms to complete, with 127.50ms of that beingMiniProfiler Prep(https://github.com/MiniProfiler/dotnet/blob/master/src/MiniProfiler.AspNetCore/MiniProfilerMiddleware.cs#L77).I think this is due to the call to
SetHeadersAndState(https://github.com/MiniProfiler/dotnet/blob/master/src/MiniProfiler.AspNetCore/MiniProfilerMiddleware.cs#L142), which gets the IDs of the unviewed profiles from the underlyingStorage, which I have set to anSqlServerStorage, and this query occasionally takes a while to execute. This adds a lot of time to perform an API request, which is a bit longer than I'd prefer.There are two solutions I have thought of to work around this problem.
Implement
MiniProfilerOptions.ResultsAuthorizeand implement middleware to handle authorization instead..ResultsAuthorizeto only return true when actually viewing the results of a profiler, getting the unviewed IDs in.SetHeadersAndStateis bypassed asisAuthorizedwould be false (https://github.com/MiniProfiler/dotnet/blob/master/src/MiniProfiler.AspNetCore/MiniProfilerMiddleware.cs#L150)Created a wrapper
IAsyncStoragearoundSqlServerStorage.IAsyncStoragewould internally use anSqlServerStoragefor all methods (ex:public async Task SaveAsync(MiniProfiler p) => _InternalStorage.SaveAsync(p)), except forGetUnviewedIds, it would just return an emptyList<Guid>, and not execute the SQL query.While I don't see any issues with both of these methods (Haven't actually tried them yet), I would much prefer just having an option during
.ConfigureServicesto not get the past profilers. For example:This would be used in
MiniProfilerOptions.SetHeadersAndState:From:
To:
Thank you for taking the time to read this, let me know if you have any questions and I'll get back to you as soon as I can.