首页 技术 正文
技术 2022年11月8日
0 收藏 941 点赞 1,359 浏览 3401 个字

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290