您现在的位置是:亿华云 > 人工智能
使用 .NET Core 中的 EventCounters 衡量性能
亿华云2025-10-09 07:02:11【人工智能】1人已围观
简介背景对于每隔几毫秒发生的事件,最好使每个事件的开销较低(小于一毫秒)。 否则,对性能的影响将很大。 记录事件意味着你将向磁盘写入内容。 如果磁盘不够快,你将丢失事件。 你需要一个解决方案,而不是记录事
背景
对于每隔几毫秒发生的使用事件,最好使每个事件的衡量开销较低(小于一毫秒)。 否则,使用对性能的衡量影响将很大。 记录事件意味着你将向磁盘写入内容。使用 如果磁盘不够快,衡量你将丢失事件。使用 你需要一个解决方案,衡量而不是使用记录事件本身。
在处理大量事件时,站群服务器衡量了解每个事件的使用度量值也无济于事。 大多数时候,衡量你只需要一些统计信息。使用 因此,衡量你可以在进程本身中获取统计信息,使用然后偶尔编写一个事件来报告统计信息,这是 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" }); }); }很赞哦!(96)
相关文章
- 域后缀首选.com,.net,然后是.cn。后缀选择不当,导致流量损失。域名是企业与互联网网址之间的链接,关键是企业在网络上存在的标志。因此,选择好域名是开展网上工作的首要重要条件。
- 我为什么不用gitlab做私有Git服务器
- AMD推出AMD Alveo™ V80 计算加速卡:专为内存密集型工作负载设计,满足各种场景应用下的计算加速需求
- 地下水危机如何影响数据中心的运营
- 在更换域名后,并不是就万事大吉了,我们需要将旧域名做301重定向到新域名上,转移旧域名的权重到新域名上。
- 保护边缘数据中心:挑战与解决方案
- 数据中心无法实现自动化的五件事
- KADC 2025 东方通与鲲鹏深化合作 共筑RAG解决方案与出海新生态
- 为什么起域名意义非凡?起域名有什么名堂?
- 数据中心故障预防90%的问题源于这三点