ASP.NET Core WebAPI中的分析工具MiniProfiler
介紹
作為一個開發人員,你知道如何分析自己開發的Api效能麼?
在Visual Studio和Azure中, 我們可以使用Application Insight來監控專案。除此之外我們還可以使用一個免費工具Stackify Prefix,它允許追蹤所有的Http請求, 這裡有一篇部落格講解了如何使用Stackify Prefix(Scalable and Performant ASP.NET Core Web APIs: Profiling and Monitoring)。
本文我將引入另外一個工具MiniProfiler, 我將講解如何將MiniProfiler
與Stackify Prefix相似,MiniProfiler也是一款免費的工具(官網地址:https://miniprofiler.com/dotnet/),你可以使用它精確的分析ASP.NET和ASP.NET Core應用程式的任何程式碼。
Tips: MiniProfiler在ASP.NET和控制檯程式中也可以使用哦。
安裝
我們可以使用Nuget來下載這個包。
Copy
PM> Install-Package MiniProfiler.AspNetCore.Mvc
配置Startup.cs
MiniProfiler配置起來很簡單,只需要以下幾步
在ConfigureServices
方法中新增MiniProfiler服務
Copy
public void ConfigureServices(IServiceCollection services) { services.AddMiniProfiler(options => options.RouteBasePath = "/profiler" ); }
- 這裡是配置了MiniProfiler的路由基礎路徑,預設的路徑是/mini-profiler-resources
- 按照當前配置,你可以使用"/profiler/results"來訪問分析報告
啟用中介軟體,啟用MiniProfiler服務
Copy
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseMiniProfiler(); }
配置需要監控分析的程式碼
Copy
public class ValueController : ControllerBase { [HttpGet] public IEnumerable<string> Get() { string url1 = string.Empty; string url2 = string.Empty; using (MiniProfiler.Current.Step("Get方法")) { using (MiniProfiler.Current.Step("準備資料")) { using (MiniProfiler.Current.CustomTiming("SQL", "SELECT * FROM Config")) { // 模擬一個SQL查詢 Thread.Sleep(500); url1 = "https://www.baidu.com"; url2 = "https://www.sina.com.cn/"; } } using (MiniProfiler.Current.Step("使用從資料庫中查詢的資料,進行Http請求")) { using (MiniProfiler.Current.CustomTiming("HTTP", "GET " + url1)) { var client = new WebClient(); var reply = client.DownloadString(url1); } using (MiniProfiler.Current.CustomTiming("HTTP", "GET " + url2)) { var client = new WebClient(); var reply = client.DownloadString(url2); } } } return new string[] { "value1", "value2" }; } }
程式碼解釋:
MiniProfiler.Current.Step
方法定義了分析的步驟,這個方法可以接受一個String
型別的引數,它會顯示在最終的報告中MiniProfiler.Current.CustomTiming
方法是更細粒度的對報告內容進行分類,以上程式碼中定義了2種分類,一種是SQL, 一種是Http- 上述程式的功能: 模擬從資料庫拉取2個網站的Url, 並使用
WebClient
來分別請求網站的Url
檢視效果
下面我們啟動專案, 專案預設開啟/api/values
然後我們來訪問以下/profiler/results, 就會出現如下頁面
如上圖所示,我們可以很清楚的看到程式碼中每一部分的耗時,由於我們添加了2種分類SQL和Http,所以列表中會對2種分類進行彙總。
重點: 當前頁面只會顯示最近的一次請求
從當前報告中可以得到以下結論
- 當前請求總響應時間 1723.6ms
- SQL語句查詢耗時517.ms
- 2次Http請求共耗時868.3ms, 其中訪問百度耗時424.6ms, 訪問新浪耗時443.7ms
如何讓MiniProfiler與Swagger整合
這裡我們就不再講如何在ASP.NET Core中整合Swagger。
MiniProfiler和Swagger是可以整合在一起的,為了完成這個功能,我們需要進行以下幾步
下載Swagger自定義頁面
預設的index.html頁面可以從如下連結下載
下載之後將這個檔案放置到專案根目錄下。
接下來我們需要在這個檔案的頭部加入如下指令碼程式碼:
Copy
<script async="async" id="mini-profiler" src="/profiler/includes.min.js?v=4.0.138+gcc91adf599" data-version="4.0.138+gcc91adf599" data-path="/profiler/" data-current-id="4ec7c742-49d4-4eaf-8281-3c1e0efa748a" data-ids="" data-position="Left" data-authorized="true" data-max-traces="15" data-toggle-shortcut="Alt+P" data-trivial-milliseconds="2.0" data-ignored-duplicate-execute-types="Open,OpenAsync,Close,CloseAsync"> </script>
最後我們需要配置這個index.html檔案的Bulid Action為Embedded resource
安裝自定義頁面
在Startup.cs
檔案中,我們需要修改UseSwaggerUI
中介軟體的配置,這裡我們需要新增一個InputStream
配置。
Copy
app.UseSwaggerUI(c => { c.RoutePrefix = "swagger"; c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); c.IndexStream = () => GetType().GetTypeInfo().Assembly.GetManifestResourceStream("MiniProfilerSample.index.html"); });
注意:這裡
MiniProfilerSample
是專案的名稱空間名
最終效果
重新啟動專案,Swagger文件頁面的左上角就出現了一個小的面板,當模擬請求一個連線之後,它就會顯示出當前請求的分析資料,看起來是不是很酷炫。
總結
本篇部落格描述瞭如何使用MiniProfiler來監控分析你的Api。 MiniProfiler除了提供網頁顯示報告,還支援將報告結果儲存在資料庫中,後面我會補充一篇文章來說明如何將報告儲存到資料庫中。
本篇原始碼: https://github.com/lamondlu/Sample_MiniProfiler
作者:Lamond Lu