zoukankan      html  css  js  c++  java
  • 转:asp.net mvc ef 性能监控调试工具 MiniProfiler

    MiniProfiler官网:http://miniprofiler.com/

        MiniProfiler的一个特别有用的功能是它与数据库框架的集成。除了.NET原生的 DbConnection类,MiniProfiler还内置了对实体框架(Entity Framework)以及LINQ to SQL、RavenDb和MongoDB的支持。任何执行的Step都会包括当时查询的次数和所花费的时间。为了检测常见的错误,如N+1反模式,profiler将检测仅有参数值存在差异的多个查询。

    跟蓝狐学MVC教程--MiniProfiler.EF6监控调试MVC5和EF6的性能

    以前开发Webform的时候可以开启trace来跟踪页面事件,这对于诊断程序的性能是有很大的帮助的,起到事半功倍的作用,今天我就来谈用mvc开发项目的调试和性能监控。EF框架自动给我生成sql语句,当我们的程序遇到性能问题的时候我们可以用MiniProfiler.EF来监控调试MVC和EF的性能,查看生成的sql语句、运行了哪些sql,以及所花的时间。MiniProfiler.EF,一个轻量级开源的mvc性能调试、监控组件MiniProfiler专门为EF定制的版本。下面通过一个具体一例子的说明怎么在我们的项目中用MiniProfiler.EF6监控调试MVC和EF的性能。下面的项目是基于我上面的一篇文章的,MVC5与EF6 Code First 第一个入门完整实例教程

    1、安装MiniProfiler.EF6

    nuget搜索框中输入MiniProfiler,将出现下面结果:

    点击安装将把MiniProfiler.EF6相关的dll加到项目中。

    2、添加MiniProfiler.EF相关代码到项目里面

    1、在Global.asax加入MiniProfiler相关的监控代码

    修改之后完整内容为:

    using System.Web.Mvc;
    using System.Web.Optimization;
    using System.Web.Routing;
    using StackExchange.Profiling;
    using StackExchange.Profiling.EntityFramework6;
    namespace MiniProfilerDemo
    {
        public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
                MiniProfilerEF6.Initialize();
            }
            protected void Application_BeginRequest()
            {
                MiniProfiler.Start();
            }
            protected void Application_EndRequest()
            {
                MiniProfiler.Stop();
            }
        }
    }

    其中是在Application_Start加入了MiniProfilerEF6.Initialize()和添加了Application_BeginRequest、Application_BeginRequest两个Application的事件函数,这个的作用分别是初始化MiniProfilerEF6和开始、结束MiniProfiler监控。

    2、修改_Layout.cshtml视图文件

    在ViewsShared\_Layout.cshtml文件的body前面加上一段代码,让监控展示在页面上。

    @StackExchange.Profiling.MiniProfiler.RenderIncludes()

    如下图:

    3、在Web.config加入代码

    <system.webServer>
      <handlers>
      <add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
      </handlers>
    </system.webServer>

    为了要在页面上显示MVC和EF的调试跟踪时间必须要加入上面的代码。如下图:

    在system.webServer配置结点下的handlers结点,加入了一个名为MiniProfiler的handler。

    3、查看运行结果

    运行程序,查看页面如下图:

    可以看到左角多了一个数字的区块,表示这个页面所花的毫秒数,点击上面的数字就可以弹出详细的时间跟踪信息,如下图:

    可以看到这个页面运行了三个sql语句,sql所花时间为您673.4毫秒,占部时间为为12.5%。还可以点击这个sql的时间,将显示运行了哪些sql,如下图:

    4、细微监控方法内部的时间

    现在我们为ProductController加上监控,监控获取从数据库中获取Product记录所花的时间。我们把ProductController修改为:

    using MiniProfilerDemo.DAL;
    using System.linq;
    using System.Web.Mvc;
    using StackExchange.Profiling;
    using System.Collections.Generic;
    using MiniProfilerDemo.Models;
    namespace MiniProfilerDemo.Controllers
    {
        public class ProductController : Controller
        {
            public ActionResult Index()
            {
                using (EFDbContext db = new EFDbContext())
                {
                    var profiler = MiniProfiler.Current;
                    List<Product> m;
                    using (profiler.Step("获取Product列表"))
                    {
                        m = db.Products.ToList();
                    }
                    return View(m);
                }
            }
        }
    }
    重新生成项目,再次运行查看页面,如下图:

    可以看到上面多了我们刚才手动加的“获取Product列表”条记录。这样可以细微监控方法内部的时间,方便、快速地帮我们找出我们的程序的瓶颈所在。

    原文地址:http://www.lanhusoft.com/Article/125.html

    注:此文针对EF6,nuget中有EF5的版本,稍加改动即可。

  • 相关阅读:
    Sencha Touch 使用笔记
    区数据
    省市 数据
    js校验身份证
    js 邮政编码验证
    原生js添加class
    让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法
    js学习笔记 Function类型属性的理解
    js学习笔记 理解原型对象
    js学习笔记 chapter5 引用类型
  • 原文地址:https://www.cnblogs.com/superfeeling/p/9024196.html
Copyright © 2011-2022 走看看