如何在ASP.Net Core中使用MiniProfiler

Web应用程序的性能是世界范围内的一个严重问题。 开发人员可以使用许多工具来分析Web应用程序并查找性能瓶颈。 MiniProfiler就是这样一种工具,它是用于配置Web应用程序的简单但功能强大的工具。 MiniProfiler可帮助您检测运行缓慢的查询,延迟的服务器响应时间等。

MiniProfiler可用于.Net,ASP.Net和ASP.Net Core。 您可以在GitHub上找到MiniProfiler的文档。 本文讨论了MiniProfiler,为什么有用,以及如何使用它来分析ASP.Net Core MVC应用程序并发现应用程序中的性能问题。

[.Net路线图: .Net Standard 2.1中可以期待新功能 | .Net Framework或.Net Core? 了解何时使用哪个 | 通过InfoWorld的App Dev Report新闻通讯了解编程方面的热门话题。 ]

在Visual Studio 2017中创建ASP.Net Core MVC项目

首先,让我们在Visual Studio中创建一个ASP.Net Core MVC项目。 如果您的系统已启动并运行Visual Studio 2017,请按照以下给定的步骤创建一个ASP.Net Core MVC项目。

  1. 启动Visual Studio 2017 IDE。
  2. 单击文件>新建>项目。
  3. 从显示的模板列表中选择“ ASP.Net Core Web应用程序(.Net Core)”。
  4. 指定项目的名称。
  5. 单击确定保存项目。
  6. 将显示一个新窗口“ New .Net Core Web Application…”。
  7. 选择.Net Core作为运行时,并从顶部的下拉列表中选择ASP.Net Core 2.1(或更高版本)。 我正在使用.Net Core 2.2。
  8. 选择“ Web应用程序(Model-View-Controller)”作为项目模板(如下图1所示)。
  9. 确保未选中“启用Docker支持”和“配置HTTPS”复选框。 我们不会在这里使用这些功能。
  10. 确保选择“无身份验证”。 我们也不会在这里使用身份验证。
  11. 单击确定。

按照这些步骤将在Visual Studio中创建一个新的ASP.Net Core MVC项目。 我们将使用该项目通过MiniProfiler来分析应用程序。

miniprofiler 1 IDG

图1:在Visual Studio 2017中创建一个新的ASP.Net Core MVC项目。

在ASP.Net Core中安装和配置MiniProfiler

要开始使用MiniProfiler,您需要安装必要的NuGet软件包。 要在您的项目中安装MiniProfiler,请按照以下步骤操作。

  1. 在“解决方案资源管理器”窗口中选择项目。
  2. 右键单击并选择“管理NuGet软件包...”
  3. 搜索“ MiniProfiler.AspNetCore.Mvc”包。
  4. 单击“安装”以安装NuGet软件包。

这将在您的项目中安装MiniProfiler.AspNetCore.Mvc NuGet包。 要在项目中开始使用MiniProfiler,您需要在Startup类中对其进行配置。 以下代码段显示了如何在IServiceCollection实例上调用AddMiniProfiler方法,以将MiniProfiler添加到管道中。

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMiniProfiler(options =>
            options.RouteBasePath = "/profiler"
            );
            //Usual code
        }

您可以从MiniProfiler网站上了解有关在管道中注册MiniProfiler时指定的选项的更多信息。

您还应该在IApplicationBuilder实例上调用UseMiniProfiler方法,以开始在控制器和视图中使用MiniProfiler。

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
       app.UseMiniProfiler();
       //Usual code
    }

接下来,在_Layout.cshtml文件的<html>标记内添加以下两行。

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

您还应该指定MiniProfiler窗口在网页中的显示位置,即渲染位置。 为此,可以在<body>标记内包含以下语句。

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

使用MiniProfiler中的步骤分析ASP.Net Core MVC代码

MiniProfiler将让您知道页面加载时间以及与数据库查询性能有关的信息。 当您运行该应用程序时,输出将如下图2所示。 请注意屏幕右上角的MiniProfiler窗口。

miniprofiler 2 IDG

图2:运行中的MiniProfiler。

要了解代码的特定部分执行所花费的时间,可以利用以下步骤。 以下代码段说明了如何实现此目的。

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);
 }

下面的代码片段显示了上述Author类的外观。

public class Author
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
    }

运行应用程序时,您将观察到我们定义的步骤所花费的时间,如下图3所示。 我以绿色突出显示的条目显示了执行“获取作者”步骤所需的时间。

miniprofiler 3 IDG

图3:在MiniProfiler中执行步骤所花费的时间。

如果要从性能分析中忽略应用程序代码的特定部分,则可以指定要忽略的代码,如下面的代码片段所示。

using (MiniProfiler.Current.Ignore())
{
  // Write code here that you don't
  // want MiniProfiler to profile
}

使用MiniProfiler来分析ADO.Net查询

您也可以使用MiniProfiler来分析ADO.Net查询。 为此,您需要利用ProfileDbConnection和ProfileDbCommand,如下面的代码片段所示。

using (SqlConnection connection = new SqlConnection(@"Data Source=JOYDIP\SQLEXPRESS; Initial Catalog=SyncDB; 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 Authors", connection))
               {
                 using (ProfiledDbCommand profiledDbCommand =
                   new ProfiledDbCommand(command, connection,
                     MiniProfiler.Current))
                       {                               
                         var data =
                          profiledDbCommand.ExecuteReader();
              //Write code here to populate the list of Authors
                        }
                 }
          }                      
    }

请注意ProfileDbConnection和ProfileDbCommand如何包装DbConnection和DbCommand对象。 您可以从MiniProfiler网站上了解有关如何使用MiniProfiler剖析源代码的更多信息。

MiniProfiler是用于.Net,Ruby,Go和Node.js的简单分析器。 您可以使用MiniProfiler来分析由Dapper,Linq2SQL和Entity Framework生成的查询。 除了易于使用之外,MiniProfiler不会为您的应用程序增加太多开销。 您可以使用MiniProfiler来配置生产中的应用程序,而不会对性能产生重大影响。

From: https://www.infoworld.com/article/3330560/how-to-use-miniprofiler-in-aspnet-core.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值