web應用程序的性能相信是你們廣泛關心的一個問題,也相信你們有不少工具可用來分析應用程序的性能並可以找到其中的瓶頸,MiniProfiler
就是這個領域中的一款產品,它是一款簡單的,功能強大的web應用分析工具,MiniProfiler
可用來幫助咱們找到 慢查詢, 慢響應 等問題。html
MiniProfiler 可用在 Asp.Net
和 ASP.Net Core
中,這篇文章將會討論如何使用 MiniProfiler,並經過它找到應用程序的性能問題。web
要想使用 MiniProfiler
,須要經過 nuget 引用 MiniProfiler.AspNetCore.Mvc
包,能夠經過 Visual Studio 2019 的 NuGet package manager
可視化界面安裝 或者 經過 NuGet package manager
命令行工具輸入如下命令:sql
dotnet add package MiniProfiler.AspNetCore.Mvc
安裝好以後,接下來就要將 MiniProfiler 注入到 ServiceCollection 容器中,以下代碼所示:數據庫
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddMiniProfiler(options => options.RouteBasePath = "/profiler"); }
注入好以後,接下來就須要使用 UseMiniProfiler
擴展方法將其注入到 Request Pipeline 管道中,以下代碼所示:mvc
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) { app.UseMiniProfiler(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); }
而後在 _Layout.cshtml
頁面中增長以下兩行命令。app
@using StackExchange.Profiling @addTagHelper *, MiniProfiler.AspNetCore.Mvc
最後須要在 WebPage
中指定 MiniProfiler 分析窗口應該顯示的位置,那如何作呢? 在 body 標籤內使用 mini-profiler
標記,以下代碼所示:工具
<mini-profiler position="@RenderPosition.Right" max-traces="5" />
MiniProfiler 會提供 頁面加載時間
和 數據庫查詢性能指標
,接下來把程序跑起來,你會看到以下的性能指標圖。性能
有些朋友可能就要問了,大致時間我是知道了,那若是我只想獲取某一指定代碼塊的執行時間呢? 固然也是能夠的,下面的代碼展現瞭如何去實現。ui
public class HomeController : Controller { ILogger<HomeController> logger; public HomeController(ILogger<HomeController> logger) { this.logger = logger; } public IActionResult Index() { var miniProfiler = MiniProfiler.Current; List<Author> authors = new List<Author>(); miniProfiler.RenderIncludes(this.HttpContext); using (miniProfiler.Step("Get Authors")) { authors.Add(new Author() { Id = 1, FirstName = "Joydip", LastName = "Kanjilal", Address = "Hyderabad, India" }); authors.Add(new Author() { Id = 2, FirstName = "Stephen", LastName = "Smith", Address = "NY, USA" }); authors.Add(new Author() { Id = 3, FirstName = "Anand", LastName = "Narayanan", Address = "Chennai, India" }); authors.Add(new Author() { Id = 4, FirstName = "Steve", LastName = "Jones", Address = "London, UK" }); } return View(authors); } } public class Author { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } }
從上面的代碼中能夠看到,我用 using (miniProfiler.Step("Get Authors"))
作了語句塊標記,理論上 mini-profile 窗口上應該有相似 Get Authors
指標欄,接下來把程序跑起來,一塊兒來看看效果。this
除了順向操做,你也能夠指定讓某些代碼塊不要顯示在 mini-profile 中,須要作的是調用 Ignore()
便可,以下代碼所示:
using (MiniProfiler.Current.Ignore()) { // Write code here that you don't // want MiniProfiler to profile }
除了作一些常規的頁面分析,還能夠直接對 ADO.NET 查詢性能進行分析,這就🐂👃了,要這麼作的話,須要使用 ProfileDbConnection
和 ProfileDbCommand
便可,以下代碼所示:
public IActionResult Index() { using (SqlConnection connection = new SqlConnection(@"Data Source=.; Initial Catalog=PYZ_L; Trusted_Connection=Yes")) { using (ProfiledDbConnection profiledDbConnection = new ProfiledDbConnection(connection, MiniProfiler.Current)) { if (profiledDbConnection.State != System.Data.ConnectionState.Open) { profiledDbConnection.Open(); } using (SqlCommand command = new SqlCommand("Select * From Clothes", connection)) { using (ProfiledDbCommand profiledDbCommand = new ProfiledDbCommand(command, connection, MiniProfiler.Current)) { var data = profiledDbCommand.ExecuteReader(); //Write code here to populate the list of Authors } } } } return View(); }
從上圖能夠看到,確實對 ADO.NET 查詢有着清晰的分析,相信在幫助你們分析問題時頗有幫助。
MiniProfiler 是一個可應用於 .NET, Ruby, Go 和 Node.js 的性能分析工具,你能夠使用 MiniProfiler 去分析 Dapper,Linq2SQL,Entity Framework 所使用的sql的查詢性能,此外 MimiProfile 之因此 Mini,意味着它介入到你的應用程序中所帶來的性能開銷微乎其微,因此你們可放心的丟到生產上去吧!
譯文連接:https://www.infoworld.com/article/3597060/how-to-use-recyclablememorystream-in-net-core.html