欢迎光临
个人技术文档整理

.Net Core 性能监控-MiniProfiler的基础使用

什么是 MiniProfiler

MiniProfiler 是一个轻量级的性能分析工具,适用于 .NET Core 和其它 .NET 技术栈。它提供了一种简单的方式来测量和分析代码的性能,以及监视数据库查询。MiniProfiler 具有以下特点:

  • 轻量级:MiniProfiler 非常轻巧,不会对应用程序的性能产生显著影响。
  • 易于使用:通过包装代码块,您可以迅速测量其执行时间并检查查询统计信息。
  • 数据库支持:MiniProfiler 支持许多常见数据库,可帮助您优化数据库查询性能。

 

Nuget安装 MiniProfiler 包

Install-Package MiniProfiler.AspNetCore.Mvc

配置 MiniProfiler

public void ConfigureServices(IServiceCollection services)
{ 
    services.AddMiniProfiler(); 
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseMiniProfiler();
}

测量性能 MiniProfiler.Current.Step

  • 在您想要测量性能的代码块周围包装 MiniProfiler.Current.Step 方法
            [HttpGet]
            public string Test()
            {
                using (MiniProfiler.Current.Step("整体测试"))
                {
                    using (MiniProfiler.Current.Step("获取用户名称GetUserName"))
                    {
                        var userName = GetUserName();
                    }
    
                    using (MiniProfiler.Current.Step("其他业务逻辑1"))
                    { 
                        Thread.Sleep(200);
                    }
    
                    using (MiniProfiler.Current.Step("其他业务逻辑2"))
                    {
                        Thread.Sleep(100);
                    }
                    return "OK";
                }
    
            }
    
            private string GetUserName()
            {
                Thread.Sleep(100);
                return "admin";
            }

     

查看分析结果

  • 最近的分析器列表 
    https://localhost:7187/mini-profiler-resources/results-index

     

  • 查看当前用户的最后一个分析器或特定的分析器
    http://localhost:port/mini-profiler-resources/results

参考

https://miniprofiler.com/dotnet/AspDotNetCore

 

赞(2)