2016年6月14日 星期二

NanoProfiler 對於資料庫存取的監控 - Dapper

上一篇「NanoProfiler 對於資料庫存取的監控 - ADO.NET」的最後有講到使用 ADO.NET 進行資料存取處理雖然可以使用 NanoProfiler 將程式的執行效能作監控,但是卻無法將執行的 T-SQL 內容給記錄起來,所以算是一個未完成品。

現在這一篇則是改用 Dapper 取代 ADO.NET 的操作,然後再使用 NanoProfiler.Data 去進行資料存取的監控並且將執行的 T-SQL 給記錄起來。

 


這個部落格在去年九月時有發表了一系列數篇的 Dapper 相關文章,其中有一篇就是使用了同樣的一個範例,將原本已經有 ADO.NET, Enterprise Library Data Access Application Block, Entity Framework 三種資料存取方式的 Repository 又多增加了一個使用 Dapper 的 Repository,

ASP.NET MVC 的 Model 使用 Dapper

這篇就依照該篇文章的實作去增加使用 Dapper 的 Repository,然後接著的步驟就跟上一篇「NanoProfiler 對於資料庫存取的監控 - ADO.NET」一樣,透過 NuGet 在專案裡安裝 NanoProfiler.Data 套件,建立介面 IDatabaseHelper 與實作類別 DatabaseHelper,

image

再來就是在 EmployeeRepository.cs 裡使用 ProfilingSession 將資料存取的程式包起來,

using System.Collections.Generic;
using System.Linq;
using Dapper;
using EF.Diagnostics.Profiling;
using Sample.Domain;
using Sample.Repository.Interface;
 
namespace Sample.Repository.Dapper
{
    public class EmployeeRepository : BaseRepository, IEmployeeRepository
    {
        private IDatabaseHelper DatabaseHelper { get; set; }
 
        public EmployeeRepository(IDatabaseHelper databaseHelper)
        {
            this.DatabaseHelper = databaseHelper;
        }
 
        public Employee GetOne(int id)
        {
            using (ProfilingSession.Current.Step("EmployeeRepository - GetOne"))
            {
                var profiledDbConnection =
                    this.DatabaseHelper.GetProfiledConnection(this.ConnectionString, ProfilingSession.Current);
 
                string sqlStatement = "select * from Employees where EmployeeID = @EmployeeID";
 
                using (var conn = profiledDbConnection)
                {
                    var result = conn.Query<Employee>(
                        sqlStatement,
                        new
                        {
                            EmployeeID = id
                        })
                        .FirstOrDefault();
 
                    return result;
                }
            }
        }
 
        public IEnumerable<Employee> GetEmployees()
        {
            using (ProfilingSession.Current.Step("EmployeeRepository - GetEmployees"))
            {
                var profiledDbConnection =
                    this.DatabaseHelper.GetProfiledConnection(this.ConnectionString, ProfilingSession.Current);
 
                string sqlStatement = "select * from Employees order by EmployeeID";
 
                using (var conn = profiledDbConnection)
                {
                    var result = conn.Query<Employee>(sqlStatement);
                    return result;
                }
            }
        }
    }
}

 

再來就是讓 Sample.Web.WebApi 專案改用 Sample.Repository.Dapper,

SNAGHTMLacaf2b2

image

 

上面的步驟都處理完成後就直接執行,在 Postman 裡多執行幾次,最後再開啟 NanoProfiler 的 Profiling Results 頁面,

image

點開第一個執行的 api/Employee 項目,查看 Detail 內容,這時候有沒有看到什麼呢?紅色的部分就是NanoProfiler.Data 將資料存取的 T-SQL 執行的時間與內容給記錄起來,

image

在 Detail 裡會將該次 Request 裡有對資料庫存取的次數與時間給統計起來

image

點選紅色項目、T-SQL 前面的 [data] 連結,會開啟一個 modal window 顯示 T-SQL 的內容,

image

如果是有參數的話,就會在 modal window 裡顯示,例如:

image

image

 


過去在 ASP.NET MVC 的專案裡要看這樣的監控資料還可以使用 MiniProfiler 或 Glimpse,現在 ASP.NET WebApi 專案裡也可以藉由 NanoProfiler 去監控並觀察程式執行的效能,尤其是 NanoProfiler.Data 的搭配使用,讓以往不好去觀測的 T-SQL 執行內容與效能數據可以更方便的清楚看到。

對了,之前曾經介紹過的 Prefix 也一樣可以做到監測,但使用上就不像 NanoProfiler 這麼簡單與方便,但顯示介面則是 Prefix 比較漂亮,當然有蠻多需要花錢購買的 APM 系統也一樣可以都做到同樣的功能,但畢竟功能完備的 APM 並不是每個開發團隊能夠負擔得起的。

 

延伸閱讀

Profile ASP.NET Application 使用 Prefix

ASP.NET MVC 的 Model 使用 Dapper

 

參考資料

NanoProfiler Wiki - 4. How to enable DB profiling?

NanoProfiler - 适合生产环境的性能监控类库 之 基本功能篇 - Teddy's Knowledge Base - 博客园

 

以上

沒有留言:

張貼留言

提醒

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