MiniProfiler性能分析工具— .Net Core中用法
前言:
在日常开发中,应用程序的性能是我们需要关注的一个重点问题。当然我们有很多工具来分析程序性能:如:Zipkin等;但这些过于复杂,需要单独搭建。
MiniProfiler就是一款简单,但功能强大的应用新能分析工具;可以帮助我们定位:SQL性能问题、响应慢等问题。
本篇文章将介绍MiniProfiler在Asp.Net Core中如何使用
一、MiniProfiler介绍
MiniProfiler是一款针对.NET, Ruby, Go and Node.js的性能分析的轻量级程序。可以对一个页面本身,及该页面通过直接引用、Ajax、Iframe形式访问的其它页面进行监控,监控内容包括数据库内容,并可以显示数据库访问的SQL(支持EF、EF CodeFirst等 )。并且以很友好的方式展现在页面上。
MiniProfiler官网:http://miniprofiler.com/
MiniProfiler的一个特别有用的功能是它与数据库框架的集成。除了.NET原生的 DbConnection类,MiniProfiler还内置了对实体框架(Entity Framework)以及LINQ to SQL、RavenDb和MongoDB的支持。任何执行的Step都会包括当时查询的次数和所花费的时间。为了检测常见的错误,如N+1反模式,profiler将检测仅有参数值存在差异的多个查询。
二、MiniProfiler用法
1、Nuget包安装:
//Mvc
Install-Package MiniProfiler.AspNetCore.Mvc
//EF分析添加
Install-Package MiniProfiler.EntityFrameworkCore
2、配置MiniProfiler:修改Startup.cs
a) 注入MiniProfiler
public void ConfigureServices(IServiceCollection services)
{
// ...其他配置... // 注入MiniProfiler
services.AddMiniProfiler(options =>
{
//访问地址路由根目录;默认为:/mini-profiler-resources
options.RouteBasePath = "/profiler";
//数据缓存时间
(options.Storage as MemoryCacheStorage).CacheDuration = TimeSpan.FromMinutes(60);
//sql格式化设置
options.SqlFormatter = new StackExchange.Profiling.SqlFormatters.InlineFormatter();
//跟踪连接打开关闭
options.TrackConnectionOpenClose = true;
//界面主题颜色方案;默认浅色
options.ColorScheme = StackExchange.Profiling.ColorScheme.Dark;
//.net core 3.0以上:对MVC过滤器进行分析
options.EnableMvcFilterProfiling = true;
//对视图进行分析
options.EnableMvcViewProfiling = true; //控制访问页面授权,默认所有人都能访问
//options.ResultsAuthorize;
//要控制分析哪些请求,默认说有请求都分析
//options.ShouldProfile; //内部异常处理
//options.OnInternalError = e => MyExceptionLogger(e);
})
// AddEntityFramework是要监控EntityFrameworkCore生成的SQL
.AddEntityFramework();
}
b) 启用MiniProfiler
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IMemoryCache cache)
{
// ...其他配置 //该方法必须在app.UseEndpoints以前
app.UseMiniProfiler(); app.UseEndpoints(routes =>
{
// ...
});
}
c) MVC项目:
修改 _ViewImports.cshtml
@using StackExchange.Profiling
@addTagHelper *, MiniProfiler.AspNetCore.Mvc
将MiniProfiler添加到布局文件(Shared/_Layout.cshtml)中
<mini-profiler />
d) 运行效果:


三、 Swagger UI接入MiniProfiler
使用步骤和前面大体一样
1、下载Swagger页面:
请先在Github中下载对应版本的swagger页面:https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/src/Swashbuckle.AspNetCore.SwaggerUI/index.html
2、添加到项目中,并设置index.html为:内嵌资源

3、修改UseSwaggerUI中间件的配置
app.UseSwaggerUI(c =>
{
//AuditLogDemo项目命名空间
c.SwaggerEndpoint("/swagger/v1/swagger.json", "AuditLogDemo API V1");
c.IndexStream = () => GetType().GetTypeInfo().Assembly.GetManifestResourceStream("AuditLogDemo.wwwroot.index.html");
});
4、获取MiniProfiler的html代码片段
/// <summary>
/// 获取html片段
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("GetHtml")]
public IActionResult GetHtml()
{
var html = MiniProfiler.Current.RenderIncludes(HttpContext);
return Ok(html.Value);
}

5、在Swagger的Html中添加获取的MiniProfiler片段
<!-- HTML for static distribution bundle build -->
<script async="async" id="mini-profiler" src="/profiler/includes.min.js?v=4.2.22+4563a9e1ab"
data-version="4.2.22+4563a9e1ab" data-path="/profiler/"
data-current-id="0601948b-d995-4a86-9cae-33d73ecd2f59"
data-ids="0601948b-d995-4a86-9cae-33d73ecd2f59"
data-position="Left"
data-scheme="Dark"
data-authorized="true"
data-max-traces="15"
data-toggle-shortcut="Alt+P"
data-trivial-milliseconds="2.0"
data-ignored-duplicate-execute-types="Open,OpenAsync,Close,CloseAsync"></script> <!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>%(DocumentTitle)</title>
……
6、调用效果:


如上图可以查看到所有请求路径及Sql操作耗时,那么如果需要监控指定代码块耗时如何实现呢
四、自定义标记:
1、添加标记代码:
var miniPro = MiniProfiler.Current;
using (miniPro.Step("Add AuditLog"))
{
//保存审计日志
await _auditLogService.SaveAsync(auditInfo);
}

