Profiling ASP.NET MVC application using RavenProfiler

To setup profiling in RavenDB using RavenProfiler you must follow few simple steps defined below:
1. Add Raven.Client.MvcIntegration assembly to MVC project. Alternative way is to install NuGet package that can be found here.
2. Next step is to initialize profiler on our document store. This can be achieved by adding following code in Global.asax.cs file.

protected void Application_Start()
{
	// Code omitted for simplicity

	InitializeRavenProfiler();
}

private void InitializeRavenProfiler()
{
	Raven.Client.MvcIntegration.RavenProfiler.InitializeFor(DocumentStoreHolder.Store);
}

3. Finally add @Raven.Client.MvcIntegration.RavenProfiler.CurrentRequestSessions() to _Layout.cshtml.

Viewing profiling results

To demonstrate profiler capabilities, let's create simple ASP.NET MVC 4 application and setup profiling in it.

1. How to setup ASP.NET MVC 4 application to use RavenDB can be found here.
2. Setup profiling using steps from section above.
3. Add following code to HomeController:

public class HomeController : RavenController
{
	public ActionResult Index()
	{
		var items = RavenSession
			.Query<User>()
			.Customize(x => x.WaitForNonStaleResults())
			.ToList();

		return View("Index", items);
	}

	public ActionResult Create()
	{
		return View("Create");
	}

	[HttpPost]
	public ActionResult Create(User user)
	{
		if (!ModelState.IsValid)
		{
			return View("Create", user);
		}

		RavenSession.Store(user);

		return RedirectToAction("Index");
	}
}

4. Create Index view that will list all users.
5. Create Create view that will be used to add new user.
6. Add following User model:

public class User
{
	public string Id { get; set; }

	[Required]
	public string Name { get; set; }

	[Required]
	public string Password { get; set; }
}

7. Create new user e.g. Ayende with password Rahien.
8. View profiling results in RavenProfiler, that can be found in upper left corner of the page.

Figure 1: `RavenProfiler` window
Figure 2 Request Details in `RavenProfiler`

Various information can be pulled from the profiler, starting from request duration, method, url and ending with exact request response results.

Excluding sensitive information

There are vast number of situations, where user does not want to include all the data in profiling information. For such scenarios we have included an option to filter out specific field values from results from provided list of fields.

In our previous results you may have noticed that user password has been displayed in profiling results. To filter it out, just pass array of fields that you want to filter as a second parameter of RavenProfiler.InitializerFor method.

private readonly string[] fieldsToFilter = new[] { "Password" };

private void InitializeRavenProfiler()
{
	Raven.Client.MvcIntegration.RavenProfiler.InitializeFor(DocumentStoreHolder.Store, fieldsToFilter);
}

As a result of this setting, our profiling results will now hide data from all fields with name Password.

Figure 3: Request Details in `RavenProfiler` with filter out `Password`

Profiling only in debug mode

To enable profiling only in debug mode one can use Preprocessor Directives or ConditionalAttribute.

To use Preprocessor Directives simply wrap InitializeRavenProfiler method execution with #if directive:

protected void Application_Start()
{
	// Code omitted for simplicity

#if DEBUG
	InitializeRavenProfiler();
#endif
}

If you want to use ConditionalAttribute then do as follows:

[Conditional("DEBUG")]
private void InitializeRavenProfiler()
{
	Raven.Client.MvcIntegration.RavenProfiler.InitializeFor(DocumentStoreHolder.Store);
}

The InitializeRavenProfiler method will not be executed unless DEBUG constant is specified.