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开发环境

  • 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)中,比如以下这种结构,修改_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 object
using(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中的版本号是否一致,如果不一致就要对版本号进行修改。


来源:https://www.cnblogs.com/nepulgh/p/10766235.html

MiniProfiler性能监控分析工具在.NET项目中的使用的更多相关文章

  1. 开源性能监控分析工具glowroot

    最近在做java性能瓶颈定位分析工具的研究,发现glowroot工具是一款相当不错的APM工具(Wonderful tool),架构简洁,部署简单,上手容易. 经过亲身搭建体验,总结了产品的架构,工具 ...

  2. Zynga 开源其服务器性能监控分析工具 zPerfmon

    流年不利的社交游戏服务商Zynga近日在官方博客宣布开源zPerfmon——该工具负责监控Zynga数以千计的社交游戏服务器.zPerfmon的源代码目前已经上传至Github. 包括Facebook ...

  3. JDK自带的监控分析工具JConsole

    非常多开发人员认为自己懂Java编程.事实是大多数开发人员都仅仅领会到了Java平台的皮毛.所学也仅仅够应付工作. 作者将深度挖掘Java平台的核心功能.揭示一些鲜为人知的事实.帮助您解决最棘手的编程 ...

  4. Linux性能监控分析命令(四)—top命令介绍

    性能监控分析的命令包括如下: 1.vmstat 2.sar 3.iostat 4.top 5.free 6.uptime 7.netstat 8.ps 9.strace 10.lsof ======= ...

  5. UAVStack JVM监控分析工具:图形化展示采集及分析监控数据

    摘要:UAVStack推出的JVM监控分析工具提供基于页面的展现方式,以图形化的方式展示采集到的监控数据:同时提供JVM基本参数获取.内存dump.线程分析.内存分配采样和热点方法分析等功能. 引言 ...

  6. Web服务器性能监控分析与优化

    Web服务器性能监控分析与优化 http://www.docin.com/p-759040698.html

  7. Linux性能监控分析命令(五)—free命令介绍

    性能监控分析的命令包括如下:1.vmstat2.sar3.iostat4.top5.free6.uptime7.netstat8.ps9.strace10.lsof 命令介绍:free命令是监控Lin ...

  8. Linux性能监控分析命令(三)—iostat命令介绍

    性能监控分析的命令包括如下: 1.vmstat 2.sar 3.iostat 4.top 5.free 6.uptime 7.netstat 8.ps 9.strace 10.lsof 命令介绍: i ...

  9. Linux性能监控分析命令(二)—sar命令介绍

    性能监控分析的命令包括如下: 1.vmstat 2.sar 3.iostat 4.top 5.free 6.uptime 7.netstat 8.ps 9.strace 10.lsof ======= ...

随机推荐

  1. struct.pack()和struct.unpack() 详解(转载)

    原文链接:https://blog.csdn.net/weiwangchao_/article/details/80395941 python 中的struct主要是用来处理C结构数据的,读入时先转换 ...

  2. jar文件无法双击打开

    1.  jdk安装后环境变量未设置好 (无jdk先自行下载) 我的电脑-属性-高级系统设置-环境变量-系统变量 找到path:添加环境变量为"java/jdk/bin"文件夹路径( ...

  3. Swagger 3.0 天天刷屏,真的香吗?

    持续原创输出,点击上方蓝字关注我 目录 前言 官方文档如何说? Spring Boot版本说明 添加依赖 springfox-boot-starter做了什么? 撸起袖子就是干? 定制一个基本的文档示 ...

  4. Docker(10)- docker create 命令详解

    如果你还想从头学起 Docker,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1870863.html 作用 创建一个新的容器但不启动它 ...

  5. solr 文档二

    SOLR 5.5.5文档 参考博文: http://blog.csdn.net/matthewei6/article/details/50620600 作者:毛平 时间:2018年1月15日 17:3 ...

  6. VSCode--HTML代码片段(基础版,react、vue、jquery)

    起因是最近在学习前端,看的网上的demo也是在react.vue.jquery之间穿插,为了方便一键生成html模板(懒)写demo,有了以下折腾. 本人使用的前端编辑工具是vscode(方便.懒), ...

  7. 【SpringBoot】07.SpringBoot文件上传

    SpringBoot文件上传 1.编写html文件在classpath下的static中 <!DOCTYPE html> <html> <head> <met ...

  8. 1.1 Prism安装

    Prism框架有很多安装包,即便用了很长一段时间,也可能会不知道如何安装框架.细心分析包的依赖关系,发现所有包均依赖与依赖注入扩展插件,以使用Unity为例,Prism.Unity依赖Prism.Wp ...

  9. Python爬虫-换行的匹配

    之前在学习爬虫的时候遇到了匹配内容时发现存在换行,这时没法匹配了,后来在网上找到了一种方法,当时懒得记录,今天突然有遇到了这种情况,想想还是在这里记录一下吧. 当时爬取的时csdn首页博客,如下图 看 ...

  10. 2.while循环

    while循环 #-*- coding: utf-8-*- #指定识别utf-8的字符串 1.while循环以及跳出循环 while True: #无限循环 print('i love pyhon') ...