2、取消监控方式:
using(MiniProfiler.Current.Ignore())
{
//代码
}
3、当然MiniProfiler还有很多其他功能等待解锁:如监控ADO.NET执行耗时,需要使用:ProfileDBConnection 和 ProfileDBCommand对象:
总结:
1、MiniProfiler使用非常简单
2、功能满足日常中程序性能优化相关问题
其他:
MiniProfiler的监控列表地址:http://{xxx}/profiler/results-index
MiniProfiler性能分析工具— .Net Core中用法的更多相关文章
- sar命令,linux中最为全面的性能分析工具之一
sar是System Activity Reporter(系统活动情况报告)的缩写.这个工具所需要的负载很小,也是目前linux中最为全面的性能分析工具之一.此款工具将对系统当前的状态就行取样,然后通 ...
- Linux性能分析工具的安装和使用
转自:http://blog.chinaunix.net/uid-26488891-id-3118279.html Normal 0 7.8 磅 0 2 false false false EN-US ...
- 系统级性能分析工具perf的介绍与使用[转]
测试环境:Ubuntu16.04(在VMWare虚拟机使用perf top存在无法显示问题) Kernel:3.13.0-32 系统级性能优化通常包括两个阶段:性能剖析(performance pro ...
- valgrind和Kcachegrind性能分析工具详解
一.valgrind介绍 valgrind是运行在Linux上的一套基于仿真技术的程序调试和分析工具,用于构建动态分析工具的装备性框架.它包括一个工具集,每个工具执行某种类型的调试.分析或类似的任务, ...
- 系统级性能分析工具perf的介绍与使用
测试环境:Ubuntu16.04(在VMWare虚拟机使用perf top存在无法显示问题) Kernel:3.13.0-32 系统级性能优化通常包括两个阶段:性能剖析(performance pro ...
- Linux 性能分析工具汇总合集
出于对Linux操作系统的兴趣,以及对底层知识的强烈欲望,因此整理了这篇文章.本文也可以作为检验基础知识的指标,另外文章涵盖了一个系统的方方面面.如果没有完善的计算机系统知识,网络知识和操作系统知识, ...
- [转]Linux性能分析工具汇总合集
出于对Linux操作系统的兴趣,以及对底层知识的强烈欲望,因此整理了这篇文章.本文也可以作为检验基础知识的指标,另外文章涵盖了一个系统的方方面面.如果没有完善的计算机系统知识,网络知识和操作系统知识, ...
- 超全整理!Linux性能分析工具汇总合集
转自:http://rdc.hundsun.com/portal/article/731.html?ref=myread 出于对Linux操作系统的兴趣,以及对底层知识的强烈欲望,因此整理了这篇文章. ...
- Android 常用的性能分析工具详解:GPU呈现模式, TraceView, Systrace, HirearchyViewer(转)
此篇将重点介绍几种常用的Android性能分析工具: 一.Logcat 日志 选取Tag=ActivityManager,可以粗略地知道界面Displaying的时间消耗.当我们打开一个Activit ...
随机推荐
- Django(投票程序)
Django是一个web框架,python编写的. MTV模式 Django的MTV模式本质上和MVC是一样的,也是为了各组件间保持松耦合关系,只是定义上有些许不同 -M代表模型(Model ):负责 ...
- yolov5实战之皮卡丘检测
前言 从接触深度学习开始一直都做的是人脸识别,基本上也一直都在用mxnet. 记得之前在刚接触的时候看到博客中写到,深度学习分三个层次,第一个层次是分类,第二个层次是检测,第三个层次是分割.人脸识别算 ...
- LeetCode448-数组中消失的数字
题目 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您能 ...
- Ansible User 模块添加单用户并ssh-key复制
Ansible User 模块添加单用户并ssh-key复制 1 Ansible 版本: ansible 2.9.6 config file = /etc/ansible/ansible.cfg co ...
- 【Linux】快速创建文件的命令方法
[root@centos7 dir1]# ll total 0 -rw-r--r-- 1 root root 0 Aug 15 02:39 file1 -rw-r--r-- 1 root root 0 ...
- 【Linux】ssh互信脚本
使用互信脚本的时候,需要敲回车,但是shell脚本无法满足,所以需要用到expect脚本 rpm -qa | grep expect 如果没有的话,直接用yum安装即可 yum install exp ...
- 转发:[服务器]SSL安装证书教程
[服务器]SSL安装证书教程 来自阿里云教程 Tomcat服务器安装SSL证书 安装PFX格式证书 https://help.aliyun.com/document_detail/98576.ht ...
- MongoDB查询优化--explain,慢日志
引入 与Mysql数据库一样,MongoDB也有自己的查询优化工具,explain和慢日志 explain shell命令格式 db.collection.explain().<method(. ...
- bean与map之间的转化
import java.util.HashMap; import java.util.Map; import org.apache.commons.beanutils.BeanUtils; impor ...
- jmeter跳过验证码登录配置:通过手动添加 Cookie 跳过带验证码的登录接口
目录 一.基本配置 二.HTTP请求默认值 三.HTTP信息头管理器 四.HTTP Cookie管理器 五.线程组下接口设置 一.基本配置 二.HTTP请求默认值 (1)jmeter的设置: (2)设 ...