MVC教程--MiniProfiler.EF監控調試MVC和EF的性能


上一篇談到mvc中ef輸出執行sql日志;來談用mvc開發項目的調試和性能監控。EF框架自動給我生成sql語句,當我們的程序遇到性能問題的時候我們可以用MiniProfiler.EF來監控調試MVC和EF的性能,查看生成的sql語句、運行了哪些sql,以及所花的時間。MiniProfiler.EF,一個輕量級開源的mvc性能調試、監控組件MiniProfiler專門為EF定制的版本。下面通過一個具體一例子的說明怎么在我們的項目中用MiniProfiler.EF監控調試MVC和EF的性能。

 

1、安裝

MiniProfiler

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

 

 

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

安裝MiniProfiler.Mvc4,MiniProfiler.EF5(當然針對項目使用的EF版本;貌似只有EF4+之后的才可以使用)

 

2、添加配置相關代碼到項目里面

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

修改之后完整內容為:

using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using StackExchange.Profiling;
using StackExchange.Profiling.EntityFramework6;
namespace MiniProfilerDemo
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            MiniProfilerEF6.Initialize();
        }
        protected void Application_BeginRequest()
        {
            MiniProfiler.Start();
        }
        protected void Application_EndRequest()
        {
            MiniProfiler.Stop();
        }
    }
}

 

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

2、修改_Layout.cshtml視圖文件

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

 

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

如下圖:

 

3、在Web.config加入代碼

 

  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的調試跟蹤時間必須要加入上面的代碼。如下圖:

 

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

3、查看運行結果

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

可以看到左角多了一個數字的區塊,表示這個頁面所花的毫秒數,點擊上面的數字就可以彈出詳細的時間跟蹤信息,並且可以看到sql語句(顯示為紅色需要優化):

 

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

現在我們為ProductController加上監控,監控獲取從數據庫中獲取Product記錄所花的時間。我們把ProductController修改為:
using MiniProfilerDemo.DAL;
using System.linq;
using System.Web.Mvc;
using StackExchange.Profiling;
using System.Collections.Generic;
using MiniProfilerDemo.Models;
namespace MiniProfilerDemo.Controllers
{
    public class ProductController : Controller
    {
        public ActionResult Index()
        {
            using (EFDbContext db = new EFDbContext())
            {
                var profiler = MiniProfiler.Current;
                List<Product> m;
                using (profiler.Step("獲取Product列表"))
                {
                    m = db.Products.ToList();
                }
                return View(m);
            }
        }
    }
}

 

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

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM