自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

背景

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

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

代碼實現(xiàn)

下面是有關如何實現(xiàn) System.Diagnostics.Tracing.EventSource 的示例。 創(chuàng)建名為 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.     } 

添加操作篩選器,創(chuàng)建名為 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,并在其完成后停止,捕獲運行時間。 總毫秒數(shù)記錄到 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.         } 

 

  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 

 

責任編輯:武曉燕 來源: UP技術控
相關推薦

2024-11-12 07:28:39

2024-03-14 11:57:53

.NET Core反射開發(fā)

2024-06-11 09:00:00

異步編程代碼

2024-09-30 09:48:41

RabbitMQ消息中間件

2024-09-10 08:13:16

Asp項目輕量級

2024-10-09 07:40:43

2021-11-11 16:46:02

CPU使用率 .NET

2019-08-12 08:00:00

ASP.NetASP.Net Cor編程語言

2021-02-19 06:54:33

配置系統(tǒng)ASP.NET Cor

2019-11-08 08:00:00

ASP .NETASP .NET Cocookie

2024-11-27 00:24:04

2024-12-05 08:14:41

2024-05-31 13:07:29

.NET Core定時任務編程

2024-11-22 10:20:04

IP.NET

2025-03-06 02:00:00

.NETGrafana工具

2017-10-10 12:17:55

HTTPGo.NET Core

2023-11-16 08:34:23

.NETORM框架

2015-08-21 10:36:32

.NETRedis

2024-03-22 08:11:20

.NETJSON數(shù)據(jù)序列化

2021-06-30 08:00:00

代碼DI開發(fā)
點贊
收藏

51CTO技術棧公眾號