您的位置:首页 > 其它

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

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

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

///事例演示了

多长时间才能进一次: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("++++++++++++++++++++++");

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: