Results 1 to 2 of 2

Thread: System.Diagnostics.PerformanceCategory question

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    System.Diagnostics.PerformanceCategory question

    All,

    I have created a custom performance category thus:-

    VB Code:
    1. Dim CounterDataCollection As New CounterCreationDataCollection()
    2. '\\ Create the counters and set their properties.
    3. Dim cdCounterTotal As New CounterCreationData()
    4. Dim cdCounterEMF As New CounterCreationData()
    5. cdCounterTotal.CounterName = "Spool files read"
    6. cdCounterTotal.CounterHelp = "Total spool files read"
    7. cdCounterTotal.CounterType = PerformanceCounterType.NumberOfItems64
    8. cdCounterEMF.CounterName = "EMF Spool files read"
    9. cdCounterEMF.CounterHelp = "Total EMF spool files read"
    10. cdCounterEMF.CounterType = PerformanceCounterType.NumberOfItems64
    11. '\\ Add both counters to the collection.
    12. CounterDataCollection.Add(cdCounterTotal)
    13. CounterDataCollection.Add(cdCounterEMF)
    14. '\\ Create the category and pass the collection to it.
    15. PerformanceCounterCategory.Create( _
    16. "Spoolfile reader", "Printer spoolfile reading perdormance",
    17. CounterDataCollection )

    How do I now send data to these performance counters?...i.e. how do I create a new PerformanceCounter object refering to these counters?

    Thanks in advance,
    Duncan

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    You can look here for an example my M$:

    http://support.microsoft.com/default...b;en-us;317679

    Heres a print from the M$ help file with a clear example.


    VB Code:
    1. Imports System
    2. Imports System.Collections
    3. Imports System.Collections.Specialized
    4. Imports System.Diagnostics
    5.  
    6. Public Class App
    7.  
    8.    Private Shared PC As PerformanceCounter
    9.    Private Shared BPC As PerformanceCounter
    10.    
    11.    
    12.    Public Shared Sub Main()
    13.      
    14.       Dim samplesList As New ArrayList()
    15.      
    16.       SetupCategory()
    17.       CreateCounters()
    18.       CollectSamples(samplesList)
    19.       CalculateResults(samplesList)
    20.    End Sub 'Main
    21.    
    22.    
    23.    
    24.    Private Shared Function SetupCategory() As Boolean
    25.       If Not PerformanceCounterCategory.Exists("AverageCounter64SampleCategory") Then
    26.          
    27.          Dim CCDC As New CounterCreationDataCollection()
    28.          
    29.          ' Add the counter.
    30.          Dim averageCount64 As New CounterCreationData()
    31.          averageCount64.CounterType = PerformanceCounterType.AverageCount64
    32.          averageCount64.CounterName = "AverageCounter64Sample"
    33.          CCDC.Add(averageCount64)
    34.          
    35.          ' Add the base counter.
    36.          Dim averageCount64Base As New CounterCreationData()
    37.          averageCount64Base.CounterType = PerformanceCounterType.AverageBase
    38.          averageCount64Base.CounterName = "AverageCounter64SampleBase"
    39.          CCDC.Add(averageCount64Base)
    40.          
    41.          ' Create the category.
    42.          PerformanceCounterCategory.Create("AverageCounter64SampleCategory", "Demonstrates usage of the AverageCounter64 performance counter type.", CCDC)
    43.          
    44.          Return True
    45.       Else
    46.          Console.WriteLine("Category exists - AverageCounter64SampleCategory")
    47.          Return False
    48.       End If
    49.    End Function 'SetupCategory
    50.    
    51.    
    52.    Private Shared Sub CreateCounters()
    53.       ' Create the counters.
    54.  
    55.       PC = New PerformanceCounter("AverageCounter64SampleCategory", "AverageCounter64Sample", False)
    56.      
    57.       BPC = New PerformanceCounter("AverageCounter64SampleCategory", "AverageCounter64SampleBase", False)
    58.      
    59.      
    60.       PC.RawValue = 0
    61.       BPC.RawValue = 0
    62.    End Sub 'CreateCounters
    63.    
    64.    
    65.    Private Shared Sub CollectSamples(samplesList As ArrayList)
    66.      
    67.       Dim r As New Random(DateTime.Now.Millisecond)
    68.      
    69.       ' Loop for the samples.
    70.       Dim j As Integer
    71.       For j = 0 To 99
    72.          
    73.          Dim value As Integer = r.Next(1, 10)
    74.          Console.Write((j + " = " + value))
    75.          
    76.          PC.IncrementBy(value)
    77.          
    78.          BPC.Increment()
    79.          
    80.          If j Mod 10 = 9 Then
    81.             OutputSample(PC.NextSample())
    82.             samplesList.Add(PC.NextSample())
    83.          Else
    84.             Console.WriteLine()
    85.          End If
    86.          System.Threading.Thread.Sleep(50)
    87.       Next j
    88.    End Sub 'CollectSamples
    89.    
    90.    
    91.    Private Shared Sub CalculateResults(samplesList As ArrayList)
    92.       Dim i As Integer
    93.       For i = 0 To (samplesList.Count - 1) - 1
    94.          ' Output the sample.
    95.          OutputSample(CType(samplesList(i), CounterSample))
    96.          OutputSample(CType(samplesList((i + 1)), CounterSample))
    97.          
    98.          ' Use .NET to calculate the counter value.
    99.          Console.WriteLine((".NET computed counter value = " + CounterSampleCalculator.ComputeCounterValue(CType(samplesList(i), CounterSample), CType(samplesList((i + 1)), CounterSample))))
    100.          
    101.          ' Calculate the counter value manually.
    102.          Console.WriteLine(("My computed counter value = " + MyComputeCounterValue(CType(samplesList(i), CounterSample), CType(samplesList((i + 1)), CounterSample))))
    103.       Next i
    104.    End Sub 'CalculateResults
    105.    
    106.    
    107.    
    108.    
    109.    '++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++
    110.    '    Description - This counter type shows how many items are processed, on average,
    111.    '        during an operation. Counters of this type display a ratio of the items
    112.    '        processed (such as bytes sent) to the number of operations completed. The  
    113.    '        ratio is calculated by comparing the number of items processed during the
    114.    '        last interval to the number of operations completed during the last interval.
    115.    ' Generic type - Average
    116.    '      Formula - (N1 - N0) / (D1 - D0), where the numerator (N) represents the number
    117.    '        of items processed during the last sample interval and the denominator (D)
    118.    '        represents the number of operations completed during the last two sample
    119.    '        intervals.
    120.    '    Average (Nx - N0) / (Dx - D0)  
    121.    '    Example PhysicalDisk\ Avg. Disk Bytes/Transfer
    122.    '++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++
    123.    Private Shared Function MyComputeCounterValue(s0 As CounterSample, s1 As CounterSample) As [Single]
    124.       Dim numerator As [Single] = CType(s1.RawValue, [Single]) - CType(s0.RawValue, [Single])
    125.       Dim denomenator As [Single] = CType(s1.BaseValue, [Single]) - CType(s0.BaseValue, [Single])
    126.       Dim counterValue As [Single] = numerator / denomenator
    127.       Return counterValue
    128.    End Function 'MyComputeCounterValue
    129.    
    130.    
    131.    ' Output information about the counter sample.
    132.    Private Shared Sub OutputSample(s As CounterSample)
    133.       Console.WriteLine(ControlChars.Lf + ControlChars.Cr + "+++++++++++")
    134.       Console.WriteLine("Sample values - " + ControlChars.Lf + ControlChars.Cr)
    135.       Console.WriteLine(("   BaseValue        = " + s.BaseValue))
    136.       Console.WriteLine(("   CounterFrequency = " + s.CounterFrequency))
    137.       Console.WriteLine(("   CounterTimeStamp = " + s.CounterTimeStamp))
    138.       Console.WriteLine(("   CounterType      = " + s.CounterType))
    139.       Console.WriteLine(("   RawValue         = " + s.RawValue))
    140.       Console.WriteLine(("   SystemFrequency  = " + s.SystemFrequency))
    141.       Console.WriteLine(("   TimeStamp        = " + s.TimeStamp))
    142.       Console.WriteLine(("   TimeStamp100nSec = " + s.TimeStamp100nSec))
    143.       Console.WriteLine("++++++++++++++++++++++")
    144.    End Sub 'OutputSample
    145. End Class 'App

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width