4.0十九章 检测----性能计数器

计数器是用以收集性能数据的机制。注册表存储所有计数器的名称,每个计数器均与系统功能的一个特定区域相关。示例包括处理器繁忙时间、内存占用情况或通过某个网络连接接收的字节数。

通过计数器的名称和位置可唯一地标识每个计数器。与文件路径包括驱动器、目录、一个或多个子目录和文件名一样,计数器信息由四个元素组成:计算机、对象、对象实例和计数器名称。

///事例演示了

多长时间才能进一次:j % 10 == 9
         public static void CollectSamples()
        {
            const String categoryName = "ElapsedTimeSampleCategory";
            const String counterName = "ElapsedTimeSample";


            // If the category does not exist, create the category and exit.
            // Performance counters should not be created and immediately used.
            // There is a latency time to enable the counters, they should be created
            // prior to executing the application that uses the counters.
            // Execute this sample a second time to use the category.
            if (!PerformanceCounterCategory.Exists(categoryName))
            {


                CounterCreationDataCollection CCDC = new CounterCreationDataCollection();


                // Add the counter.
                CounterCreationData ETimeData = new CounterCreationData();
                ETimeData.CounterType = PerformanceCounterType.ElapsedTime;
                ETimeData.CounterName = counterName;
                CCDC.Add(ETimeData);


                // Create the category.
                PerformanceCounterCategory.Create(categoryName,
                        "Demonstrates ElapsedTime performance counter usage.",
                    PerformanceCounterCategoryType.SingleInstance, CCDC);
                // Return, rerun the application to make use of the new counters.
                return;


            }
            else
            {
                Console.WriteLine("Category exists - {0}", categoryName);
            }


            // Create the performance counter.
            PerformanceCounter PC = new PerformanceCounter(categoryName,
                                                           counterName,
                                                           false);
            // Initialize the counter.
            PC.RawValue = Stopwatch.GetTimestamp();


            DateTime Start = DateTime.Now;


            // Loop for the samples.
            for (int j = 0; j < 100; j++)
            {
                // Output the values.
                if ((j % 10) == 9)
                {
                    Console.WriteLine("NextValue() = " + PC.NextValue().ToString());
                    Console.WriteLine("Actual elapsed time = " + DateTime.Now.Subtract(Start).ToString());
                    OutputSample(PC.NextSample());//Obtains a counter sample, and returns the raw, or uncalculated, value for it.
                    Start = DateTime.Now;//reset the time
                }


                // Reset the counter on every 20th iteration.
                //if (j % 20 == 0)
                //{
                //    PC.RawValue = Stopwatch.GetTimestamp();
                //    Start = DateTime.Now;
                //}
                System.Threading.Thread.Sleep(50);
            }


            Console.WriteLine("Elapsed time = " + DateTime.Now.Subtract(Start).ToString());
        }




        private static void OutputSample(CounterSample s)
        {
            Console.WriteLine("\r\n+++++++++++");
            Console.WriteLine("Sample values - \r\n");
            Console.WriteLine("   BaseValue        = " + s.BaseValue);
            Console.WriteLine("   CounterFrequency = " + s.CounterFrequency);
            Console.WriteLine("   CounterTimeStamp = " + s.CounterTimeStamp);
            Console.WriteLine("   CounterType      = " + s.CounterType);
            Console.WriteLine("   RawValue         = " + s.RawValue);
            Console.WriteLine("   SystemFrequency  = " + s.SystemFrequency);
            Console.WriteLine("   TimeStamp        = " + s.TimeStamp);
            Console.WriteLine("   TimeStamp100nSec = " + s.TimeStamp100nSec);
            Console.WriteLine("++++++++++++++++++++++");
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值