MiniProfiler工具介绍

简介:

   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 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中的版本号是否一致,如果不一致就要对版本号进行修改。

参考:

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

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

本文转自邹琼俊博客园博客,原文链接:http://www.cnblogs.com/jiekzou/p/6374726.html,如需转载请自行联系原作者

相关文章
|
7月前
|
安全 Java PHP
【工具使用】Pystinger(毒刺)的使用
一款用于Webshell实现内网 socks4 代理、端口映射的工具,可直接用于CobaltStrike / Metasploit上线,该工具使用python语言编写,目前支持的语言有 php, jsp(x) 和 aspx 三种脚本语言。
87 0
|
10月前
|
监控 测试技术 Apache
测试工具介绍
ApacheBench、JMeter 和 Gatling 都是常用的性能测试工具,这里为您详细介绍这三个工具的操作流程。
113 0
An工具介绍之3D工具
An工具介绍之3D工具
371 0
An工具介绍之3D工具
|
安全 中间件 应用服务中间件
|
前端开发 IDE Java
开发工具介绍|学习笔记
快速学习开发工具介绍
112 0
几个工具介绍
在idea中的几个插件的介绍
|
SQL 前端开发 JavaScript
[工具使用]WhatWeb(上)
[工具使用]WhatWeb
468 0
[工具使用]WhatWeb(上)
|
SQL 安全 Shell
|
前端开发 IDE 数据挖掘
7款Python开发工具介绍,你最中意哪一款
工欲善其事必先利其器,有一款好用上手的Python编辑器能够大大提高码代码的效率,而且所带来的提升不是从 1 到 1.1 倍速,是从 1 到 10 倍速。所以行哥这里来推荐几个常用的Python开发工具,看你用过几个?
130 0
7款Python开发工具介绍,你最中意哪一款

热门文章

最新文章