zoukankan      html  css  js  c++  java
  • MiniProfiler工具介绍(监控EF生成的SQL语句)--EF,迷你监控器,哈哈哈

    十年河东,十年河西,莫欺少年穷...

    今天是抄袭的别人的博客,不过我感觉蛮好,挺有用,特别是老板让你优化EF项目SQL耗时的情况下,你可以采用这种方式来优化你的LINQ。

    时间很宝贵,废话还是不多说,直接入主题

      MiniProfiler是一款针对.NET, Ruby, Go and Node.js的性能分析的轻量级程序。可以对一个页面本身,及该页面通过直接引用、Ajax、Iframe形式访问的其它页面进行监控,监控内容包括数据库内容,并可以显示数据库访问的SQL(支持EF、EF CodeFirst等 )。并且以很友好的方式展现在页面上。

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

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

        MiniProfiler是以Apache License V2.0协议发布的,你可以在NuGet找到。

      过去一直使用Sqlserver Profiler,但是发现实在是太痛苦了,你不得不进行新建、过滤、清除、关闭等操作,而且过滤筛选往往比较难以控制。后来发现MiniProfiler工具非常好用。

      同类监控工具有NanoProfiler,下载地址:https://github.com/ef-labs/nanoprofiler/issues/1

    Demo演示

    Demo开发环境

    • Win10
    • VS2013

    准备工作

    新建MVC项目WebAppEF,使用Northwind数据库。

    1、先安装MiniProfiler

    2、安装MiniProfiler.MVC4

    3、安装MiniProfiler.EF

    4、修改Global.asax文件

    我这里只需要在开发环境使用SQL性能监控,所以使用了#if DEBUG,因为生产环境,我们一般是采用release模式。

    using StackExchange.Profiling;
    using StackExchange.Profiling.EntityFramework6;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Http;
    using System.Web.Mvc;
    using System.Web.Routing;
    
    namespace BingFa.UI
    {
        // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
        // visit http://go.microsoft.com/?LinkId=9394801
        public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                 #if DEBUG
                MiniProfilerEF6.Initialize();
                 #endif
                AreaRegistration.RegisterAllAreas();
    
                WebApiConfig.Register(GlobalConfiguration.Configuration);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
            }
            protected void Application_BeginRequest(Object source, EventArgs e)
            {
                 #if DEBUG
                MiniProfiler.Start();
                 #endif
            }
            protected void Application_EndRequest()
            {
                #if DEBUG
                MiniProfiler.Stop();
                #endif
            }
        }
    }

    5、在你的布局页(_Layout)中/或者普通页面,加入以下这种结构,修改_Layout.cshtml为:

    @using StackExchange.Profiling;
    <head>
     ..
    </head>
    <body>
      ...
      @MiniProfiler.RenderIncludes()
    </body>
    6、修改配置文件Web.config,在节点<handlers>之间加如下配置
    <handlers>
    <add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode"/>
    </handlers>
    7、页面中注册:MiniProfiler
            public ActionResult Index()
            {  
                var profiler = MiniProfiler.Current;
               
                using (profiler.Step("查询Student的数据"))
                {
                    using (BingFaTestEntities context = new BingFaTestEntities())
                    {
                        var b = context.Student.Where(A => A.StuName.Contains("")).ToList();
    
                    }
                }
                using (profiler.Step("查询Student的数据"))
                {
                    using (BingFaTestEntities context = new BingFaTestEntities())
                    {
                        var a = context.Student.AsNoTracking().Where(A => A.StuName.Contains("")).ToList();
    
                    }
                }
                return View();
            }
    View Code
    8、按F5调试运行

    说明:标记为duplicate的部分,代表在一次请求当中,重复执行了查询,可以进行优化。通过Step可以对独立的sql块进行标记。

    @陈卧龙的博客
  • 相关阅读:
    linux中mysql,mongodb,redis,hbase数据库操作
    hbase数据库操作
    hdfs基本操作
    linux及hadoop基本操作
    创建有关hbase数据库的项目时所遇到的问题
    数据科学与大数据
    《TCP/IP详解卷1:协议》——第2章:链路层(转载)
    《TCP/IP详解卷1:协议》——第1章:概述(转载)
    深入理解计算机操作系统——第9章:缓存,存储器管理
    深入理解计算机操作系统——第9章:物理和虚拟寻址,地址空间
  • 原文地址:https://www.cnblogs.com/chenwolong/p/MiniProfile.html
Copyright © 2011-2022 走看看