博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用 .NET Core 中的 EventCounters 衡量性能
阅读量:4036 次
发布时间:2019-05-24

本文共 3281 字,大约阅读时间需要 10 分钟。

背景

对于每隔几毫秒发生的事件,最好使每个事件的开销较低(小于一毫秒)。 否则,对性能的影响将很大。 记录事件意味着你将向磁盘写入内容。 如果磁盘不够快,你将丢失事件。 你需要一个解决方案,而不是记录事件本身。

在处理大量事件时,了解每个事件的度量值也无济于事。 大多数时候,你只需要一些统计信息。 因此,你可以在进程本身中获取统计信息,然后偶尔编写一个事件来报告统计信息,这是 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
()); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApplication42", Version = "v1" }); }); }

url:https://localhost:5008/WeatherForecast  elapsedMilliseconds:70url:https://localhost:5008/WeatherForecast  elapsedMilliseconds:19url:https://localhost:5008/WeatherForecast  elapsedMilliseconds:18url:https://localhost:5008/WeatherForecast  elapsedMilliseconds:19url:https://localhost:5008/WeatherForecast  elapsedMilliseconds:22url:https://localhost:5008/WeatherForecast  elapsedMilliseconds:17url:https://localhost:5008/WeatherForecast  elapsedMilliseconds:17

转载地址:http://jgkdi.baihongyu.com/

你可能感兴趣的文章
JavaScript基础知识(2)
查看>>
转载一个webview开车指南以及实际项目中的使用
查看>>
android中对于非属性动画的整理
查看>>
一个简单的TabLayout的使用
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
android给文字加边框(修改不能居中的问题)
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
coursesa课程 Python 3 programming Dictionary methods 字典的方法
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
visca接口转RS-232C接口线序
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>