👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Profiling Web API with Mini Profiler

Profiling Web API with Mini Profiler

WebAPI

19 Articles

Improve

In this article, let's learn about how to use Mini Profiler in WebAPI in ASP.NET Core.

Table of Contents

  1. Introduction
  2. Why MiniProfiler?
  3. Steps
  4. Custom Profiling
  5. Persisting Profiling Data in Database
  6. Authorization
  7. Useful Configurations
  8. Complete Configurations
  9. Summary

Introduction

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.

Why MiniProfiler?

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.

Steps

  1. Install MiniProfiler.AspNetCore.Mvc and MiniProfiler.EntityFrameworkCore Nuget Packages.
  2. Add AddMiniProfiler and AddEntityFramework services to dependency injection container and configure RouteBasePath.

    Code Sample - WebAPI Mini Profiler Configuration

  3. Configure app.UseMiniProfiler() to the request pipeline.
  4. Your project is ready for profiling. Build the project and run it. Call your API via Swagger or any other tools.
Swagger Response

To see the results navigate to https://localhost:5001/profiler/results-index in your browser.

Profiler Results Index

To check the details of API call, click on respective entry from profiling results index page.

Profiler Results

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.

Custom Profiling

Now to analyse further and see which portion of our code block or logic takes more time we can wrap the code block using MiniProfiler.Current.Step. I'm 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.

Profiler Results WIth 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.

  1. Install Nuget Package MiniProfiler.Providers.PostgreSql.
  2. Modify your Mini Profiler Configuration and pass the Provider in the options as shown below.

    Code Sample - WebAPI Mini Profiler Persisting Data Configuration

  3. 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

    Database Tables

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.

Profiler Index Query Result
Profiler Query Result

Authorization

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.

UnAuthorized Access

Useful Configurations

Configuration Description
Exclude certain request path from profiling options.IgnoredPaths.Add("/swagger");
Include only certain request path for profiling options.ShouldProfile = request => ShouldProfile(request);

You can implement ShouldProfile menthod as follows.

bool ShouldProfile(HttpRequest request) => request.Path.StartsWithSegments("/api");
Configure theme options.ColorScheme = ColorScheme.Dark;

Complete Configurations

Here is the complete configuration that I have used for this demo.

Code Sample - WebAPI Mini Profiler Complete Configuration

Summary

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.

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • Webapi
  • Profiling
  • Mini Profiler