|
Project Information
Members
Links
|
A simple but effective mini-profiler for ASP.NET MVC and ASP.NET. Official Github clone: https://github.com/SamSaffron/MiniProfiler IMPORTANT for the next release the namespace for Mini Profiler is changing. Instead of using MvcMiniProfiler use using StackExchange.Profiling What does it profile?It does not attach itself to every single method call; that would be too invasive and wouldn't focus on the biggest performance issues. Instead, it provides:
Simple. Fast. Pragmatic. Useful. Other What is included?A single assembly, with an example ASP.NET and ASP.NET MVC project to show usage. Getting Started1: Write out our css and javascript includes in the master page/layout, after jQuery: <head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
@MvcMiniProfiler.MiniProfiler.RenderIncludes()
</head>For requests that aren't being profiled, nothing will be written. 2: Decide when you want to profile; perhaps for local requests, perhaps for special users (developers, admins, test team, whatever). The sample project starts profiling in Global.asax.cs Application_BeginRequest: using MvcMiniProfiler;
...
protected void Application_BeginRequest()
{
if (Request.IsLocal)
{
MiniProfiler.Start();
}
}3: For the code you want to profile, use (regardless of whether the profiler is null or not): using MvcMiniProfiler;
...
var profiler = MiniProfiler.Current; // it's ok if this is null
using (profiler.Step("Set page title"))
{
ViewBag.Title = "Home Page";
}
using (profiler.Step("Doing complex stuff"))
{
using (profiler.Step("Step A"))
{ // something more interesting here
Thread.Sleep(100);
}
using (profiler.Step("Step B"))
{ // and here
Thread.Sleep(250);
}
}You can make this as granular or high-level as you like; passing a MiniProfiler in as an optional argument (defaulting to null) to downstream methods works well. 4: stop the profiler; the sample project does this in its Global.asax.cs: protected void Application_EndRequest()
{
MiniProfiler.Stop();
}If all goes well, you'll see something like this:
Database profilingThe profiler includes powerful and comprehensive database profiling capabilities. To enable wrap your database connection with a profiling connection. The built in database profiler support any kind of DbConnection. It also supports Entity Framework and Linq-2-SQL. Usage: Use a factory to return your connection: public static DbConnection GetOpenConnection()
{
var cnn = CreateRealConnection(); // A SqlConnection, SqliteConnection ... or whatever
// wrap the connection with a profiling connection that tracks timings
return new MvcMiniProfiler.Data.ProfiledDbConnection(cnn, MiniProfiler.Current);
}Entity Framework public static MyModel Get()
{
var conn = new MvcMiniProfiler.Data.EFProfiledDbConnection(GetConnection(), MiniProfiler.Current);
return ObjectContextUtils.CreateObjectContext<MyModel>(conn); // resides in the MiniProfiler.EF nuget pack
}LINQ-2-SQLpartial class DBContext
{
public static DBContext Get()
{
var conn = new MvcMiniProfiler.Data.ProfiledDbConnection(GetConnection(), MiniProfiler.Current);
return new DBContext(conn);
}
}Entity Framework - Code FirstMVC Mini Profiler can be hooked in to an EF code first project. To do so: Install the Nuget MiniProfiler.EF package: Run: MiniProfilerEF.Initialize(); // in Application_Start That's it ... you are done :) Note: EF Code First will store table metadata in a table called: EdmMetadata. This metadata uses the provider as part of the entity key. If you initialized your provider as a non-profiled provider, you will have to re-build this metadata. Deleting all the rows from EdmMetadata may do the trick, alternatively some smarter providers are able to handle this transparently. EF 4.1 Update 1 Note, that EF 4.1 Update 1 (version currently on NuGet) has a breaking change which throws the following error when specifying a connection string: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) MiniProfiler attempts to resolve this issue by detecting the version of EntityFramework that it is running against. If this doesn't work (due to security exceptions), force the hack to be applied by replacing the Initialize() call with: MiniProfilerEF.Initialize_EF42(); // in Application_Start However, this is currently unable to profile SqlCE or Oracle databases. To work around this an additional parameter has been added to the Initialize call:
Note, this should be a temporary solution until EF 4.2 is widely available. Various features(N+1) and duplicate query detectionThe profiler is able to detect and highlight areas where you are executing the same query multiple times with different parameters. This allows you to quickly find queries you may be able to batch.
What about AJAX?The profiler is also able to log all ajax calls:
You will get one profile timing per ajax call. Abandoning a profiler sessionOften at the beginning of a session you may not know if a user should or should not be allowed to profile, a common pattern for dealing with such issues is: protected void Application_BeginRequest()
{
MvcMiniProfiler.MiniProfiler.Start();
}
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if(!CurrentUserIsAllowedToSeeProfiler())
{
MvcMiniProfiler.MiniProfiler.Stop(discardResults: true);
}
}Where is profiler used?MVC Mini Profiler was designed by the team at Stack Overflow. It is in production use there and on the Stack Exchange family of sites. Also used at: - https://acturent.com - http://jobseriously.com - http://www.plane9.com (if you are using this in production please let us know and we will add you to the list) Frequently Asked QuestionsIf you have other questions, please visit our Frequently Asked Questions wiki, this will be updated as common issues come to our attention. Also, see the Stack Overflow mvc-mini-profiler tag for other common questions. |