MiniProfiler

之前開發Webform的時候能夠開啓trace來跟蹤頁面事件,這對於診斷程序的性能是有很大的幫助的,起到事半功倍的做用,今天我就來談用mvc開發項目的調試和性能監控。EF框架自動給我生成sql語句,當咱們的程序遇到性能問題的時候咱們能夠用MiniProfiler.EF來監控調試MVC和EF的性能,查看生成的sql語句、運行了哪些sql,以及所花的時間。MiniProfiler.EF,一個輕量級開源的mvc性能調試、監控組件MiniProfiler專門爲EF定製的版本。下面經過一個具體一例子的說明怎麼在咱們的項目中用MiniProfiler.EF6監控調試MVC和EF的性能。下面的項目是基於我上面的一篇文章的,MVC5與EF6 Code First 第一個入門完整實例教程html

一、安裝MiniProfiler.EF6

nuget搜索框中輸入MiniProfiler,將出現下面結果:web

 

點擊安裝將把MiniProfiler.EF6相關的dll加到項目中。

二、添加MiniProfiler.EF相關代碼到項目裏面

一、在Global.asax加入MiniProfiler相關的監控代碼sql

修改以後完整內容爲:數據庫

  1. using System.Web.Mvc;
  2. using System.Web.Optimization;
  3. using System.Web.Routing;
  4. using StackExchange.Profiling;
  5. using StackExchange.Profiling.EntityFramework6;
  6. namespace MiniProfilerDemo
  7. {
  8. public class MvcApplication : System.Web.HttpApplication
  9. {
  10. protected void Application_Start()
  11. {
  12. AreaRegistration.RegisterAllAreas();
  13. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  14. RouteConfig.RegisterRoutes(RouteTable.Routes);
  15. BundleConfig.RegisterBundles(BundleTable.Bundles);
  16. MiniProfilerEF6.Initialize();
  17. }
  18. protected void Application_BeginRequest()
  19. {
  20. MiniProfiler.Start();
  21. }
  22. protected void Application_EndRequest()
  23. {
  24. MiniProfiler.Stop();
  25. }
  26. }
  27. }

其中是在Application_Start加入了MiniProfilerEF6.Initialize()和添加了Application_BeginRequest、Application_BeginRequest兩個Application的事件函數,這個的做用分別是初始化MiniProfilerEF6和開始、結束MiniProfiler監控。mvc

二、修改_Layout.cshtml視圖文件框架

在Views\Shared\_Layout.cshtml文件的body前面加上一段代碼,讓監控展現在頁面上。函數

 

  1. @StackExchange.Profiling.MiniProfiler.RenderIncludes()

以下圖:性能

 

三、在Web.config加入代碼spa

 

  1. <system.webServer>
  2. <handlers>
  3. <add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
  4. </handlers>
  5. </system.webServer>

爲了要在頁面上顯示MVC和EF的調試跟蹤時間必需要加入上面的代碼。以下圖:3d

 

在system.webServer配置結點下的handlers結點,加入了一個名爲MiniProfiler的handler。

三、查看運行結果

運行程序,查看頁面以下圖:

能夠看到左角多了一個數字的區塊,表示這個頁面所花的毫秒數,點擊上面的數字就能夠彈出詳細的時間跟蹤信息,以下圖:

 

能夠看到這個頁面運行了三個sql語句,sql所花時間爲您673.4毫秒,佔部時間爲爲12.5%。還能夠點擊這個sql的時間,將顯示運行了哪些sql,以下圖:

 

四、細微監控方法內部的時間

如今咱們爲ProductController加上監控,監控獲取從數據庫中獲取Product記錄所花的時間。咱們把ProductController修改成:
  1. using MiniProfilerDemo.DAL;
  2. using System.linq;
  3. using System.Web.Mvc;
  4. using StackExchange.Profiling;
  5. using System.Collections.Generic;
  6. using MiniProfilerDemo.Models;
  7. namespace MiniProfilerDemo.Controllers
  8. {
  9. public class ProductController : Controller
  10. {
  11. public ActionResult Index()
  12. {
  13. using (EFDbContext db = new EFDbContext())
  14. {
  15. var profiler = MiniProfiler.Current;
  16. List<Product> m;
  17. using (profiler.Step("獲取Product列表"))
  18. {
  19. m = db.Products.ToList();
  20. }
  21. return View(m);
  22. }
  23. }
  24. }
  25. }

從新生成項目,再次運行查看頁面,以下圖:

 

能夠看到上面多了咱們剛纔手動加的「獲取Product列表」條記錄。這樣能夠細微監控方法內部的時間,方便、快速地幫咱們找出咱們的程序的瓶頸所在。

 

文章轉載自:藍狐軟件工做室 » 跟藍狐學MVC教程--MiniProfiler.EF6監控調試MVC5和EF6的性能

相關文章
相關標籤/搜索