如何在 ASP.Net Core 中使用 MiniProfiler

开发 前端
MiniProfiler 可用在 Asp.Net 和 ASP.Net Core 中,这篇文章将会讨论如何使用 MiniProfiler,并通过它找到应用程序的性能问题。

 [[380275]]

本文转载自微信公众号「码农读书」,作者码农读书 。转载本文请联系码农读书公众号。

web应用程序的性能相信是大家普遍关心的一个问题,也相信大家有很多工具可用来分析应用程序的性能并能够找到其中的瓶颈,MiniProfiler 就是这个领域中的一款产品,它是一款简单的,功能强大的web应用分析工具,MiniProfiler 可用来帮助我们找到 慢查询, 慢响应 等问题。

MiniProfiler 可用在 Asp.Net 和 ASP.Net Core 中,这篇文章将会讨论如何使用 MiniProfiler,并通过它找到应用程序的性能问题。

安装 MiniProfiler

要想使用 MiniProfiler,需要通过 nuget 引用 MiniProfiler.AspNetCore.Mvc 包,可以通过 Visual Studio 2019 的 NuGet package manager 可视化界面安装 或者 通过 NuGet package manager 命令行工具输入以下命令:

  1. dotnet add package MiniProfiler.AspNetCore.Mvc 

安装好之后,接下来就要将 MiniProfiler 注入到 ServiceCollection 容器中,如下代码所示:

  1. // This method gets called by the runtime. Use this method to add services to the container. 
  2.        public void ConfigureServices(IServiceCollection services) 
  3.        { 
  4.            services.AddControllersWithViews(); 
  5.  
  6.            services.AddMiniProfiler(options => options.RouteBasePath = "/profiler"); 
  7.        } 

注入好之后,接下来就需要使用 UseMiniProfiler 扩展方法将其注入到 Request Pipeline 管道中,如下代码所示:

  1. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) 
  2.         { 
  3.             app.UseMiniProfiler(); 
  4.  
  5.             app.UseEndpoints(endpoints => 
  6.             { 
  7.                 endpoints.MapControllerRoute( 
  8.                     name"default"
  9.                     pattern: "{controller=Home}/{action=Index}/{id?}"); 
  10.             }); 
  11.         } 

然后在 _Layout.cshtml 页面中增加如下两行命令。

  1. @using StackExchange.Profiling 
  2. @addTagHelper *, MiniProfiler.AspNetCore.Mvc 

最后需要在 WebPage 中指定 MiniProfiler 分析窗口应该显示的位置,那如何做呢?在 body 标签内使用 mini-profiler 标记,如下代码所示:

  1. <mini-profiler position="@RenderPosition.Right" max-traces="5" /> 

在 ASP.Net Core MVC 中使用 MiniProfiler

MiniProfiler 会提供 页面加载时间 和 数据库查询性能指标,接下来把程序跑起来,你会看到如下的性能指标图。

有些朋友可能就要问了,大体时间我是知道了,那如果我只想获取某一指定代码块的执行时间呢?当然也是可以的,下面的代码展示了如何去实现。

  1. public class HomeController : Controller 
  2.     { 
  3.         ILogger<HomeController> logger; 
  4.  
  5.         public HomeController(ILogger<HomeController> logger) 
  6.         { 
  7.             this.logger = logger; 
  8.         } 
  9.  
  10.         public IActionResult Index() 
  11.         { 
  12.             var miniProfiler = MiniProfiler.Current
  13.             List<Author> authors = new List<Author>(); 
  14.  
  15.             miniProfiler.RenderIncludes(this.HttpContext); 
  16.  
  17.             using (miniProfiler.Step("Get Authors")) 
  18.             { 
  19.                 authors.Add(new Author() { Id = 1, FirstName = "Joydip", LastName = "Kanjilal", Address = "Hyderabad, India" }); 
  20.                 authors.Add(new Author() { Id = 2, FirstName = "Stephen", LastName = "Smith", Address = "NY, USA" }); 
  21.                 authors.Add(new Author() { Id = 3, FirstName = "Anand", LastName = "Narayanan", Address = "Chennai, India" }); 
  22.                 authors.Add(new Author() { Id = 4, FirstName = "Steve", LastName = "Jones", Address = "London, UK" }); 
  23.             } 
  24.             return View(authors); 
  25.         } 
  26.     } 
  27.  
  28.     public class Author 
  29.     { 
  30.         public int Id { get; set; } 
  31.         public string FirstName { get; set; } 
  32.         public string LastName { get; set; } 
  33.         public string Address { get; set; } 
  34.     } 

