• 服务计数器


        /// <summary>
        /// CLR Version: 2.0.50727.5448
        /// NameSpace: Performence.Impl
        /// FileName: PerformenceCounter
        ///
        /// Created at 2014/03/18 10:23:38
        /// </summary>
        public class PerformenceCounter
        {
            #region fields
    
            private static readonly PerformenceCounter _current = new PerformenceCounter();
            private static readonly IDictionary<string, PerformanceCounter> PerfCounters = new Dictionary<string, PerformanceCounter>();
    
            private const string CategoryName = "PerformenceCounter.Service.Search";
    
            private const string SendPerSecond = "send/sec";
            private const string SendTotal = "send total";
            private const string SendFailedPerSecond = "failed/sec";
            private const string SendFailedTotal = "failed total";
            private const string ExceptionPerSecond = "error/sec";
            private const string ExcerptionTotal = "error total";
            private const string CacheHitPerSecond = "cache hit/sec";
            private const string CacheHitTotal = "cache hit total";
    
            private const string CacheFailedTotal = "cache failed total";
    
            #endregion
    
            #region .ctor
    
            #endregion
    
            #region properties
    
            public static PerformenceCounter Current
            {
                get { return _current; }
            }
    
            #endregion
    
            #region methods
    
            private void IncremnetPerCounter(string perfCounterName)
            {
                GetPerformanceCounter(perfCounterName).Increment();
            }
    
            private PerformanceCounter GetPerformanceCounter(string perfCounterName)
            {
                return PerfCounters[perfCounterName];
            }
    
            /// <summary>
            /// 创建计数器
            /// </summary>
            private void CreateCounters()
            {
                // 检查性能计数器是否存在
                Clear();
    
                CounterCreationDataCollection counterCollection = new CounterCreationDataCollection();
    
                CounterCreationData ccd1 = new CounterCreationData(SendPerSecond, "每秒发送数", PerformanceCounterType.RateOfCountsPerSecond64);
                counterCollection.Add(ccd1);
    
                CounterCreationData ccd2 = new CounterCreationData(SendTotal, "发送总数", PerformanceCounterType.NumberOfItems64);
                counterCollection.Add(ccd2);
    
                CounterCreationData ccd3 = new CounterCreationData(SendFailedPerSecond, "每秒发送失败数", PerformanceCounterType.RateOfCountsPerSecond64);
                counterCollection.Add(ccd3);
    
                CounterCreationData ccd4 = new CounterCreationData(SendFailedTotal, "发送失败总数", PerformanceCounterType.NumberOfItems64);
                counterCollection.Add(ccd4);
    
                CounterCreationData ccd5 = new CounterCreationData(ExceptionPerSecond, "每秒错误数(发送后超时或者异常数)", PerformanceCounterType.RateOfCountsPerSecond64);
                counterCollection.Add(ccd5);
    
                CounterCreationData ccd6 = new CounterCreationData(ExcerptionTotal, "错误总数(发送后超时或者异常数)", PerformanceCounterType.NumberOfItems64);
                counterCollection.Add(ccd6);
    
                CounterCreationData ccd7 = new CounterCreationData(CacheHitPerSecond, "每秒缓存命中数", PerformanceCounterType.RateOfCountsPerSecond64);
                counterCollection.Add(ccd7);
    
                CounterCreationData ccd8 = new CounterCreationData(CacheHitTotal, "缓存命中总数", PerformanceCounterType.NumberOfItems64);
                counterCollection.Add(ccd8);
    
                CounterCreationData ccd9 = new CounterCreationData(CacheFailedTotal, "缓存失效总数", PerformanceCounterType.NumberOfItems64);
                counterCollection.Add(ccd9);
    
                PerformanceCounterCategory.Create(CategoryName, "PerformanceCounter.Service.Search 发送计数器", PerformanceCounterCategoryType.SingleInstance, counterCollection);
            }
    
            public void Initiailize()
            {
                //创建计数器
                CreateCounters();
    
                // 添加到缓存中
                PerfCounters.Add(SendPerSecond, new PerformanceCounter(CategoryName, SendPerSecond, false));
                PerfCounters.Add(SendTotal, new PerformanceCounter(CategoryName, SendTotal, false));
                PerfCounters.Add(SendFailedPerSecond, new PerformanceCounter(CategoryName, SendFailedPerSecond, false));
                PerfCounters.Add(SendFailedTotal, new PerformanceCounter(CategoryName, SendFailedTotal, false));
                PerfCounters.Add(ExceptionPerSecond, new PerformanceCounter(CategoryName, ExceptionPerSecond, false));
                PerfCounters.Add(ExcerptionTotal, new PerformanceCounter(CategoryName, ExcerptionTotal, false));
                PerfCounters.Add(CacheHitPerSecond, new PerformanceCounter(CategoryName, CacheHitPerSecond, false));
                PerfCounters.Add(CacheHitTotal, new PerformanceCounter(CategoryName, CacheHitTotal, false));
            }
    
            /// <summary>
            /// 清理性能计数器
            /// </summary>
            public void Clear()
            {
                if (PerformanceCounterCategory.Exists(CategoryName))
                {
                    PerformanceCounterCategory.Delete(CategoryName);
                }
            }
    
    
            /// <summary>
            /// 发送计数
            /// </summary>
            public void IncrementSendTotal()
            {
                IncremnetPerCounter(SendTotal);
                IncremnetPerCounter(SendPerSecond);
            }
    
            /// <summary>
            /// 失败计数
            /// </summary>
            public void IncrementSendFaildTotal()
            {
                IncremnetPerCounter(SendFailedTotal);
                IncremnetPerCounter(SendFailedPerSecond);
            }
    
            /// <summary>
            /// 异常计数
            /// </summary>
            public void IncrementExcetionTotal()
            {
                IncremnetPerCounter(ExcerptionTotal);
                IncremnetPerCounter(ExceptionPerSecond);
            }
    
            /// <summary>
            /// 缓存命中计数
            /// </summary>
            public void IncrementCacheHitTotal()
            {
                IncremnetPerCounter(CacheHitTotal);
                IncremnetPerCounter(CacheHitPerSecond);
            }
    
            /// <summary>
            /// 缓存失效计数
            /// </summary>
            public void IncrementCacheFaildTotal()
            {
                IncremnetPerCounter(CacheFailedTotal);
            }
    
            #endregion
        }
    

     计数器:

       PerformenceCounter.Current.Initiailize();

       PerformenceCounter.Current.IncrementSendTotal();

       PerformenceCounter.Current.IncrementCacheHitTotal();

       PerformenceCounter.Current.IncrementCacheFaildTotal();

       PerformenceCounter.Current.IncrementSendFaildTotal();   

       //当没有缓存命中 直接记异常
      PerformenceCounter.Current.IncrementExcetionTotal();

  • 相关阅读:
    MYSQL57密码策略修改
    dataSource' defined in class path resource [org/springframework/boot/autocon
    ubuntu14.04 red5
    hadoop中修改端口号
    FATAL org.apache.hadoop.hdfs.server.datanode.DataNode: Initialization failed for Block pool <registering> (Datanode Uuid unassigned) service to controller/192.168.1.183:9000. Exiting. java.io.IOExcep
    hadoop ssh 问题WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
    openstack安装dashboard后访问horizon出错 End of script output before headers: django.wsgi
    sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) Cannot add a NOT NULL column with default value NULL [SQL: u'ALTER TABLE address_scopes ADD COLUMN ip_version INTEGER NOT NULL']
    mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")
    OpenStack安装keyston 错误BError: (pymysql.err.InternalError) (1071, u‘Specified key was too long; max key length is 767 bytes‘) [SQL: u‘ CREATE TABLE migrate_ver
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3607260.html
Copyright © 2020-2023  润新知