Monitor EF and. NET MVC projects with Miniprofiler (Entity Framework Extended Series 1)

Source: Internet
Author: User

Today, the performance detection and monitoring of EF and MVC projects

First, let's introduce the tools we use today.

miniprofiler~

The introduction of this thing is as follows:

MVC Miniprofiler is a small program designed by the stack Overflow team for performance analysis of ASP. You can monitor a page itself, as well as other pages accessed through direct references, Ajax, and IFRAME, to monitor content including database content, and to display SQL for database access (EF, EF codefirst, etc.). and displayed on the page in a friendly way.

A particularly useful feature of the profiler is its integration with the database framework. Apart from. NET native DbConnection class, Profiler also has built-in support for the Entity Framework and LINQ to SQL. Any step that executes will include the number of queries and the time spent. To detect common errors, such as the n+1 anti-pattern, Profiler detects multiple queries with only a difference in parameter values.

Miniprofiler is released under the Apache License V2.0 protocol, which you can find in NuGet. Configuration and use can be seen here: Http://code.google.com/p/mvc-mini-profiler

In order to establish a fast site Gold reference standard, Yahoo 2007 for the site to improve the speed of 13 simple rules.

Above this paragraph is copy of Zhang Shanyu's blog, the original address: http://www.cnblogs.com/shanyou/archive/2012/04/03/2430977.html

Of course, Baidu also can Baidu out a lot of tutorials, but the tutorials are older and now the new version or the gap is very large, and the blog post is not very detailed, so this cock to detailed talk about it.

The environment and technology used in this paper

System: WIN7

Database: SQL Server2008

Related technologies: mvc5+ef6.1.3

Chapter One: Start monitoring

First, define the Ben Boven target, monitor the SQL and execution time of EF, and monitor the execution time of the MVC page.

So let's start.

The first step is to download the required packages from NuGet, downloading the content:

Miniprofiler core (all Miniprofiler related resources need to have him first)

It is important to note that the new version of MINIPROFILER.EF is required to be downloaded according to your EF version, divided into Miniprofiler.ef6,miniprofiler.ef5,miniprofiler.ef (EF4 below) three versions

Download the corresponding package yourself based on your EF version.

Miniprofiler.mvc4 (Note: Here the MVC4 is can be analyzed mvc4,5 two versions, use MVC3 students please download MINIPROFILER.MVC3)

At this point, all the packages we need to install are OK,

Below we start monitoring:

First, add the following to your Global.asax file:

       protected void Application_BeginRequest ()        {            if (request.islocal)//Here is allow local access to boot monitoring, can not write            {                Miniprofiler.start ();                           }        }        protected void Application_EndRequest ()        {            miniprofiler.stop ();        }

Then find the page you need to monitor and add it to the page:

@using stackexchange.profiling; @MiniProfiler. Renderincludes ();

Of course, we generally want to monitor all the pages, so I recommend adding it to your layout page (_layout), such as the following structure:

Then join in the configuration file (note that this is important here):

  <system.webServer>      

In this way, our basic monitoring is done and we look at the effect.

Chapter Two: Monitor EF and monitor the operation in a targeted direction

First we add the following code in the Global.asax file:

  protected void Application_Start ()        {  ....                           StackExchange.Profiling.EntityFramework6.MiniProfilerEF6.Initialize ();          ....        }

Because this is a simple demo, so we randomly find a controller, write some EF query, the code is as follows:

public class Homecontroller:controller    {public        actionresult Index ()        {                           using (studentinfoentities US = new Studentinfoentities ())                {                    Viewbag.data = us. Logdata.where (A = 1 = = 1). ToList ();                }                                    return View ();}        }

Let's look at the effect.

As you can see, this query uses 56.2MS, which takes up 71% of the load time of the entire page., click on the blue 56.2 to see the detailed SQL statement, as follows:

This allows us to monitor and analyze the SQL statements used by EF in the page at any time.

Targeted monitoring (important)

Of course, this is only a simple operation, we in the process of analysis will definitely encounter strange, or background code more complex situations (such as a page 10 query), this time the monitoring on the page will be very confusing, inconvenient to read, we need to conduct targeted monitoring.

We have just modified the code as follows (here we do two queries, sorted by Miniprofiler):

  public class Homecontroller:controller    {public        actionresult Index ()        {            var profiler = miniprofiler.current;            Using (Profiler. Step ("Querying Data logdata data")            {                using (studentinfoentities US = new Studentinfoentities ())                {                    Viewbag.data = us. Logdata.where (A = 1 = = 1). ToList ();                }            }            Using (Profiler. Step ("Querying Data logoperate data")            {                using (studentinfoentities US = new Studentinfoentities ())                {                    Viewbag.data = us. Logoperate.where (A = 1 = = 1). ToList ();                }            }            return View ();}        }

The following results are monitored:

In this way, we can track the results of an EF operation in detail according to our needs.

Chapter III: Permissions for monitoring (assigning permissions to administrators for monitoring)

In the actual project development, we are not able to all the users to open the monitoring permission, so we have to show him the control.

In Miniprofiler, two delegates were provided, as follows:

MiniProfiler.Settings.Results_Authorize//configure Permissions for monitoring

MiniProfiler.Settings.Results_List_Authorize//Configure permissions for historical information monitoring (in ~/mini-profiler-resources/ Results-index can see the last 100 request analysis)

Here we simply do a privilege control,

We add the following code in the Global.asax file:

  protected void Application_Start ()        {        ....            MiniProfiler.Settings.Results_Authorize = Request = =            {                string name = request.cookies["name"] = = null? "": request.cookies["name"]. Value;                if (name. Equals ("admin"))                    return true;                else                    return false;            };                           StackExchange.Profiling.EntityFramework6.MiniProfilerEF6.Initialize ();        }

This allows only the user with the name attribute of the cookie to have the admin monitor display

The effect is as follows (we can see that there is a monitoring display when the name in the cookie equals Admin):

Written in the last

At this point, the article is all over, welcome to the big God to shoot bricks.

Monitor EF and. NET MVC projects with Miniprofiler (Entity Framework Extended Series 1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.