AS.Net CoreでMiniProfilerを使用する方法


webアプリケーションのパフォーマンスは、一般的に関心のある問題であると信じています。多くのツールが、アプリケーションの性能を分析し、ボトルネックを見つけることができると信じています。スローレスポンスなどの問題。
MiniProfilerは、MiniProfilerおよびMiniProfilerにおいて利用可能であり、この記事では、MiniProfilerの使用方法を議論し、アプリケーションの性能問題を見つける。
MiniProfilerをインストールするAsp.Netを使用するには、nugtを通じてASP.Net Coreパケットを参照する必要があり、Visual Studio 2019のMiniProfilerを介して、インターフェースの実装を可視化するか、またはMiniProfiler.AspNetCore.Mvcコマンドラインツールを介して、以下のコマンドを入力することができる。

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");
    }
注入が完了すると、次にNuGet package manager拡張法を用いてRequest Pipelineパイプラインに注入する必要があります。

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
    {
      app.UseMiniProfiler();

      app.UseEndpoints(endpoints =>
      {
        endpoints.MapControllerRoute(
          name: "default",
          pattern: "{controller=Home}/{action=Index}/{id?}");
      });
    }
次に、NuGet package managerページに次の2つのコマンドを追加します。

@using StackExchange.Profiling
@addTagHelper *, MiniProfiler.AspNetCore.Mvc
最後に、UseMiniProfilerでMiniProfiler分析ウィンドウの表示すべき位置を指定したいですが、どうすればいいですか?bodyタグ内に_Layout.cshtmlタグを使用して、下記のコードで示します。

<mini-profiler position="@RenderPosition.Right" max-traces="5" />
AS.Net Core MVCにMiniProfilerを使用します。
MiniProfilerはWebPagemini-profilerを提供します。次にプログラムを走らせば、次のような性能指標図が見られます。

一部の友達が聞きたいかもしれませんが、大体の時間は分かりました。もしコードブロックを指定した実行時間だけを取得したいならば?もちろんいいです。次のコードはどうやって実現するかを示しています。

 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; }
  }
上のコードから見れば、私は で文のブロックマークをしました。理論的にはmini-profileウィンドウに のような指標欄があるはずです。次にプログラムを実行して、効果を見てみましょう。

順方向操作以外に、いくつかのコードブロックをmini-profileに表示させないように指定してもいいです。必要なのはusing (miniProfiler.Step("Get Authors"))を呼び出してもいいです。下記のコードの通りです。

using (MiniProfiler.Current.Ignore())
{
 // Write code here that you don't
 // want MiniProfiler to profile
}
MiniProfileを使ってADO.NETクエリを分析します。
いくつかの一般的なページ分析のほかに、ADO.NETクエリの性能を直接分析することもできます。🐂👃このようにするにはGet AuthorsIgnore()を使用すればいいです。

   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の性能分析ツールです。Dapper,Linq 2 SQL,Entity Fraameworkが使っているSqlの検索性能をMini Profileを使って分析してもいいです。また、マイクロオーバーヘッドに介入することを意味します。だから安心して生産に落としてください。
ここではASP.Net CoreでMiniProfilerを使用する方法についての記事を紹介します。詳細にはASP.Net CoreがMiniProfilerを使用しています。以前の記事を検索したり、下記の関連記事を閲覧したりしてください。これからもよろしくお願いします。