Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Specialized;
  4. using System.Diagnostics;
  5. using System.Runtime.InteropServices;
  6.  
  7. public class App
  8. {
  9.     private static PerformanceCounter PC;
  10.  
  11.  
  12.     public static void Main()
  13.     {    
  14.         ArrayList samplesList = new ArrayList();
  15.  
  16.         SetupCategory();
  17.         CreateCounters();
  18.         CollectSamples(samplesList);
  19.     }
  20.  
  21.  
  22.     private static bool SetupCategory()
  23.     {
  24.         if ( !PerformanceCounterCategory.Exists("ElapsedTimeSampleCategory") )
  25.         {
  26.  
  27.             CounterCreationDataCollection CCDC = new CounterCreationDataCollection();
  28.  
  29.             // Add the counter.
  30.             CounterCreationData ETimeData = new CounterCreationData();
  31.             ETimeData.CounterType = PerformanceCounterType.ElapsedTime;
  32.             ETimeData.CounterName = "ElapsedTimeSample";
  33.             CCDC.Add(ETimeData);      
  34.        
  35.             // Create the category.
  36.             PerformanceCounterCategory.Create("ElapsedTimeSampleCategory",
  37.                 "Demonstrates usage of the ElapsedTime performance counter type.",    
  38.                 CCDC);
  39.  
  40.             return(true);
  41.         }
  42.         else
  43.         {
  44.             Console.WriteLine("Category exists - ElapsedTimeSampleCategory");
  45.             return(false);
  46.         }
  47.     }
  48.  
  49.     private static void CreateCounters()
  50.     {
  51.         // Create the counter.
  52.         PC = new PerformanceCounter("ElapsedTimeSampleCategory",
  53.             "ElapsedTimeSample",
  54.             false);
  55.  
  56.     }
  57.  
  58.     private static void CollectSamples(ArrayList samplesList)
  59.     {
  60.    
  61.         long pcValue;
  62.         DateTime Start;
  63.  
  64.         // Initialize the counter.
  65.         QueryPerformanceCounter(out pcValue);
  66.         PC.RawValue = pcValue;
  67.         Start = DateTime.Now;
  68.  
  69.         // Loop for the samples.
  70.         for (int j = 0; j < 1000; j++)
  71.         {
  72.             // Output the values.
  73.             if ((j % 10) == 9)
  74.             {
  75.                 Console.WriteLine("NextValue() = " + PC.NextValue().ToString());
  76.                 Console.WriteLine("Actual elapsed time = " + DateTime.Now.Subtract(Start).ToString());
  77.                 OutputSample(PC.NextSample());
  78.                 samplesList.Add( PC.NextSample() );
  79.             }
  80.  
  81.             // Reset the counter on 100th iteration.
  82.             if (j % 100 == 0)
  83.             {
  84.                 QueryPerformanceCounter(out pcValue);
  85.                 PC.RawValue = pcValue;
  86.                 Start = DateTime.Now;
  87.             }
  88.             System.Threading.Thread.Sleep(50);
  89.         }
  90.  
  91.         Console.WriteLine("Elapsed time = " + DateTime.Now.Subtract(Start).ToString());
  92.     }
  93.  
  94.    
  95.     private static void OutputSample(CounterSample s)
  96.     {
  97.         Console.WriteLine("\r\n+++++++++++");
  98.         Console.WriteLine("Sample values - \r\n");
  99.         Console.WriteLine("   BaseValue        = " + s.BaseValue);
  100.         Console.WriteLine("   CounterFrequency = " + s.CounterFrequency);
  101.         Console.WriteLine("   CounterTimeStamp = " + s.CounterTimeStamp);
  102.         Console.WriteLine("   CounterType      = " + s.CounterType);
  103.         Console.WriteLine("   RawValue         = " + s.RawValue);
  104.         Console.WriteLine("   SystemFrequency  = " + s.SystemFrequency);
  105.         Console.WriteLine("   TimeStamp        = " + s.TimeStamp);
  106.         Console.WriteLine("   TimeStamp100nSec = " + s.TimeStamp100nSec);
  107.         Console.WriteLine("++++++++++++++++++++++");
  108.     }
  109.  
  110.  
  111.     // Reads the counter information to enable setting the RawValue.
  112.     [DllImport("Kernel32.dll")]
  113.     public static extern bool QueryPerformanceCounter(out long value);
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement