Introduction
In today’s article we will see how to use the performance profiler in Visual Studio 2019 to check various diagnostic information for our application in .NET. This is an extremely critical part of application development and we can consider performance as one of the most important aspects of any application. We will look at the CPU usage but there are other items which can be profiled as well.
Running the Performance Profiler
As an example, for testing the performance profiler, I will be using the Web API application I created in a previous article that reads data from a local instance of Mongo DB.
You can find this article
here. This application is loaded and run from the Visual Studio 2019 Community Edition.
Open the solution and select Debug, Performance Profile (It is better to run in Release mode for more accurate results) as below,
After that you will be presented with the below options,
As you can see in the above screenshot, we have selected the CPU Usage tool. Next, click the start button and the application will start running. In this case, the Web API will be launched in IIS Express and the default browser will open in which we can test the APIs as below,
Now, with all pages visited, we can close the browser and quit the web api screen as below,
Next, stop the collection of data
We will then see the summary page of our results as below,
In the above you can see that the maximum CPU usage was by the JIT. This is followed by the Kernel. After that we have IO which makes sense and finally the Networking and File System. If anyone of these main categories seems out of place like the File System is at the top of the list for a test with simple database calls and no large file access, we can be sure there is a problem.
To see the hot path, click on the link in the Hot Path section and we see a detail of the CPU Usage during our small test as below,
Finally, in the above we can see the CPU usage in different functions, which is the highest in the Employee Service constructor where we create a reference to the MongoDB.
This is followed by the main program bootstrapping and then the calls to various controller actions and services. In this application, functions were simple. However, in a more complex application you would need to look into each function in great detail and ensure that the CPU usage time is logical and if there is any function that is taking CPU usage percentage higher than expected, this would need to be investigated. Here, your judgement and analysis will play a major role.
Summary
In this article, we looked at how to run the performance profiler from Visual Studio 2019 and see the CPU usage for the different options that we executed while the application was collecting diagnostics data. There are other options available as well and I would recommend that you try those options as well. I would recommend not only running the performance profiler when there is a performance related issue but making it a regular practice to run it after each significant code change to ensure there is no bottleneck in the application code. Happy coding!