C#-获取原始分数性能计数器以显示持久值

C#-获取原始分数性能计数器以显示持久值,c#,.net,performancecounter,C#,.net,Performancecounter,我创建了一个性能计数器,它显示递增值(RawFractiontype)相对于基值(RawBase)的分数 不幸的是,在监视此值时,它仅显示其中一个计数器递增时的百分比。在所有其他采样时间,它显示为0。有没有办法告诉计数器在下次需要重新计算分数之前保留最后一个值?如果没有看到任何代码,很难知道发生了什么错误,但下面是一个正确使用该值的示例(来源:) 使用系统; 使用系统集合; 使用System.Collections.Specialized; 使用系统诊断; 公共类应用程序 { 专用静态性能计数器

我创建了一个性能计数器,它显示递增值(
RawFraction
type)相对于基值(
RawBase
)的分数


不幸的是,在监视此值时,它仅显示其中一个计数器递增时的百分比。在所有其他采样时间,它显示为0。有没有办法告诉计数器在下次需要重新计算分数之前保留最后一个值?

如果没有看到任何代码,很难知道发生了什么错误,但下面是一个正确使用该值的示例(来源:)

使用系统;
使用系统集合;
使用System.Collections.Specialized;
使用系统诊断;
公共类应用程序
{
专用静态性能计数器PC;
私有静态性能计数器BPC;
公共静态void Main()
{
ArrayList SampleList=新的ArrayList();
//如果类别不存在,请创建该类别并退出。
//不应创建并立即使用性能计数器。
//有一个延迟时间来启用计数器,应该创建它们
//在执行使用计数器的应用程序之前。
//再次执行此示例以使用计数器。
如果(SetupCategory())
返回;
CreateCounters();
采集样本(样本列表);
计算结果(样本列表);
}
私有静态bool SetupCategory()
{
如果(!PerformanceCounterCategory.Exists(“RawFractionSampleCategory”))
{
CounterCreationDataCollection CCDC=新的CounterCreationDataCollection();
//添加计数器。
CounterCreationData rf=新的CounterCreationData();
rf.CounterType=PerformanceCounterType.RawFraction;
rf.CounterName=“RawFractionSample”;
添加(rf);
//添加基本计数器。
CounterCreationData rfBase=新的CounterCreationData();
rfBase.CounterType=PerformanceCounterType.RawBase;
rfBase.CounterName=“RawFractionSampleBase”;
CCDC.Add(rfBase);
//创建类别。
PerformanceCounterCategory.Create(“RawFractionSampleCategory”,
“演示原始分数性能计数器类型的用法。”,
PerformanceCounterCategoryType.SingleInstance,CCDC);
返回(真);
}
其他的
{
Console.WriteLine(“类别存在-RawFractionSampleCategory”);
返回(假);
}
}
私有静态void CreateCounters()
{
//创建计数器。
PC=新性能计数器(“RawFractionSampleCategory”,
“RawFractionSample”,
假);
BPC=新性能计数器(“RawFractionSampleCategory”,
“RawFractionSampleBase”,
假);
PC.RawValue=0;
BPC.RawValue=0;
}
私有静态void CollectSamples(ArrayList samplesList)
{
Random r=新随机数(DateTime.Now.毫秒);
//初始化性能计数器。
PC.NextSample();
//循环查找样本。
对于(int j=0;j<100;j++)
{
int值=r.Next(1,10);
控制台写入(j+“=”+值);
//每次递增基数,因为计数器测量数字
//高命中率(原始分数值)与所有命中率(基值)的对比。
BPC.Increment();
//获取所有采集样本中9或10个样本的百分比。
如果(值>=9)
PC.Increment();
//在循环中每隔十次复制下一个值。
如果((j%10)==9)
{
Console.WriteLine(“;NextValue()=”+PC.NextValue().ToString());
OutputSample(PC.NextSample());
samplesList.Add(PC.NextSample());
}
其他的
Console.WriteLine();
系统.线程.线程.睡眠(50);
}
}
私有静态无效计算结果(ArrayList SampleList)
{
for(int i=0;iusing System;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;

public class App
{
    private static PerformanceCounter PC;
    private static PerformanceCounter BPC;

    public static void Main()
    {
        ArrayList samplesList = new ArrayList();

        // 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 counters.
        if (SetupCategory())
            return;
        CreateCounters();
        CollectSamples(samplesList);
        CalculateResults(samplesList);
    }

    private static bool SetupCategory()
    {
        if (!PerformanceCounterCategory.Exists("RawFractionSampleCategory"))
        {
            CounterCreationDataCollection CCDC = new CounterCreationDataCollection();

            // Add the counter.
            CounterCreationData rf = new CounterCreationData();
            rf.CounterType = PerformanceCounterType.RawFraction;
            rf.CounterName = "RawFractionSample";
            CCDC.Add(rf);

            // Add the base counter.
            CounterCreationData rfBase = new CounterCreationData();
            rfBase.CounterType = PerformanceCounterType.RawBase;
            rfBase.CounterName = "RawFractionSampleBase";
            CCDC.Add(rfBase);

            // Create the category.
            PerformanceCounterCategory.Create("RawFractionSampleCategory",
                "Demonstrates usage of the RawFraction performance counter type.",
                PerformanceCounterCategoryType.SingleInstance, CCDC);

            return (true);
        }
        else
        {
            Console.WriteLine("Category exists - RawFractionSampleCategory");
            return (false);
        }
    }

    private static void CreateCounters()
    {
        // Create the counters.
        PC = new PerformanceCounter("RawFractionSampleCategory",
            "RawFractionSample",
            false);

        BPC = new PerformanceCounter("RawFractionSampleCategory",
            "RawFractionSampleBase",
            false);

        PC.RawValue = 0;
        BPC.RawValue = 0;
    }

    private static void CollectSamples(ArrayList samplesList)
    {
        Random r = new Random(DateTime.Now.Millisecond);

        // Initialize the performance counter.
        PC.NextSample();

        // Loop for the samples.
        for (int j = 0; j < 100; j++)
        {
            int value = r.Next(1, 10);
            Console.Write(j + " = " + value);

            // Increment the base every time, because the counter measures the number 
            // of high hits (raw fraction value) against all the hits (base value).
            BPC.Increment();

            // Get the % of samples that are 9 or 10 out of all the samples taken.
            if (value >= 9)
                PC.Increment();

            // Copy out the next value every ten times around the loop.
            if ((j % 10) == 9)
            {
                Console.WriteLine(";       NextValue() = " + PC.NextValue().ToString());
                OutputSample(PC.NextSample());
                samplesList.Add(PC.NextSample());
            }
            else
                Console.WriteLine();

            System.Threading.Thread.Sleep(50);
        }
    }

    private static void CalculateResults(ArrayList samplesList)
    {
        for (int i = 0; i < samplesList.Count; i++)
        {
            // Output the sample.
            OutputSample((CounterSample)samplesList[i]);

            // Use .NET to calculate the counter value.
            Console.WriteLine(".NET computed counter value = " +
                CounterSampleCalculator.ComputeCounterValue((CounterSample)samplesList[i]));

            // Calculate the counter value manually.
            Console.WriteLine("My computed counter value = " +
                MyComputeCounterValue((CounterSample)samplesList[i]));
        }
    }

    //++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++
    // Formula from MSDN -
    //      Description - This counter type shows the ratio of a subset to its set as a percentage.
    //            For example, it compares the number of bytes in use on a disk to the
    //            total number of bytes on the disk. Counters of this type display the 
    //            current percentage only, not an average over time.
    //
    // Generic type - Instantaneous, Percentage 
    //        Formula - (N0 / D0), where D represents a measured attribute and N represents one
    //            component of that attribute.
    //
    //        Average - SUM (N / D) /x 
    //        Example - Paging File\% Usage Peak
    //++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++
    private static Single MyComputeCounterValue(CounterSample rfSample)
    {
        Single numerator = (Single)rfSample.RawValue;
        Single denomenator = (Single)rfSample.BaseValue;
        Single counterValue = (numerator / denomenator) * 100;
        return (counterValue);
    }

    // Output information about the counter sample.
    private static void OutputSample(CounterSample s)
    {
        Console.WriteLine("+++++++++++");
        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("++++++++++++++++++++++");
    }
}