Web应用程序的性能是世界范围内的一个严重问题。 开发人员可以使用许多工具来分析Web应用程序并查找性能瓶颈。 MiniProfiler就是这样一种工具,它是用于配置Web应用程序的简单但功能强大的工具。 MiniProfiler可帮助您检测运行缓慢的查询,延迟的服务器响应时间等。
MiniProfiler可用于.Net,ASP.Net和ASP.Net Core。 您可以在GitHub上找到MiniProfiler的文档。 本文讨论了MiniProfiler,为什么有用,以及如何使用它来分析ASP.Net Core MVC应用程序并发现应用程序中的性能问题。
在Visual Studio 2017中创建ASP.Net Core MVC项目
首先,让我们在Visual Studio中创建一个ASP.Net Core MVC项目。 如果您的系统已启动并运行Visual Studio 2017,请按照以下给定的步骤创建一个ASP.Net Core MVC项目。
- 启动Visual Studio 2017 IDE。
- 单击文件>新建>项目。
- 从显示的模板列表中选择“ ASP.Net Core Web应用程序(.Net Core)”。
- 指定项目的名称。
- 单击确定保存项目。
- 将显示一个新窗口“ New .Net Core Web Application…”。
- 选择.Net Core作为运行时,并从顶部的下拉列表中选择ASP.Net Core 2.1(或更高版本)。 我正在使用.Net Core 2.2。
- 选择“ Web应用程序(Model-View-Controller)”作为项目模板(如下图1所示)。
- 确保未选中“启用Docker支持”和“配置HTTPS”复选框。 我们不会在这里使用这些功能。
- 确保选择“无身份验证”。 我们也不会在这里使用身份验证。
- 单击确定。
按照这些步骤将在Visual Studio中创建一个新的ASP.Net Core MVC项目。 我们将使用该项目通过MiniProfiler来分析应用程序。
在ASP.Net Core中安装和配置MiniProfiler
要开始使用MiniProfiler,您需要安装必要的NuGet软件包。 要在您的项目中安装MiniProfiler,请按照以下步骤操作。
- 在“解决方案资源管理器”窗口中选择项目。
- 右键单击并选择“管理NuGet软件包...”
- 搜索“ MiniProfiler.AspNetCore.Mvc”包。
- 单击“安装”以安装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窗口。
要了解代码的特定部分执行所花费的时间,可以利用以下步骤。 以下代码段说明了如何实现此目的。
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所示。 我以绿色突出显示的条目显示了执行“获取作者”步骤所需的时间。
如果要从性能分析中忽略应用程序代码的特定部分,则可以指定要忽略的代码,如下面的代码片段所示。
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