In this article, let's learn about how to use
Mini Profiler in
WebAPI in ASP.NET Core.
Table of Contents
- Why MiniProfiler?
- Custom Profiling
- Persisting Profiling Data in Database
- Useful Configurations
- Complete Configurations
MiniProfiler is a simple but effective library and UI for profiling your application. Mini profiler provides an ADO.NET profiler, capable of profiling calls on raw ADO.NET (SQL Server, Oracle, etc), LINQ-to-SQL, Entity Framework (including Code First and EF Core), and a range of other data access scenarios. Mini profiler also helps to profile code paths explicitly using steps.
Miniprofiler helps to quickly configure and identify performance issues in your code base. There are many tools available for profiling application during development but it can be challenging to use those tools specially in production environment. MiniProfiler is a very lightweight, opensourced, stable and easy to use profiling library for ASP.Net applications which can profile dotnet application during runtime without change to the environment and effect on the application. It is maintained and backed by STackOverflow and used in StackExchange family of sites.
MiniProfiler profiles each and every part of your code starting from filters to model binding and from controller execution to database calls.
AddEntityFrameworkservices to dependency injection container and configure
Code Sample - WebAPI Mini Profiler Configuration
app.UseMiniProfiler()to the request pipeline.
- Your project is ready for profiling. Build the project and run it. Call your API via Swagger or any other tools.
To see the results navigate to
https://localhost:5001/profiler/results-index in your browser.
To check the details of API call, click on respective entry from profiling results index page.
From the details page you can get insights on how much time each step took along with the database queries used in the API call. This will help you to quickly identify the major bottleneck in your API. Note that if your not using Entity Framework for data access then you need to do additional configuration to record database queries. You can find them in their official documentation on how to profile sql using the following link.
Now to analyse further and see which portion of our code block or logic takes more time we can wrap the code block using
MinimalAPI introduced in .NET 6 as example here.
Code Sample - WebAPI Mini Profiler with Custom Step
Now if we again run the app and navigate to profiler results, we can notice the timings recorded for our custom step.
Persisting Profiling Data in Database
We can also store the profiling data in database for further analysis. To do so we need to add another provider Nuget Package given by MiniProfiler. I'm using Postgres SQL and I'll be installing corresponding provider. You can find the list of available providers in the following link.
- Install Nuget Package
Modify your Mini Profiler Configuration and pass the Provider in the options as shown below.
Code Sample - WebAPI Mini Profiler Persisting Data Configuration
Note that you need to create database tables to store profiling details. This is a manual step. You can get the database scripts using the below code.
Code Sample - WebAPI Mini Profiler Persisting Data Database Scripts
Now after creating database run the API and check the database. Now all the profile results are stored in Database and can be seen anytime for further analysis.
The profiling data is sensitive data and will provide lot of insight about the code flow so you don’t want it to be available to everyone who is having access to application but only to certain group or role of user. Luckily, access of profiling data can be restricted with authorization mechanism.
To do so, modify the MiniProfiler Configuration as follows,
Code Sample - WebAPI Mini Profiler Authorization Configuration
For simplicity, I have added code only for checking if user is authorized or not, but it can be enhanced to check if user is in certain role or not. thus restricting access to only certain group or role of user. With these changes only authorized user can access the profile results.
Now when we try to navigate to profiling results page, we get unauthorized error.
|Exclude certain request path from profiling|
|Include only certain request path for profiling|
You can implement ShouldProfile menthod as follows.
Here is the complete configuration that I have used for this demo.
Code Sample - WebAPI Mini Profiler Complete Configuration
In this article we learn't how to profile performance issues in ASP.NET Core WebAPI using Miniprofiler and we also learnt how to persist data in database and how to configure authorization. We also saw some bonus tips to simplify and remove unnecessary routes from getting profiled.