9-使用 MiniProfiler 监控运行效率

2018-09-09  本文已影响294人  俊果果

零、序言

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


            // 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();
@using StackExchange.Profiling
@addTagHelper *, MiniProfiler.AspNetCore.Mvc
<mini-profiler />

三、运行

image.png

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

            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


            services.AddMiniProfiler().AddEntityFramework();

五、代码commit

github commit 点此查看

上一篇下一篇

猜你喜欢

热点阅读