做为一个开发人员,你知道如何分析本身开发的Api性能么?html
在Visual Studio和Azure中, 咱们可使用Application Insight来监控项目。除此以外咱们还可使用一个免费工具Stackify Prefix,它容许追踪全部的Http请求, 这里有一篇博客讲解了如何使用Stackify Prefix(Scalable and Performant ASP.NET Core Web APIs: Profiling and Monitoring)。git
本文我将引入另一个工具MiniProfiler, 我将讲解如何将MiniProfiler集成到ASP.NET Core WebAPI中。github
与Stackify Prefix类似,MiniProfiler也是一款免费的工具(官网地址:https://miniprofiler.com/dotnet/),你可使用它精确的分析ASP.NET和ASP.NET Core应用程序的任何代码。web
Tips: MiniProfiler在ASP.NET和控制台程序中也可使用哦。数据库
咱们可使用Nuget来下载这个包。json
PM> Install-Package MiniProfiler.AspNetCore.Mvc
Startup.cs
MiniProfiler配置起来很简单,只须要如下几步c#
ConfigureServices
方法中添加MiniProfiler服务public void ConfigureServices(IServiceCollection services) { services.AddMiniProfiler(options => options.RouteBasePath = "/profiler" ); }
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseMiniProfiler(); }
public class ValueController : ControllerBase { [HttpGet] public IEnumerable<string> Get() { string url1 = string.Empty; string url2 = string.Empty; using (MiniProfiler.Current.Step("Get方法")) { using (MiniProfiler.Current.Step("准备数据")) { using (MiniProfiler.Current.CustomTiming("SQL", "SELECT * FROM Config")) { // 模拟一个SQL查询 Thread.Sleep(500); url1 = "https://www.baidu.com"; url2 = "https://www.sina.com.cn/"; } } using (MiniProfiler.Current.Step("使用从数据库中查询的数据,进行Http请求")) { using (MiniProfiler.Current.CustomTiming("HTTP", "GET " + url1)) { var client = new WebClient(); var reply = client.DownloadString(url1); } using (MiniProfiler.Current.CustomTiming("HTTP", "GET " + url2)) { var client = new WebClient(); var reply = client.DownloadString(url2); } } } return new string[] { "value1", "value2" }; } }
代码解释:api
MiniProfiler.Current.Step
方法定义了分析的步骤,这个方法能够接受一个String
类型的参数,它会显示在最终的报告中MiniProfiler.Current.CustomTiming
方法是更细粒度的对报告内容进行分类,以上代码中定义了2种分类,一种是SQL, 一种是HttpWebClient
来分别请求网站的Url下面咱们启动项目, 项目默认打开/api/valuesapp
而后咱们来访问如下/profiler/results, 就会出现以下页面dom
如上图所示,咱们能够很清楚的看到代码中每一部分的耗时,因为咱们添加了2种分类SQL和Http,因此列表中会对2种分类进行汇总。
重点: 当前页面只会显示最近的一次请求
从当前报告中能够获得如下结论
这里咱们就再也不讲如何在ASP.NET Core中整合Swagger。
MiniProfiler和Swagger是能够集成在一块儿的,为了完成这个功能,咱们须要进行如下几步
默认的index.html页面能够从以下连接下载
下载以后将这个文件放置到项目根目录下。
接下来咱们须要在这个文件的头部加入以下脚本代码:
<script async="async" id="mini-profiler" src="/profiler/includes.min.js?v=4.0.138+gcc91adf599" data-version="4.0.138+gcc91adf599" data-path="/profiler/" data-current-id="4ec7c742-49d4-4eaf-8281-3c1e0efa748a" data-ids="" data-position="Left" 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>
最后咱们须要配置这个index.html文件的Bulid Action为Embedded resource
在Startup.cs
文件中,咱们须要修改UseSwaggerUI
中间件的配置,这里咱们须要添加一个InputStream
配置。
app.UseSwaggerUI(c => { c.RoutePrefix = "swagger"; c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); c.IndexStream = () => GetType().GetTypeInfo().Assembly.GetManifestResourceStream("MiniProfilerSample.index.html"); });
注意:这里
MiniProfilerSample
是项目的命名空间名
从新启动项目,Swagger文档页面的左上角就出现了一个小的面板,当模拟请求一个链接以后,它就会显示出当前请求的分析数据,看起来是否是很酷炫。
本篇博客描述了如何使用MiniProfiler来监控分析你的Api。 MiniProfiler除了提供网页显示报告,还支持将报告结果存储在数据库中,后面我会补充一篇文章来讲明如何将报告保存到数据库中。