从上面的代码中可以看到,我用 using (miniProfiler.Step("Get Authors")) 做了语句块标记,理论上 mini-profile 窗口上应该有类似 Get Authors 指标栏,接下来把程序跑起来,一起来看看效果。

除了顺向操作,你也可以指定让某些代码块不要显示在 mini-profile 中,需要做的是调用 Ignore() 即可,如下代码所示:

  1. using (MiniProfiler.Current.Ignore()) 
  2.   // Write code here that you don't 
  3.   // want MiniProfiler to profile 

使用 MiniProfile 分析 ADO.NET 查询

除了做一些常规的页面分析,还可以直接对 ADO.NET 查询性能进行分析,这就????了,要这么做的话,需要使用 ProfileDbConnection 和 ProfileDbCommand 即可,如下代码所示:

  1. public IActionResult Index() 
  2.        { 
  3.            using (SqlConnection connection = new SqlConnection(@"Data Source=.; Initial Catalog=PYZ_L; Trusted_Connection=Yes")) 
  4.            { 
  5.                using (ProfiledDbConnection profiledDbConnection = new ProfiledDbConnection(connection, MiniProfiler.Current)) 
  6.                { 
  7.                    if (profiledDbConnection.State != System.Data.ConnectionState.Open
  8.                    { 
  9.                        profiledDbConnection.Open(); 
  10.                    } 
  11.  
  12.                    using (SqlCommand command = new SqlCommand("Select * From Clothes"connection)) 
  13.                    { 
  14.                        using (ProfiledDbCommand profiledDbCommand = new ProfiledDbCommand(command, connection, MiniProfiler.Current)) 
  15.                        { 
  16.                            var data = profiledDbCommand.ExecuteReader(); 
  17.                            //Write code here to populate the list of Authors 
  18.                        } 
  19.                    } 
  20.                } 
  21.            } 
  22.  
  23.            return View(); 
  24.        } 

从上图可以看到,确实对 ADO.NET 查询有着清晰的分析,相信在帮助大家分析问题时很有帮助。

MiniProfiler 是一个可应用于 .NET, Ruby, Go 和 Node.js 的性能分析工具,你可以使用 MiniProfiler 去分析 Dapper,Linq2SQL,Entity Framework 所使用的sql的查询性能,此外 MimiProfile 之所以 Mini,意味着它介入到你的应用程序中所带来的性能开销微乎其微,所以大家可放心的丢到生产上去吧!

译文链接:https://www.infoworld.com/article/3330560/how-to-use-miniprofiler-in-aspnet-core.html

 

责任编辑:武晓燕 来源: 码农读书
相关推荐

2021-02-02 16:19:08

Serilog日志框架

2021-02-06 21:40:13

SignalR通讯TypeScript

2021-03-17 09:45:31

LazyCacheWindows

2021-03-10 09:40:43

LamarASP容器

2021-01-07 07:39:07

工具接口 Swagger

2021-01-28 22:39:35

LoggerMessa开源框架

2021-03-03 22:37:16

MediatR中介者模式

2021-01-31 22:56:50

FromServiceASP

2021-02-28 20:56:37

NCache缓存框架

2021-02-07 17:29:04

监视文件接口

2021-06-22 16:59:56

微软.NETC# 软件开发

2021-01-26 14:57:00

中间件应用模块化

2021-01-04 05:44:54

框架日志

2021-04-12 07:03:10

轻量级模块化框架

2017-10-20 08:52:11

内存缓存并发模式Linux

2022-08-01 08:00:00

开发工具跟踪侦听器

2009-02-05 14:02:46

SmtpMail发送邮件ASP.NET

2021-04-14 07:35:12

Json格式化日期

2021-11-01 14:52:38

ElasticSear索引SQL

2009-03-30 10:34:03

ASP.NETMySQL
点赞
收藏

51CTO技术栈公众号