使用 .NET Core 中的 EventCounters 衡量性能
背景
对于每隔几毫秒发生的使用事件,最好使每个事件的衡量开销较低(小于一毫秒)。 否则,使用对性能的衡量影响将很大。 记录事件意味着你将向磁盘写入内容。使用 如果磁盘不够快,衡量你将丢失事件。使用 你需要一个解决方案,衡量而不是使用记录事件本身。
在处理大量事件时,云南idc服务商衡量了解每个事件的使用度量值也无济于事。 大多数时候,衡量你只需要一些统计信息。使用 因此,衡量你可以在进程本身中获取统计信息,使用然后偶尔编写一个事件来报告统计信息,这是 EventCounter 将执行的操作。
代码实现
下面是有关如何实现 System.Diagnostics.Tracing.EventSource 的示例。云服务器 创建名为 MinimalEventCounterSource.cs 的新文件
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Diagnostics.Tracing; namespace WebApplication42 { [EventSource(Name = "Sample.EventCounter.Minimal")] public sealed class MinimalEventCounterSource : EventSource { public static readonly MinimalEventCounterSource Log = new MinimalEventCounterSource(); private EventCounter _requestCounter; private MinimalEventCounterSource() => _requestCounter = new EventCounter("request-time", this) { DisplayName = "Request Processing Time", DisplayUnits = "ms" }; public void Request(string url, float elapsedMilliseconds) { Console.WriteLine("url:" + url + " elapsedMilliseconds:" + elapsedMilliseconds); WriteEvent(1, url, elapsedMilliseconds); _requestCounter?.WriteMetric(elapsedMilliseconds); } protected override void Dispose(bool disposing) { _requestCounter?.Dispose(); _requestCounter = null; base.Dispose(disposing); } } }添加操作筛选器,创建名为 LogRequestTimeFilterAttribute.cs 的新文件,并使用以下代码:
using Microsoft.AspNetCore.Http.Extensions; using Microsoft.AspNetCore.Mvc.Filters; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; namespace WebApplication42 { public class LogRequestTimeFilterAttribute : ActionFilterAttribute { private readonly Stopwatch _stopwatch = new Stopwatch(); public override void OnActionExecuting(ActionExecutingContext context) => _stopwatch.Start(); public override void OnActionExecuted(ActionExecutedContext context) { _stopwatch.Stop(); MinimalEventCounterSource.Log.Request( context.HttpContext.Request.GetDisplayUrl(), _stopwatch.ElapsedMilliseconds); } } }操作筛选器在请求开始时启动 Stopwatch,并在其完成后停止,捕获运行时间。 总毫秒数记录到 MinimalEventCounterSource 单一实例。 为了应用此筛选器,需要将其添加到筛选器集合。 在 Startup.cs 文件中,更新包含此筛选器的 ConfigureServices 方法。亿华云计算
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(options => options.Filters.Add<LogRequestTimeFilterAttribute>()); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApplication42", Version = "v1" }); }); }