2011年9月26日 星期一

ASP.NET MVC 3 + MiniProfiler.MVC3 偵測執行效能


上一篇文章「ASP.NET MVC2 + MiniProfiler 偵測執行效能」介紹了在MVC2的專案中使用miniprofiler偵測執行效能,

而文章的最後有說到,MVC2 + miniprofiler並無法去偵測到EF所產生的SQL指令碼,

或許是還要改某些地方才可以讓MVC2專案偵測EF的SQL執行,我就沒有再多去研究,

但是看到許多的文章都是以MVC3來做說明,另外在Nuget上面也有看到miniprofiler.MVC3,

所以就直接用MVC3開個新專案來使用miniprofiler。


miniprofiler.MVC3 (1.9.1)

Nuget gallery:http://nuget.org/List/Packages/MiniProfiler.MVC3

PM> Install-Package MiniProfiler.MVC3

image

 

安裝完成之後,在Global.asax程式中一樣也要增加以下兩個Method:

protected void Application_BeginRequest()
{
  if (Request.IsLocal)
  {
    MiniProfiler.Start();
  }
}
protected void Application_EndRequest()
{
  MiniProfiler.Stop();
}

 

而ViewPage的部份,我這邊MVC3所使用的ViewEngine是「Razor」,所以MasterPage就是要去改「_Layout.cshtml」

這個檔案是預設的檔案,修改的部份如下:

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.6.4.min.js")" type="text/javascript"></script>
  @MvcMiniProfiler.MiniProfiler.RenderIncludes()
</head>

也是一樣在head區塊中去增加以下的程式:

@MvcMiniProfiler.MiniProfiler.RenderIncludes()

另外,使用Nuget安裝MiniProfiler.MVC3後,也會增加一些檔案,

例如在目錄「Views/Shared」增加「_MINIPROFILER UPDATED Layout.cshtml」

image

就是告訴你要如何去加入ViewPage的顯示

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.6.4.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/modernizr-2.0.6-development-only.js")" type="text/javascript"></script>
    
    @* Make sure you've added this one line to your LAYOUT or MASTER PAGE *@
    
    @MvcMiniProfiler.MiniProfiler.RenderIncludes()
        
</head>

 

而在Controller的Action中增加增側區塊的方式也是以MVC2時一樣:

public ActionResult Index()
{
  var profiler = MiniProfiler.Current;
  using(profiler.Step("Home Index"))
  {
    ViewBag.Message = "歡迎使用 ASP.NET MVC!";
    return View();
  }
}

 

就來看看執行的畫面:

image

而展開的詳細資料比MVC2(miniprofiler 1.9)的版本還多出一些:

image

image

 

說好的ADO.NET Entity Framework + SQL偵測呢?

接下來我們增加使用ADO.NET Entity Framework,利用EF對資料庫取得一些資料,讓我們來試試MiniProfiler.MVC3的SQL偵測。

首先,請先利用Nuget去安裝MiniProfiler.EF

image

另外因為專案中的ADO.NET Entity Framework版本也需要使用「4.1.10715.0」所以也透過Nuget來安裝
(如果沒有安裝的話,就無法啟用偵測SQL的功能)

於Manage NuGet Packages中去尋找「EntityFramework」並且安裝。

image

 

上面的兩個Packages都安裝完成後,我們要去修改目錄「App_Start」下的「MiniProfiler.cs」檔案

image

首先請修改檔案最前面的using:

using System;
using System.Web;
using System.Web.Mvc;
using System.Linq;
using MvcMiniProfiler;
using MvcMiniProfiler.MVCHelpers;
using Microsoft.Web.Infrastructure;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
//using MvcMiniProfiler.Data.EntityFramework;
//using MvcMiniProfiler.Data.Linq2Sql;

主要是將原本被註解起來的兩行給解除,

using System.Data.Entity;
using System.Data.Entity.Infrastructure;

接下來一樣是在MiniProfiler.cs中,去找第37行的地方,一樣也是把註解給拿掉:

  //TODO: If you are profiling EF code first try: 
  MiniProfilerEF.Initialize();


再來就是修改Controller的Action:

  public ActionResult Index()
  {
    var profiler = MiniProfiler.Current;
    using (profiler.Step("Home Index"))
    {
      ViewBag.Message = "歡迎使用 ASP.NET MVC!";
      using (profiler.Step("Retrive Cities"))
      using (var db = new TestDBEntities())
      {
        var cities = db.PostalCity.Select(x => x).OrderBy(x => x.SORT);
        int count = cities.Count();
        ViewBag.CitiesCount = count.ToString();
      }
      return View();
    }
  }

在原本的偵測區塊裡,我們增加一個透過EF取得資料庫資料的Linq語法,並且用另一個偵測區塊將它給包起來,

接著就來看看實際執行的畫面:

可以看到展開的資料區塊項目「Retrive Vities」後面多了一個「1 sql」的字串

image

再對著「1 sql」點擊,可以看到由EF所產生的SQL指令碼。

image

這樣一來,就可以在畫面中立即看到當下頁面透過EF對資料庫存取時所產生的SQL指令為何,不用再去透過SQL Profiler,

有些開發者的環境因為安裝的是SQL Express版本而無法使用SQL Profiler查看,這一個功能簡直是福音,

而且在頁面上就可以看出這個頁面到底對資料庫是執行了多少次的存取動作、用了多少時間、所使用的SQL Script都一目了然。

 

藉由MiniProfiler.MVC3這樣一個Package的加持,在開發時期的效能偵測就更加得心應手、更加方便!

 

延伸閱讀:

Sam Saffron - Profiling your website like a true Ninja

Scott Hanselman - NuGet Package of the Week #9 - ASP.NET MiniProfiler from StackExchange rocks your world

MiniProfiler: Lightweight profiler for ASP.NET web applications

KingKong Bruce記事: ASP.NET MVC - 使用MVC MiniProfiler測試您的網頁跑多快

Jun1st Notes - 使用MiniProfiler来优化你EntityFramework的查询

 

2011-09-28 補充

Simple fast and useful MiniProfiler for ASP.NET MVC

 

以上

沒有留言:

張貼留言

提醒

千萬不要使用 Google Talk (Hangouts) 或 Facebook 及時通訊與我聯繫、提問,因為會掉訊息甚至我是過了好幾天之後才發現到你曾經傳給我訊息過,請多多使用「詢問與建議」(在左邊,就在左邊),另外比較深入的問題討論,或是有牽涉到你實作程式碼的內容,不適合在留言板裡留言討論,請務必使用「詢問與建議」功能(可以夾帶檔案),謝謝。