2010-03-11 5 views
1

J'ai créé un compteur de performances qui affiche une fraction d'une valeur incrémentée (type RawFraction) sur une valeur de base (RawBase).C# - Obtenir un compteur de performance RawFraction pour afficher une valeur persistante

Malheureusement, lors de la surveillance de cette valeur, il affiche uniquement le pourcentage lorsque l'un des compteurs est incrémenté. Dans tous les autres cas, il est échantillonné, il montre 0. Y at-il un moyen de dire au compteur de conserver la dernière valeur jusqu'à la prochaine fois qu'il doit recalculer la fraction?

+1

Pouvez-vous poster une partie de ce code s'il vous plaît? – MPelletier

+1

Oui, s'il vous plaît nous montrer du code. –

+0

Désolé les gars, été très occupé ... Je vais poster du code sur le lendemain ou deux et réinitialiser la prime – mwjackson

Répondre

3

Il est difficile de savoir ce qui a mal tourné pour vous sans voir votre code, mais voici un exemple de celui-ci utilisé correctement (à partir de: http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecountertype.aspx)

using 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("++++++++++++++++++++++"); 
    } 
} 
+2

Ceci est une réponse généralement acceptable à une question très vague :) – MPelletier

Questions connexes