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模式。同时,MiniProfiler还支持授权,这里不做介绍。

using StackExchange.Profiling;
using StackExchange.Profiling.EntityFramework6;
using System;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;namespace WebAppEF
{
publicclassMvcApplication : System.Web.HttpApplication{
protectedvoid Application_Start(){
#if DEBUG
MiniProfilerEF6.Initialize();
#endif
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);}
protectedvoid Application_BeginRequest(Object source, EventArgs e){
#if DEBUG
MiniProfiler.Start();
#endif}
protectedvoid Application_EndRequest(){
#if DEBUG
MiniProfiler.Stop();
#endif}}
}

5、在你的布局页(_Layout)中,比如以下这种结构,修改_Layout.cshtml

@using StackExchange.Profiling;
<head>..
</head>
<body>...@MiniProfiler.RenderIncludes()
</body>

6、修改配置文件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>

7、添加控制器测试代码
        public ActionResult Index(){// create the entity objectusing (NorthwindEntities mobjentity = new NorthwindEntities()){ViewBag.SelectCustomer = mobjentity.Customers.Select(x => x.City == "Delhi").ToList();}var profiler = MiniProfiler.Current;using (profiler.Step("查询Customers的数据")){using (NorthwindEntities entity = new NorthwindEntities()){ViewBag.data = entity.Customers.ToList();}}return View();}

8、按F5调试运行

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

常见错误

1、The Entity Framework was already using a DbConfiguration instance before an attempt was made to add an 'Loaded' event handler. 'Loaded' event handlers can only be added as part of application start up before the Entity Framework is used. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.

分析:错误提示的大意是在试图为DbConfiguration 实例加Loaded事件之前已经在其它地方使用了这个实例了

解决方案:把MiniProfiler.EF6.Initialize()在放在Database.SetInitializer<WebAppEF.Models.NorthwindEntities>(null); 之前。

2、Could not load file or assembly 'MiniProfiler, Version=3.0.11.0, Culture=neutral, PublicKeyToken=b44f9351044011a3' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

分析:找不到MiniProfiler程序集或者它的依赖项。程序集定义和引用不匹配。

解决方案:查看Web.config中是否存在如下配置节点

<dependentAssembly>
<assemblyIdentityname="MiniProfiler"publicKeyToken="b44f9351044011a3"culture="neutral" />
<bindingRedirectoldVersion="0.0.0.0-3.2.0.157"newVersion="3.2.0.157" />
</dependentAssembly>

如果不存在则添加,如果存在,则检查MiniProfiler版本号和packages.config中的版本号是否一致,如果不一致就要对版本号进行修改。

参考:

使用MiniProfiler调试ASP.NET MVC网站性能

采用MiniProfiler监控EF与.NET MVC项目(Entity Framework 延伸系列1)

转载于:https://www.cnblogs.com/sjqq/p/7507741.html

MiniProfiler工具介绍相关推荐

  1. 软件包管理 之 软件在线升级更新yum 图形工具介绍

    作者:北南南北 来自:LinuxSir.Org 提要:yum 是Fedora/Redhat 软件包管理工具,包括文本命令行模式和图形模式:图形模式的yum也是基于文本模式的:目前yum图形前端程序主要 ...

  2. IDEA IntelliJ 开发工具介绍

    IntelliJ IDEA 开发Java的IDE 官网:https://www.jetbrains.com/ 下面是该工具介绍,有兴趣的可以看下,基本一堆废话: 集成开发环境(IDE,Integrat ...

  3. postman 接口测试工具介绍

    postman 接口测试工具介绍 https://www.cnblogs.com/fly_dragon/p/9186745.html

  4. Java代码缺陷自动分析工具介绍

    Java代码缺陷自动分析工具介绍                                                                                     ...

  5. 77.Linux系统日志,screen工具介绍

    Linux系统日志 日志重要吗?必须的,没有日志我们怎么知道系统状况?没有日志如何排查一个trouble?日志记录了系统每天发生的各种各样的事情,你可以通过他来检查错误发生的原因,或者受到***时** ...

  6. Exchange Server 2003多服务器安装以及管理工具介绍

    Exchange服务器系列课程之二--Exchange Server 2003多服务器安装以及管理工具介绍 http://www.z8soft.com/article/server| 2011年4月1 ...

  7. soapui工具_python接口自动化(四)--接口测试工具介绍(详解)

    简介 "工欲善其事必先利其器",通过前边几篇文章的介绍,大家大致对接口有了进一步的认识.那么接下来让我们看看接口测试的工具有哪些. 目前,市场上有很多支持接口测试的工具.利用工具进 ...

  8. Java新手看招 常用开发工具介绍

    Java新手看招 常用开发工具介绍 Java的应用越来越广泛,学习Java的人也越来越多.学过程序设计的人知道,使用Basic进行程序设计,可以使用QBasic.Visual Basic等开发工具:使 ...

  9. arcgis python工具-使用python制作ArcGIS插件(1)工具介绍

    使用python制作ArcGIS插件(1)工具介绍 by 李远祥 ArcGIS从10.0开始支持addin(ArcGIS软件中又叫作加载项)的方式进行插件制作.相对于以往9.x系列,addin的无论是 ...

  10. 10.28rsync工具介绍10.2910.30rsync常用选项10.31rsync通过ssh同步

    10.28 rsync工具介绍 本机同步,将passwd同步到/tmp/更名为1.txt 远程的的同步方法 10.29/10.30 rsync常用选项 加上L同步软链接时会把源文件给同步 aming3 ...

最新文章

  1. 使用函数对4*4的二维数组转置
  2. 谷歌程序员犯低级错误?少打一个字符引发重大 Bug,致大量 Chromebook 无法解锁...
  3. MySQL installer
  4. 《研磨设计模式》chap10 中介者模式Mediator(2)应用举例
  5. POJ-1845 Sumdiv 逆元,特殊情况
  6. 用铁丝做“电路板”,这波操作你不得不服!
  7. Apache Kafka
  8. Android emulator error: x86 emulation currently requires hardware acceleration的解决方案
  9. HTML注释的表示方法,如何表示HTML注释
  10. 金蝶软件常见问题及解决方案
  11. Python提取docx格式Word文档中所有尾注
  12. 360WiFi的服务器网站,360随身无线wifi怎么搭建web认证网页
  13. php redis setex如何获取,php-redis中文参考手册_Ping_echo_set_get_setex_psetex_...
  14. //菱形,内藏十字架
  15. ps导出gif+html,ps怎么导出gif格式的动图
  16. nltk download所需包
  17. Redis数据类型有哪些?
  18. 程序员辞职的一万个理由
  19. php $_files怎么上传,如何利用​PHP中的 $_FILES上传文件
  20. 实现一个go语言的简单爬虫来爬取CSDN博文(一)

热门文章

  1. 开发基于GUI的R包(2)
  2. DirectX11 With Windows SDK--13 动手实现一个简易Effects框架、阴影效果绘制
  3. jqGrid复制表格数据到另外一张表中
  4. PAT 1031 查验身份证
  5. php扩展开发2--添加类
  6. 【技术文档】jeecg3.7.3-maven搭建环境入门
  7. Kia#39;s Calculation(贪心)
  8. 优秀程序设计的原则(可以多读读)
  9. c1flexGrid 在单元格中显示图片, 及行号
  10. WebSocket,不再“轮询”