In this post I will demonstrate how to add MiniProfiler to the XAF MainDemo web application.
MiniProfiler is a simple fast profiler with a pretty user interface. It is fast because it only profiles code that you have explicitly decorated with the
MiniProfiler.Step() method. It was designed by the team at StackOverflow.
First, add the MiniProfiler NuGet package to the MainDemo.Web project. Then add a placeholder to default.aspx just before the last
1 2 3 4 5 6 7 8
RenderIncludes(). Hopefully this will be fixed in a future version of MiniProfiler.)
In global.asax.cs add the following to the
1 2 3 4 5
EndRequest as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Now we implement a helper class which determines whether profiling is enabled and which URLs to profile. We can use a variety of methods, the cookie probably being the most versatile one, but for the moment, the
IsEnabled() function always returns true.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
Done. Now whenever you run the web application, you get timing statistics for the loading of the assets. They appear as little clickable ‘chiclets’ in the top left of the browser page.
However, the real strength of MiniProfiler comes with the ability to add your own profiling steps. Let’s say we want to know exactly what percentage of the load takes place in the
OnLoad event. Then we add the following to default.aspx.cs in order to add a ‘step’ to the MiniProfiler breakdown.
1 2 3 4 5 6 7 8
Now, the output is a good deal richer. Also, note that if the MiniProfiler assembly is missing from the web application’s bin directory, the profiling is ignored completely without error.
As another example, let’s profile the FindBySubject controller action.
Add the MiniProfiler NuGet package to the MainDemo.Module project. Then modify the FindBySubjectController.cs as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Now navigate to the Tasks list view and enter some text where it says ‘Type Subject…’. You should see a new chiclet appear which contains the timing details as shown here.
MiniProfiler is a great tool for providing helpful profiling benchmarks, even in production. It’s often difficult to measure when a remote user complains to support that the site seems slow. How slow is slow? In a production environment, you can turn on MiniProfiler for the user (by setting a cookie for instance) and then ask them to share their profiling information for some basic operations. This information can be invaluable in determining where the fault lies.
You can play around with the sample solution up on GitHub.