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