美文网首页
9-使用 MiniProfiler 监控运行效率

9-使用 MiniProfiler 监控运行效率

作者: 俊果果 | 来源:发表于2018-09-09 11:04 被阅读294次

    零、序言

    • 1、介绍

    MiniProfiler is a library and UI for profiling your application. By letting you see where you time is spent, which queries are run, and any other custom timings you want to add, MiniProfiler helps you debug issues and optimize performance.

    MiniProfiler 可以基于action(request)记录每个阶段的耗时,并且支持coding以自定义step信息输出。支持.net core

    一、项目文件

    使用 OAuth2 的项目:github 地址

    二、修改 Client

    • 1、Nuget 安装包MiniProfiler.AspNetCore.Mvc
    • 2、Startup类的方法ConfigureServices添加代码:
    
                // Note .AddMiniProfiler() returns a IMiniProfilerBuilder for easy intellisense
                services.AddMiniProfiler(options =>
                {
                    // All of this is optional. You can simply call .AddMiniProfiler() for all defaults
    
                    // (Optional) Path to use for profiler URLs, default is /mini-profiler-resources
                    options.RouteBasePath = "/profiler";
    
                    // (Optional) Control storage
                    // (default is 30 minutes in MemoryCacheStorage)
                    (options.Storage as MemoryCacheStorage).CacheDuration = TimeSpan.FromMinutes(60);
    
                    // (Optional) Control which SQL formatter to use, InlineFormatter is the default
                    options.SqlFormatter = new StackExchange.Profiling.SqlFormatters.InlineFormatter();
    
                    // (Optional) To control authorization, you can use the Func<HttpRequest, bool> options:
                    // (default is everyone can access profilers)
                    //options.ResultsAuthorize = request => MyGetUserFunction(request).CanSeeMiniProfiler;
                    //options.ResultsListAuthorize = request => MyGetUserFunction(request).CanSeeMiniProfiler;
    
                    // (Optional)  To control which requests are profiled, use the Func<HttpRequest, bool> option:
                    // (default is everything should be profiled)
                    //options.ShouldProfile = request => MyShouldThisBeProfiledFunction(request);
    
                    // (Optional) Profiles are stored under a user ID, function to get it:
                    // (default is null, since above methods don't use it by default)
                    //options.UserIdProvider = request => MyGetUserIdFunction(request);
    
                    // (Optional) Swap out the entire profiler provider, if you want
                    // (default handles async and works fine for almost all appliations)
                    // options.ProfilerProvider = new MyProfilerProvider();
    
                    // (Optional) You can disable "Connection Open()", "Connection Close()" (and async variant) tracking.
                    // (defaults to true, and connection opening/closing is tracked)
                    options.TrackConnectionOpenClose = true;
                });
    

    Configure方法添加代码

    
                // ...existing configuration...  需要将此句放在 useMVC 前
                app.UseMiniProfiler();
    
    • 3、修改 ViewImports.cshtml,添加 tagHelper
    @using StackExchange.Profiling
    @addTagHelper *, MiniProfiler.AspNetCore.Mvc
    
    • 4、_Layout.cshtml 添加 miniprofiler 的使用
    <mini-profiler />
    

    三、运行

    image.png

    界面左上角会出现一个访问列表,点击可以查看每次访问的具体耗时情况

    • 在controller 中修改代码为如下:
                var profiler = MiniProfiler.Current;
                var url = "api/images";
                HttpResponseMessage response;
                using (profiler.Step("HTTP GET " + url))
                {
                    response = await httpClient.GetAsync(url).ConfigureAwait(false);
                } 
    

    再次运行,可以看到如下信息:


    image.png

    四、监控 ef

    • 首先修改Client solution, 添加 Dbcontext、Models、Repository
    • Nuget安装packageMiniProfiler.EntityFrameworkCore
    • Startup类的方法ConfigureServices添加代码:
    
                services.AddMiniProfiler().AddEntityFramework();
    
    • 然后在 ControllerIndex 方法中添加对 repository 方法的调用
    • 运行


      image.png

      可以看到sql语句的耗时,点击sql列,查看详细信息如下:


      image.png

    五、代码commit

    github commit 点此查看

    相关文章

      网友评论

          本文标题:9-使用 MiniProfiler 监控运行效率

          本文链接:https://www.haomeiwen.com/subject/ogvrgftx.html