激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

香港云服务器
服務器之家 - 編程語言 - ASP.NET教程 - 使用 .NET Core 中的 EventCounters 衡量性能

使用 .NET Core 中的 EventCounters 衡量性能

2022-01-07 22:12UP技術控conan5566 ASP.NET教程

對于每隔幾毫秒發生的事件,最好使每個事件的開銷較低(小于一毫秒)。 否則,對性能的影響將很大。 記錄事件意味著你將向磁盤寫入內容。 如果磁盤不夠快,你將丟失事件。 你需要一個解決方案,而不是記錄事件本身。

使用 .NET Core 中的 EventCounters 衡量性能

背景

對于每隔幾毫秒發生的事件,最好使每個事件的開銷較低(小于一毫秒)。 否則,對性能的影響將很大。 記錄事件意味著你將向磁盤寫入內容。 如果磁盤不夠快,你將丟失事件。 你需要一個解決方案,而不是記錄事件本身。

在處理大量事件時,了解每個事件的度量值也無濟于事。 大多數時候,你只需要一些統計信息。 因此,你可以在進程本身中獲取統計信息,然后偶爾編寫一個事件來報告統計信息,這是 EventCounter 將執行的操作。

代碼實現

下面是有關如何實現 System.Diagnostics.Tracing.EventSource 的示例。 創建名為 MinimalEventCounterSource.cs 的新文件

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Threading.Tasks; 
  5. using System.Diagnostics.Tracing; 
  6.  
  7. namespace WebApplication42 
  8.     [EventSource(Name = "Sample.EventCounter.Minimal")] 
  9.     public sealed class MinimalEventCounterSource : EventSource 
  10.     { 
  11.         public static readonly MinimalEventCounterSource Log = new MinimalEventCounterSource(); 
  12.  
  13.         private EventCounter _requestCounter; 
  14.  
  15.         private MinimalEventCounterSource() => 
  16.             _requestCounter = new EventCounter("request-time", this) 
  17.             { 
  18.                 DisplayName = "Request Processing Time"
  19.                 DisplayUnits = "ms" 
  20.             }; 
  21.  
  22.         public void Request(string url, float elapsedMilliseconds) 
  23.         { 
  24.             Console.WriteLine("url:" + url + "  elapsedMilliseconds:" + elapsedMilliseconds); 
  25.             WriteEvent(1, url, elapsedMilliseconds); 
  26.             _requestCounter?.WriteMetric(elapsedMilliseconds); 
  27.         } 
  28.  
  29.         protected override void Dispose(bool disposing) 
  30.         { 
  31.             _requestCounter?.Dispose(); 
  32.             _requestCounter = null
  33.  
  34.             base.Dispose(disposing); 
  35.         } 
  36.     } 

添加操作篩選器,創建名為 LogRequestTimeFilterAttribute.cs 的新文件,并使用以下代碼:

  1. using Microsoft.AspNetCore.Http.Extensions; 
  2. using Microsoft.AspNetCore.Mvc.Filters; 
  3. using System; 
  4. using System.Collections.Generic; 
  5. using System.Diagnostics; 
  6. using System.Linq; 
  7. using System.Threading.Tasks; 
  8.  
  9. namespace WebApplication42 
  10.     public class LogRequestTimeFilterAttribute : ActionFilterAttribute 
  11.     { 
  12.         private readonly Stopwatch _stopwatch = new Stopwatch(); 
  13.  
  14.         public override void OnActionExecuting(ActionExecutingContext context) => _stopwatch.Start(); 
  15.  
  16.         public override void OnActionExecuted(ActionExecutedContext context) 
  17.         { 
  18.             _stopwatch.Stop(); 
  19.  
  20.             MinimalEventCounterSource.Log.Request( 
  21.                 context.HttpContext.Request.GetDisplayUrl(), _stopwatch.ElapsedMilliseconds); 
  22.         } 
  23.     } 

操作篩選器在請求開始時啟動 Stopwatch,并在其完成后停止,捕獲運行時間。 總毫秒數記錄到 MinimalEventCounterSource 單一實例。 為了應用此篩選器,需要將其添加到篩選器集合。 在 Startup.cs 文件中,更新包含此篩選器的 ConfigureServices 方法。

  1. // This method gets called by the runtime. Use this method to add services to the container. 
  2.         public void ConfigureServices(IServiceCollection services) 
  3.         { 
  4.             services.AddControllers(options => options.Filters.Add<LogRequestTimeFilterAttribute>()); 
  5.             services.AddSwaggerGen(c => 
  6.             { 
  7.                 c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApplication42", Version = "v1" }); 
  8.             }); 
  9.         } 

使用 .NET Core 中的 EventCounters 衡量性能

使用 .NET Core 中的 EventCounters 衡量性能

  1. url:https://localhost:5008/WeatherForecast elapsedMilliseconds:70 
  2.  
  3. url:https://localhost:5008/WeatherForecast elapsedMilliseconds:19 
  4.  
  5. url:https://localhost:5008/WeatherForecast elapsedMilliseconds:18 
  6.  
  7. url:https://localhost:5008/WeatherForecast elapsedMilliseconds:19 
  8.  
  9. url:https://localhost:5008/WeatherForecast elapsedMilliseconds:22 
  10.  
  11. url:https://localhost:5008/WeatherForecast elapsedMilliseconds:17 
  12.  
  13. url:https://localhost:5008/WeatherForecast elapsedMilliseconds:17 

原文鏈接:https://mp.weixin.qq.com/s/rRYKxa1iHLKCKwTH2SK4Mg

延伸 · 閱讀

精彩推薦
1282
主站蜘蛛池模板: 免费在线观看午夜视频 | 日本免费a∨ | 亚洲第一成av人网站懂色 | 99精品视频一区二区三区 | 91久久久久久久久久久久久 | 久久综合给合久久狠狠狠97色69 | 成人h精品动漫一区二区三区 | 99视频在线观看视频 | 亚洲精品7777xxxx青睐 | 天天夜夜操操 | 免费一级欧美在线观看视频 | 久久免费毛片 | 精品亚洲夜色av98在线观看 | 好吊色欧美一区二区三区四区 | 中文在线日韩 | 久久精品视频首页 | 韩国一大片a毛片 | 日本一区免费看 | 欧美人与禽性xxxxx杂性 | 精品一区二区三区免费毛片 | 色婷婷久久久久久 | 天天黄色片 | 特级无码毛片免费视频尤物 | 黄色av电影在线 | 一级电影免费在线观看 | 日韩欧美中文字幕视频 | 久久精品成人免费国产片桃视频 | 国产精品成年片在线观看, 激情小说另类 | 色吧综合网 | 黄色网址免费入口 | 日韩欧美电影一区二区三区 | 亚洲91精品 | 国产免费永久在线观看 | 91成人天堂久久成人 | 精精国产xxxx视频在线播放7 | 国产羞羞视频在线观看免费应用 | 神马久久精品综合 | 欧美人xx| 91热久久免费频精品黑人99 | 久久精品网 | 巨根插入|