zoukankan      html  css  js  c++  java
  • MVC教程--MiniProfiler.EF监控调试MVC和EF的性能

    上一篇谈到mvc中ef输出执行sql日志;来谈用mvc开发项目的调试和性能监控。EF框架自动给我生成sql语句,当我们的程序遇到性能问题的时候我们可以用MiniProfiler.EF来监控调试MVC和EF的性能,查看生成的sql语句、运行了哪些sql,以及所花的时间。MiniProfiler.EF,一个轻量级开源的mvc性能调试、监控组件MiniProfiler专门为EF定制的版本。下面通过一个具体一例子的说明怎么在我们的项目中用MiniProfiler.EF监控调试MVC和EF的性能。

    1、安装

    MiniProfiler

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

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

    安装MiniProfiler.Mvc4,MiniProfiler.EF5(当然针对项目使用的EF版本;貌似只有EF4+之后的才可以使用)

    2、添加配置相关代码到项目里面

    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加入了MiniProfilerEF.Initialize()和添加了Application_BeginRequest、Application_BeginRequest两个Application的事件函数,这个的作用分别是初始化MiniProfilerEF6和开始、结束MiniProfiler监控。

    2、修改_Layout.cshtml视图文件

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

    1. @StackExchange.Profiling.MiniProfiler.RenderIncludes()

    如下图:

    3、在Web.config加入代码

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

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

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

    3、查看运行结果

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

    可以看到左角多了一个数字的区块,表示这个页面所花的毫秒数,点击上面的数字就可以弹出详细的时间跟踪信息,并且可以看到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列表”条记录。这样可以细微监控方法内部的时间,方便、快速地帮我们找出我们的程序的瓶颈所在。

  • 相关阅读:
    【Java EE 学习 36】【struts2】【struts2系统验证】【struts2 ognl值栈】【struts2 ongl标签】【struts2 UI标签】【struts2模型驱动和令牌机制】
    【Java EE 学习 35 下】【struts2】【struts2文件上传】【struts2自定义拦截器】【struts2手动验证】
    【Java EE 学习 35 上】【strus2】【类型转换器】【struts2和Servlet API解耦】【国际化问题】【资源文件乱码问题已经解决】
    【Java EE 学习 34】【struts2学习第一天】
    【JavaScript中的正则表达式】
    【Java EE 学习 33 下】【validate表单验证插件】
    【Java EE 学习 33 上】【JQuery样式操作】【JQuery中的Ajax操作】【JQuery中的XML操作】
    【Java EE 学习 32 下】【JQuery】【JQuey中的DOM操作】
    【Java EE 学习 32 上】【JQuery】【选择器】
    【Java EE 学习 31】【JavaScript基础增强】【Ajax基础】【Json基础】
  • 原文地址:https://www.cnblogs.com/tianboblog/p/4774810.html
Copyright © 2011-2022 走看看