MiniProfiler是一款針對.NET, Ruby, Go and Node.js的性能分析的輕量級程序。能夠對一個頁面自己,及該頁面經過直接引用、Ajax、Iframe形式訪問的其它頁面進行監控,監控內容包括數據庫內容,並能夠顯示數據庫訪問的SQL(支持EF、EF CodeFirst等 )。而且以很友好的方式展示在頁面上。html
MiniProfiler官網:http://miniprofiler.com/git
MiniProfiler的一個特別有用的功能是它與數據庫框架的集成。除了.NET原生的 DbConnection類,MiniProfiler還內置了對實體框架(Entity Framework)以及LINQ to SQL、RavenDb和MongoDB的支持。任何執行的Step都會包括當時查詢的次數和所花費的時間。爲了檢測常見的錯誤,如N+1反模式,profiler將檢測僅有參數值存在差別的多個查詢。github
MiniProfiler是以Apache License V2.0協議發佈的,你能夠在NuGet找到。web
過去一直使用Sqlserver Profiler,可是發現實在是太痛苦了,你不得不進行新建、過濾、清除、關閉等操做,並且過濾篩選每每比較難以控制。後來發現MiniProfiler工具很是好用。sql
同類監控工具備NanoProfiler,下載地址:https://github.com/ef-labs/nanoprofiler/issues/1數據庫
Demo開發環境app
準備工做框架
新建MVC項目WebAppEF,使用Northwind數據庫。工具
一、先安裝MiniProfiler佈局
二、安裝MiniProfiler.MVC4
三、安裝MiniProfiler.EF
四、修改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 } } }
五、在你的佈局頁(_Layout)中,好比如下這種結構,修改_Layout.cshtml
@using StackExchange.Profiling; <head> .. </head> <body> ... @MiniProfiler.RenderIncludes() </body>
六、修改配置文件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>
七、添加控制器測試代碼
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(); }
八、按F5調試運行
說明:標記爲duplicate的部分,表明在一次請求當中,重複執行了查詢,能夠進行優化。經過Step能夠對獨立的sql塊進行標記。
分析:錯誤提示的大意是在試圖爲DbConfiguration 實例加Loaded事件以前已經在其它地方使用了這個實例了
解決方案:把MiniProfiler.EF6.Initialize()在放在Database.SetInitializer<WebAppEF.Models.NorthwindEntities>(null); 以前。
分析:找不到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中的版本號是否一致,若是不一致就要對版本號進行修改。
參考: